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!

Filter by
Sorted by
Tagged with
12 votes
1 answer
1k 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 ...
HandiworkNYC.com's user avatar
8 votes
1 answer
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: ...
Morwenn's user avatar
  • 20k
6 votes
2 answers
513 views

Improving my Tic Tac Toe Solution in Scala

I'm relatively new to Scala and made a small TicTacToe in a functional Style. Any improvement options would be highly appreciated. There are some things which I am unsure of if they are made in an ...
elauser's user avatar
  • 179
5 votes
1 answer
757 views

Refactoring away from exceptions

What do you use for refactoring away from exceptions while programming functional in C#? I defined this class to hold function outcome: ...
Dmitry Nogin's user avatar
  • 6,131
4 votes
1 answer
437 views

Determine whether any permutation of a string is a palindrome

The task: Given a string, determine whether any permutation of it is a palindrome. For example, "carrace" should return true, since it can be rearranged ...
thadeuszlay's user avatar
  • 4,003
4 votes
2 answers
1k views

Wrath Cookie Switch for Auto Golden Cookie Clicker

Sometimes you feel like a wrath cookie and sometimes you don't. Here is a function that I created that will run on an interval, so all I have to do is set a boolean when I want to switch between ...
Malachi's user avatar
  • 28.6k
3 votes
2 answers
273 views

Sequence processing problems

Problem 1: Sum the even numbers for the first n fibonacci numbers Solution: ...
overexchange's user avatar
  • 3,321
3 votes
2 answers
1k views

Find the elements that appear only once

The task: Given an array of integers in which two elements appear exactly once and all other elements appear exactly twice, find the two elements that appear only once. For example, given ...
thadeuszlay's user avatar
  • 4,003
2 votes
1 answer
295 views

Parser Combinators in OO C

In trying to understand and use the Parser Combinator concept, I've coded up as close an analogue as I could manage within the constraints of the C language. C doesn't have first-class functions or ...
luser droog's user avatar
  • 2,018
2 votes
1 answer
105 views

Newspaper Bill Calculator CLI with Python (1 of 3, Core)

Code is posted after explanation. Due to the size of the project, this is being posted in three separate posts. This also ensures each post is more focused. Post 2 of 3, CLI: Newspaper Bill ...
eccentricOrange's user avatar
1 vote
2 answers
81 views

UPDATE on Newspaper Bill Calculator CLI with Python (1 of 3, Core)

Code is posted after explanation. Due to the size of the project, this is being posted in three separate posts. This also ensures each post is more focused. Post 2 of 3, CLI: UPDATE 1 on Newspaper ...
eccentricOrange's user avatar
0 votes
2 answers
375 views

Write an anonymous function validating email address

Problem statement Write an anonymous function which take email address as parameter and returns true/false after validating the input parameter. Objective This assignment will help you ...
overexchange's user avatar
  • 3,321
19 votes
1 answer
417 views

Streaming Collatz

Challenge The recent question The 3n + 1 algorithm for a range inspired me to investigate a Java-8 dependent streaming mechanism for solving the programming challenge: For any two numbers ...
rolfl's user avatar
  • 97.3k
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 ...
Zizheng Tai's user avatar
12 votes
2 answers
706 views

N-Queens functional

I'm trying to get a better grip on the new functional possibilities of Java 8. As an example, I took this very elegant Haskell snippet: ...
Landei's user avatar
  • 6,982
11 votes
4 answers
2k views

Am I thinking functionally in these simple Haskell functions?

I'm learning Haskell using the University of Pennsylvania's online materials. I'm a few lessons in and was looking for some feedback about whether I'm thinking functionally enough or porting over my ...
Garry Cairns's user avatar
11 votes
1 answer
492 views

American Checkers

I am implementing the logic for a Checkers game in F#. I am writing my code in a library so it can be called from any UI provider, and am trying to do it in good FP style. I currently have the ...
user avatar
11 votes
2 answers
655 views

FizzBuzz in JavaScript using Ramda

Just getting up to speed in Ramda, I have implemented this: ...
Keith Nicholas's user avatar
10 votes
2 answers
636 views

Naive Bayes classifier

I just started learning Haskell, and I must say it is unlike anything else. As my first not-entirely-trivial piece of code I tried to write a simple Bayes Classifier. It is in two parts, a classifier ...
V9801's user avatar
  • 203
10 votes
2 answers
395 views

You can't sink me

Here is a Battlehips game in F#. Now, before you start jumping up and down on my code, please understand that, for all its awesomeness, F# has a serious limitation: Its compilation is linear, and the ...
user avatar
10 votes
1 answer
1k views

Generate iCalendar .ics files with events for astrological aspects

I'm relatively new to Python, coming from a deep C++ background. I'm mostly looking for feedback on how to make my code more idiomatic/pythonic, but I would welcome and appreciate any and all other ...
feuGene's user avatar
  • 363
10 votes
3 answers
688 views

JavaScript OOD: 2048

I wrote a 2048 game in JavaScript with an object-oriented paradigm. The game board is presented with a two-dimensional array and each tile holds an integer. Here is the implementation: ...
Joji's user avatar
  • 315
10 votes
4 answers
7k views

Split IEnumerable by predicate

In order to divide an IEnumerable in C# by a predicate, I implemented the following (in a static class): ...
Heman Gandhi's user avatar
8 votes
3 answers
1k views

Rolling dice in a method chain

I've been writing code for nearly 40 years now and am still not too old to learn and understand new things. Right now, my focus is a bit of OO with functional programming combined and C# with Linq is ...
Wim ten Brink's user avatar
8 votes
1 answer
880 views

"Curious Numbers" (HackerRank PE 34)

I was inspired by a certain recent question to try my hand at this challenge: Project Euler #34: Digit factorials \$19!\$ is a curious number, as \$1!+9!=1+362880=362881\$ is divisible by \$19\$...
Phrancis's user avatar
  • 20.4k
8 votes
3 answers
517 views

Story-based adventure with functions and relationships

This is a story-based adventure program I coded by myself (forgive the length). It took a long time but it was fun. It includes loading times, changes in relationships, relationship levels, storyline ...
notak's user avatar
  • 605
8 votes
1 answer
3k 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),...
rookie's user avatar
  • 1,233
8 votes
2 answers
585 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: <...
repeat's user avatar
  • 275
8 votes
2 answers
292 views

Compiler for a minimal LISP dialect to run on the Java Virtual Machine

As the title states, this is a compiler written in C with a ruby build script that translates a minimal LISP dialect and spits out an executable jar file. I designed this LISP dialect and named it ...
user avatar
7 votes
3 answers
26k 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 ...
overexchange's user avatar
  • 3,321
7 votes
0 answers
116 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 ...
Aaron M. Eshbach's user avatar
6 votes
1 answer
433 views

How can I make this F# more functional?

I am currently learning myself a little F# and have written the following code to practice. The code uses the Mono.Cecil library to inject some IL at the start of every method in each .net assembly ...
Mongus Pong's user avatar
6 votes
3 answers
865 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 ...
CodeYogi's user avatar
  • 5,087
6 votes
2 answers
124 views

Reformatting a structured string like "a-b-c|A-B-C" into "a-A|b-B|c-C"

The code below was inspired by this post in Code Review. Here is how it was first intended by its author: I have the following code that converts a string that looks like : aaa-bbb|ccc-ddd|eee-fff ...
cFreed's user avatar
  • 2,811
5 votes
1 answer
579 views

Quadratic Equation Solver v2.0 in Javascript using Functional Programming

I have posted this in continuation to my earlier post Quadratic equation solver in JavaScript. Tried to improve on the shortcomings last time and also changed the logic. It is still not covering all ...
Shubhendu Vaid's user avatar
5 votes
1 answer
607 views

Yet another Pascal triangle in Python written in functional programming pattern [closed]

Here is my implementation of a Pascal program, which is used to print an n-order Pascal triangle. This is actually an exercise I came up with to familiarize myself with Python's Functional Programming ...
nalzok's user avatar
  • 513
5 votes
2 answers
320 views

Change code to use functional style

I tried to solve a programming contest problem in Scala, but I have a feeling, that it could be done in a more functional way. I compared it with the solution written in imperative style and it is not ...
Zoltan Hamori's user avatar
5 votes
1 answer
107 views

Advent Of Code 2017 Day 4 (part 2) in Functional Programming (FP)

I wanted to practice functional programming (FP) without using any library but using vanilla JavaScript only. So I took a problem from Advent of Code (the 2nd part of Day 4) You can only access the ...
thadeuszlay's user avatar
  • 4,003
5 votes
1 answer
1k views

Function composition in C++

My aim was to create a function which behaves like the operator . in Haskell. Here is what I made: ...
Nestor's user avatar
  • 153
5 votes
2 answers
381 views

Project Euler 3: Getting the largest prime factor of a number

I'm looking for some general feedback on my solution to Project Euler challenge 3 The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? <...
Overly Excessive's user avatar
5 votes
1 answer
2k views

Battleship Game

Inspired by this question, I went on with the following version. My goal was to focus on pure functions, to avoid mutable variables and to be strictly functional. I have ignored the computer player ...
user avatar
4 votes
1 answer
106 views

Advent Of Code 2017 Day 4 (part 1) in Functional Programming (FP)

I wanted to practice functional programming (fp) without using any library but using vanilla JS only. So I took a problem from Advent of Code (the 1st part of Day 4): A new system policy has been ...
thadeuszlay's user avatar
  • 4,003
4 votes
2 answers
1k views

Check whether string of braces, brackets, and parentheses is balanced

The Task is taken from codewars: Write a function that takes a string of braces, and determines if the order of the braces is valid. It should return true if ...
thadeuszlay's user avatar
  • 4,003
4 votes
1 answer
161 views

Functools extension library

I wrote a utility library inspired by functools that adds some common operations on functions I frequently use in my projects. As usual I'd appreciate any feed back....
Richard Neumann's user avatar
4 votes
1 answer
130 views

Project Euler #1

I'm learning Clojure and solved Project Euler #1, I'd like to have my code reviewed on all aspects and have one specific question. This is also my first Functional Programming language, I have however ...
skiwi's user avatar
  • 10.7k
4 votes
2 answers
3k views

Download entire localStorage as file

I would like to download the entire contents from localStorage to a json file (in a "clear" formatting). I've tried this and it works perfectly. However, ...
Gibberish's user avatar
  • 109
4 votes
2 answers
172 views

Print the length of words as input as a histogram with horizontal bars

Input A list of words separated by any number of spaces. Output A horizontal ASCII art histogram, where the n-th line is composed by as many asterisks (*) as the ...
Caridorc's user avatar
  • 27.5k
4 votes
1 answer
317 views

Reporting the highest satisfied poker hand in F#

I'm learning F# by doing various small projects. One of them is a problem where the program reads Poker hands and rates them. It's Texas Hold'Em, so for each player it tries every five card selection ...
Grubl3r's user avatar
  • 205
4 votes
1 answer
542 views

Functional Html builder

This is my third attempt to create a easy to use html builder because I wasn't really satisfied with the first one that wasn't extendable at all and even the one using dynamics wasn't much better. It ...
t3chb0t's user avatar
  • 44.2k
4 votes
2 answers
788 views

Connect-Four winner-checking algorithm

I'm trying to practice some functional techniques. I haven't worked much in Haskell, so any language-specific tips would be very appreciated, but what I care for even more are general tips towards my ...
Scotty Jamison's user avatar