Questions tagged [functional-programming]
Use this tag for programs where "functional" style is important - likely making use of function composition and without side-effects. NOT simply because your code contains functions!
1,018 questions
50
votes
1
answer
2k
views
Connect Four AI (Minimax) in Clojure
I wrote a Connect Four game including a AI in Clojure and since I'm rather new to Clojure, some review would be highly appreciated. It can include everything, coding style, simplifications, etc.
But ...
33
votes
6
answers
3k
views
To 'this' or not to 'this'?
I was given a homework and I have 2 solutions: one that uses this and other one that doesn't.
I tested both solutions on jsPerf but sometimes it says the version ...
26
votes
3
answers
2k
views
Project Euler Problem 2 in Clojure
I am in the process of learning Clojure. I am fairly new to functional programming and would like to know if my code smells or if there are any performance implications with my approach.
...
22
votes
1
answer
4k
views
Java monad implementation
I'm learning functional programming and their concept of Monads. I've found nothing more effective in learning than writing an implementation in a programming language I have experience with.
I came ...
20
votes
1
answer
7k
views
Code with many "early returns (exits)" into the functional style
I have some Scala code that uses Internet to authorize a user. Therefore, it can throw Exceptions like IOException in the method.
The original code was written in ...
19
votes
0
answers
220
views
Timeoutable computations module
Defines a simple module for timeoutable computations, with the ability to return
arbitrary intermediary results on timeout or the final value otherwise. It also
allows default return values.
The ...
18
votes
2
answers
6k
views
Replacing Python Classes with Modules [closed]
I try to avoid using classes in Python as much as possible; if I don't plan on building on it, I don't build it in the first place. It helps me avoid Java-like classes like ...
16
votes
0
answers
3k
views
Realtime concurrent Haskell MIDI buffer
Problem Background
MIDI is a serial representation of control signals to a sound generator. Typically, a noteOn message initiates the attack phase of a sound at a given pitch. The note will ...
15
votes
3
answers
114k
views
Compare values of elements in Stream
I'm having fun with Java's Stream library and lambdas.
The following code looks for persons within a list that have the same ID (which might indicate that something's wrong with the data) and prints ...
15
votes
2
answers
2k
views
Message-formatting code with pluralization, revised to be more functional
I'm a C++ dev, and I've recently started working my way through Clean Code*. Whenever I encounter an example I think I could improve, I try to re-implement it in C++.
On pp. 28-29 there is an example ...
15
votes
1
answer
535
views
Clojure Minesweeper from scratch
In order to exercise and learn Clojure, I decided to write a simple Minesweeper game from scratch. I'd consider myself as a Clojure novice and would be thankful if somebody could do a review or give ...
14
votes
2
answers
40k
views
Flatten dictionary in Python (functional style)
I'm trying to learn how to write functional code with Python and have found some tutorials online. Please note that I know Python is not a promoter for functional programming. I just want to try it ...
14
votes
1
answer
3k
views
C++ identity function
I've implemented an identity function (well, actually a functor struct) in C++. The goal is that every occurrence of an ...
14
votes
1
answer
678
views
Grand Chess domain model and helper functions
So I am trying to write, essentially from a blank slate, a program that plays Grand Chess. In short, it is a chess variant that is played with two extra pieces, on a 10x10 board, no castling, and ...
13
votes
3
answers
10k
views
Apply a function to each element of a tuple ("map" a tuple)
C++ doesn't (yet) have a 'map' higher-order function, certainly not one which applies to a compile-time sequence of values like a tuple. We have std::transform, and ...
13
votes
2
answers
609
views
Concatenative PostScript library
As a part of picking up concatenative programming, I decided to implement the common concatenative operations in PostScript. Here is my attempt at implementing some of the words in other concatenative ...
13
votes
2
answers
756
views
Printing removed items using lambdas and streams
I'm struggling to make my lambdas readable.
I've seen various approaches. Below are three different examples that all do the same thing.
Forgive the example: I suspect there are better actual ...
13
votes
1
answer
483
views
Voting plugin for an IRC bot
MetaBrainz has an IRC channel called #metabrainz on which there's a bot called BrainzBot running. It provides utilities such as linking to JIRA issues or Github PRs ...
13
votes
1
answer
2k
views
Detecting cycles in a directed graph without using mutable sets of nodes
I recently came across the classic algorithm for detecting cycles in a directed graph using recursive DFS. This implementation makes use of a stack to track nodes currently being visited and an extra ...
12
votes
1
answer
2k
views
Responsive/adaptive website code
I have all these functions that work together to create functionality on a page. Is the structure of these functions OK? Can I do anything to speed this up or make my code better? I'm not exactly ...
12
votes
2
answers
2k
views
Given an array A[] and a number x, check for pair in A[] with sum as x
LINQ/FP style:
Assert.IsTrue(new[] {1,2,3}.AnyPairSum(4));
Where:
...
12
votes
1
answer
990
views
Genetic algorithm for "Hello World"
I've written an Erlang implementation of the genetic algorithm for a "Hello World" program as described here.
This is my first time writing any code in Erlang and also my first time writing code in a ...
12
votes
1
answer
393
views
Cat fight (with rockets)
My code is attempting to implement FP in an efficient / readable manner into some of my video games. I realize this may be a bit subjective, but I feel there is enough merit / objectivity to be of ...
11
votes
4
answers
3k
views
Restructuring JSON to create a new JSON where properties are grouped according to similar values
I have a JSON structure in the following example format:
...
11
votes
2
answers
2k
views
Luhn algorithm in Ruby
I'm learning Ruby 2.3 and I've tried to implement a function which performs the Luhn credit card verification algorithm on an input string, returning true if it ...