Questions tagged [functional-programming]

Functional programming is a paradigm which attempts to solve computational problems by the chained evaluation of functions whose output is determined by their inputs rather than the program state. In this style of programming, side effects and mutable data are deprecated.

Filter by
Sorted by
Tagged with
7
votes
2answers
589 views

Recursive higher-order function

I have implemented a through function for myself in a project. Since I am still quite new in python and want to learn it correctly, I would be happy about any ...
7
votes
1answer
299 views

String Format object extension

I wrote an extension to create a string-representation by an object: ...
7
votes
1answer
151 views

Improving multiple assignment using an iterator function

Often I need to perform multiple assignment from a sequence (e.g. from the result of a split()) but with some semantics that, AFAIK, are not available in the ...
7
votes
2answers
320 views

Chaining statistics in a dice queue

I've previously came up with the question "Rolling dice in a method chain" and was interested if this could be improved. It could, so now I have this class: ...
7
votes
2answers
990 views

2D lattice random walk plots in functional style

To practice writing code in the functional programming style, I wrote a program to plot two-dimensional lattice random walks. I'd appreciate any feedback about how to improve its "functionaliness". <...
7
votes
3answers
328 views

Functional Programming style in F#

I'm normally a C# developer, but I've started to learn F# and I want to make sure I'm writing code in a functional way that suits the language. I've quickly pieced this together with my knowledge from ...
7
votes
2answers
8k views

Function that builds dictionary based on lambda params

I've written a method in c# that allows me to do create a dictionary from a passed in object and N lambda expressions that reference that objects properties and methods. It's working the way I want it ...
7
votes
1answer
735 views

takeWhile, groupBy and group higher order functions

I'm trying to implement few higher order functions from Haskell in C++. Below are the definitions of takeWhile, groupBy, and <...
7
votes
1answer
268 views

Functional-style list in C++

I've implemented a functional-style list structure in C++. It seems okay so far, and doing custom allocation for list nodes improves performance greatly. I omitted that part here for simplicity. The ...
7
votes
1answer
1k views

Generic pure functions memoization

I like to create tools for memoization since it can sometimes be useful. I recently created a generic tool for memoizing the results of pure functions. Here is an example of how it works: ...
7
votes
1answer
510 views

Can this be written more concise and aligned with the Functional Reactive Programming paradigm?

Trying to learn FRP and Elm. Is the program below "ok" from the perspective of FRP and Elm? What could be improved? The program can be executed here: http://elm-lang.org/try ...
7
votes
1answer
200 views

Small console app to execute some remote scripts

I'm learning F# and functional programming, from a background in C# and imperative/OOP. I've ported a small, one-off console app from C# to F#. The port worked (the app behaves the same way), but I'd ...
7
votes
1answer
719 views

Elixir pipes and anonymous functions

I recently got started with Elixir. I'm used to F#'s pipes, and Seq.map and LINQ's .Select statements. Things are different in ...
7
votes
1answer
321 views

Sundaram's sieve in Common Lisp

I have this Sundaram's sieve to generate prime numbers up to limit in Common Lisp. ...
7
votes
1answer
931 views

F# Djikstras shortest path implementation

I'm just getting my feet wet with F#, by implementing some simple algorithms. First up: Djikstras shortest path. There is one piece I've written in this code which confuses me as to why it works: the ...
7
votes
1answer
79 views

Monadic list for a more functional approach to C#

I am learning about monadic structures and have created a sort of monad list that implements the map and binding "operators" (or technically methods) in C#. I understand that Linq already exists and ...
7
votes
1answer
153 views

Avoid incoming meteors

I'm visiting a functional programming course at my university which has a small project for examination. The language we are using is clojure and the contents of the lecture have mostly been about it'...
7
votes
1answer
277 views

F# Either computation expression with while loop

I want to implement an Either computation expression in F#. The computation executes a function inside a while loop, and this function's return type is ...
7
votes
1answer
260 views

Functional K-way merge (Merge k sorted sequences)

I've implemented a functional k way-merge algorithm to merge k sorted sequences of any type T. I used scalaz's Heap since the Scala standard library does not provide an immutable heap. Any suggestions ...
7
votes
1answer
2k views

Functional programming in Python: 2048 merge functions

I've written the core functions for a basic 2048 game. I know Python isn't a functional programming language, but I like the "functional style" (easier to maintain, simple to understand, more elegant),...
7
votes
2answers
182 views

Defining a simple function-calling operator

I have been writing Swift code for a while and have read a lot of tutorials about how functional paradigms can be applied to Swift. I defined a simple operator: ...
7
votes
2answers
231 views

Getting functional Common Lisp code for handling state in a SDL animation

The following code draws a red rectangle bouncing between the borders of a white display. I'm not particularly happy with the update function: ...
7
votes
1answer
519 views

Combinations of size k from a list in OCaml

I wanted to write a small but non-trivial function in OCaml to test my understanding. I have only a basic understanding of the libraries, and no idea at all of what is considered good style. The ...
7
votes
1answer
390 views

Hangman in stateless JavaScript

I just want some feedback to improve readability and structure for this implementation of Hangman. The goal was to write it in a purely functional way. However, coming from an imperative background, I ...
7
votes
1answer
22k views

Converting an object to an array of objects

I've written a method that takes an object whose keys are arrays and converts it to an array of objects. I feel like the code is fairly complex, after waiting a week or so and revisiting it, I don't ...
7
votes
1answer
734 views

Clojure TicTacToe (logic, no GUI)

I whipped this up last night and was wondering if anyone had any thoughts/comments on it; for example, on: Code organisation Coding style Other improvements Semantic errors ...
7
votes
1answer
244 views
7
votes
1answer
310 views

In-lining InvocationExpressions

This is regards to one of my answers: Linq Expression Calling Combines. The question wasn't very well received, but it proved to be an interesting task that I enjoyed working on. I ended up with the ...
7
votes
1answer
606 views

Game of Life rules in 14 lines of Python

I've attempted a functional solution to Conway's Game of Life in Python. The example code allows you to see the next generation of the universe by calling the ...
7
votes
2answers
517 views

Higher-order Prolog predicates combine/3 and reduce/3

I'm trying to find useful versatile meta-predicates that are different from the vanilla ones offered currently by library(apply). How about the following two: <...
7
votes
1answer
83 views

Using delegates to avoid duplicate creation of resources

I'm writing a PCL that uses an HttpClient to go visit a few sites and extract data from them. My initial code looked like this: ...
7
votes
0answers
98 views

Agent-based Immutable Map

I am in the process of removing the last few pieces of mutable state from an F#-based distributed system. Some of the remaining mutable state is a ...
7
votes
0answers
1k views

Flask view dependency injection

so, first of all, I've been into trying to have a simple dependency container as possible in python and with your help managed to get conceive https://github.com/dareenzo/tinydic, many thanks. Now, I ...
6
votes
2answers
13k views

Palindrome test in Haskell

I am new to Haskell and, coming from a OO/procedural programming background, I want to make sure my code follows the functional way of doing things. I built this short module to test if a given string ...
6
votes
3answers
2k views

A safer way to cut a string

I want to get just the first line of a big string. Currently, here's how I do it: ...
6
votes
2answers
623 views

Array duplicate removal for duplicates exceeding `N`-number

I recently encountered a front-end interview coding challenge question that required one to create a function which returned an array that excluded numbers that occurred more than ...
6
votes
3answers
2k views

GCD of two numbers in Ruby

I am trying to implement a algorithm for the GCD of two numbers in a functional approach. Can this be improved further, also with regards to performance? ...
6
votes
3answers
760 views

Flatten an array

I have got this interview question which has asked me to write a production level code which flattens the arbitrary nested array of arrays. Code ...
6
votes
4answers
1k views

Find all letter Combinations of a Phone Number

The task is taken from LeetCode Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just ...
6
votes
3answers
13k views

Hailstone Sequences in Python

For the problem given: Douglas Hofstadter’s Pulitzer-prize-winning book, Gödel, Escher, Bach, poses the following mathematical puzzle. Pick a positive integer \$n\$ as the start. If \$n\$ is ...
6
votes
3answers
163 views

Generic method to call API functions with simple retrial on errors

I have to develop a small security management application that relies on a Web API. Based on that API, I have generated the REST client classes (Open API generator), but I still have to code on top to ...
6
votes
3answers
535 views

Create an array with the highest values from each array

I am working my way through FreeCodeCamp's Javascript challenges and Return Largest Numbers in Arrays took me a minute. In this one the goal was to create an array with the highest values from each ...
6
votes
3answers
264 views

Number of Chocolates to Gluttonize in Clojure

I’ve been dabbling in Clojure by solving programming puzzles. This particular puzzle is from HackerRank, with the objective of figuring out how much chocolate one could eat given three parameters: ...
6
votes
5answers
430 views

Is this good approach to use debug functions for implementing features?

Just thinking if using code like this ...
6
votes
1answer
4k views

Binary search in functional-style Scala

Curious to know how I can improve it, especially the pattern matching part seems a bit repetitive at the moment (lots of case x if ds(x)) ...
6
votes
3answers
412 views

Funny string python solution

Problem Statement Suppose you have a string \$S\$ which has length \$N\$ and is indexed from \$0\$ to \$N−1\$. String \$R\$ is the reverse of the string \$S\$. The string \$S\$ is funny if the ...
6
votes
2answers
70 views

Refactor module for calls to internal image service

Looking for help refactoring the following code to follow a Functional Programming paradigm. The first function builds a configuration object with default settings and optional presets, while the <...
6
votes
1answer
1k views

Function composition in Python3

I wanted to implement a version of rambda's compose function in python. I also implemented some examples to show its working. However, I would like the community's input on my code: These are the ...
6
votes
1answer
510 views

Convert time 24hr format into time as words

I'm currently writing a Scala Object that converts time 24hr format (i.e. 00:00 - 23:59) into Time As Words (i.e. Eight o'clock, Half past eight, Quarter to nine etc). How can I refactor the current ...