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
2
votes
0answers
12 views

Make utility using LINQ

Make() takes projects with a list of tuples representing project references in the form of (Dependency, Dependent) and returns ...
2
votes
0answers
73 views

Kaleidoscope Effect App

I've written a simple app that draws with a kaleidoscope effect in javascript, could someone please review the code? I'm especially interested in functional practices and design patterns. ...
1
vote
0answers
75 views

Building maps for Dgraph integration tests in Java 8

I want to refactor this in Java to reduce my code duplication. ...
2
votes
1answer
305 views

Grouping models without dictionary

I want to group an array of structs only using arrays into an array of new structs. In this example I want to group the Person structs by ...
2
votes
1answer
53 views

Functional n-queens

Following the book "Structure and interpretation of computer programs" I have tried to implement a functional solution to the problem of N-queens (implemented by the function ...
3
votes
2answers
51 views

Loading items for an eCommerce site

I am attempting to make an eCommerce site from scratch. Right now I have the following code that gets all the items from a Firebase Firestore database and then adds HTML into an items div based on the ...
1
vote
1answer
41 views

Emulating the pipe operator in javascript in a readable way

In this article I found this quite elegant looking Elixir code: ...
4
votes
1answer
88 views

Function composition in C++

My aim was to create a function which behaves like the operator . in Haskell. Here is what I made: ...
1
vote
1answer
74 views

Constructor function for persons with hobbies

I'm trying to write a constructor function that generates instances of a user. One of the properties of the user class is an array of the user's hobbies. I'd like ...
3
votes
1answer
61 views

“Real world” JSON transformation using Node.js

Background I transformed a non-trivial JSON response that uses arrays into a nested object eliminating some of the original properties inserting a new summary property that is a unique list of ...
4
votes
2answers
69 views

RSS feed viewer in Clojure

This semester a classmate and I visited a course on functional programming in our university. For grading, we've to submit a small project which should be written in Clojure and make use of functional ...
4
votes
1answer
62 views

Finding optimal mapping patterns that transform relational data into a linear format

I'm trying to upgrade my javascript to be more functional. I have used function composition and curry successful on less complicated code but i'm now running into a point where i'm not sure on how to ...
2
votes
3answers
86 views
3
votes
2answers
2k views

Is there a better way to write these two nested loops in Erlang?

Since one month, I'm learning Erlang and I like it so much. Today, I wrote an algorithm to solve a problem. Some time ago, I used two nested for loops in C# to ...
6
votes
0answers
316 views

A* Algorithm in F#

Inspired by this post I looked up A* on wikipedia and went on with my own implementation as seen below where I try to mimic the pseudocode on Wikipedia but in a recursive manner. I would like any ...
4
votes
1answer
110 views

Lock-free Immutable ConcurrentQueue

Similar to the code review I posted last week for an agent-based immutable replacement for ConcurrentDictionary, I have also created an agent-based immutable ...
4
votes
1answer
69 views

Binary Trie in F#

I have this little exercise writing a binary trie in F#. In C# it can be done in few lines, but here it became more complicated than I had expected (especially the ...
1
vote
2answers
90 views

Functional / Declarative FizzBuzz

I spent some time today trying to write FizzBuzz in a functional/declarative style. I thought it would be a good chance to get some feedback on it since I've been doing functional programming for ...
15
votes
2answers
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 ...
12
votes
2answers
232 views

Emulation of SE's text input control for tags

Introduction I used React to write a component, which is meant to be a faithful emulation of the input for "Tags" which you see at the top of every question/topic on SE. Why this project? If you're ...
2
votes
1answer
56 views

Toggling the background color of a page using JavaScript closures

This question is to see if I can get some input on the "design pattern" I tried to implement here. I'm just learning about closure in JavaScript and I think I'm starting to get it. I'm wondering if ...
0
votes
1answer
49 views

Provide functional-programming style solution for 2D Array - DS challenge

Problem Statement: Given a 6×6 2D Array, arr: 1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 We define an hourglass in A to be ...
3
votes
1answer
97 views

React.js Nested Nav Bar

This was written for a coding challenge for a company I recently starting working for. I'm looking for any suggestions on how to clean up the code, as well as any issues anyone thinks may occur as it ...
8
votes
2answers
93 views

Function invocation every second time

Recently I got test task at the project, it is fairly simple: ...
6
votes
1answer
98 views

Given a string containing just parentheses, determine if the input string is valid

The task is taken from leetcode Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: ...
3
votes
2answers
81 views

Count character 'a' in first n characters of indefinitely repeating string s

Problem statement: Lilah has a string, s, of lowercase English letters that she repeated infinitely many times. Given an integer, n, find and print the number of letter a's in the first n ...
4
votes
2answers
84 views

Counting valleys traversed below sea level, given elevation changes

Problem Statement: Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly steps. For every step ...
4
votes
3answers
98 views

Functional CSV Parser

I've been starting to migrate some C++ code from imperative to more of a functional style. I'm new to functional programming so I'm trying to get a sense of how well this code fits into the functional ...
4
votes
1answer
63 views

Currying in Lua

Just whipped up this quick curry function in Lua, as a building block for partial application and memoization. Any feedback is appreciated. It takes two arguments; ...
3
votes
1answer
74 views

Pre-calculate the winning combinations for an n-sized tic tac toe board using C++ functional programming

This code pre-calculates the winning combinations for an n-sized tic tac toe board. I first created my function using an imperative approach. This is just how I naturally write code most of the time....
2
votes
1answer
60 views

JSON Parser, pulls data from a JSON file for analysis

This is the first time I have programmed something that I will actually personally use, so I am happy about that. I tried to write good code whilst applying functional programming concepts. So no ...
-2
votes
1answer
31 views

A game that deals a high card to one of two players [closed]

So for my class, I have to design a game that will deal out one card to for each player. I also need to use functions to accomplish this one for the high card game, and another for the face value of ...
8
votes
3answers
388 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 ...
6
votes
1answer
226 views

Voting app in JavaScript using Ramda

I'm just learning functional programming in JavaScript using Ramda. I took this tutorial and implemented the server core.js component using Ramda. The idea is that we'll have a collection of ...
2
votes
1answer
100 views

Manipulating arrays to extract unique objects and count occurrences

The data describes cycling activities (key1) and chunks of them (key2). In total there are around 1,000 ...
2
votes
0answers
46 views

A tiny library implementing the Elm Architecture in C++

Elm is a pure functional language for the front-end. It enforces an architecture that allows programs to stay pure in an event-based setting. This simple header-only library implements a variant of ...
0
votes
2answers
64 views

Count Substrings for a binary string

Problem is taken from leet code. Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings ...
8
votes
1answer
146 views

Lazy, Functional Parser Combinators

Parser Combinators are the amazingly elegant way to write parsers that has evolved over on the functional programming side of the world, but have been less available or accessible for imperative ...
5
votes
1answer
35 views

Typescript monad for data being loaded

I have been struggling to make typescript happy about the types. I am pulling data from our backend API and I wanted to give context to the data. Basically it is a monad with 4 shapes: Initial (...
4
votes
2answers
62 views

Grab the first 4 items that meet the condition from a data set with an unknown size

I was turned down for a job after a coding take-home test, and some of the feedback I received was: would have preferred to see more functional programming instead of while loops - and the while ...
2
votes
1answer
79 views

Find all pairs of unique indices in a list such that the concatenation of the two words is a palindrome

The task Given a list of words, find all pairs of unique indices such that the concatenation of the two words is a palindrome. For example, given the list ...
2
votes
2answers
42 views

Seeking the seeds for writing tidy event handlers in JS

I'm seeking a critique of the simple program below, which displays two counters and increments them through a button, using D3.js. In real life of course no one has a need for displaying buttons that ...
4
votes
1answer
50 views

Print a Connect 4 grid based on some rules for each cell

In a game of Connect4: we start with an empty grid two players place pieces X and O on the grid the first player to achieve 4 pieces in a line wins! this is a text based console game Here is the ...
0
votes
0answers
26 views

Create a template literal tag to remove whitespace from a string

I'm just getting into functional programming (as in, just started reading the book) and as practice I'm trying to refactor a past answer I wrote to fit in line with the functional style. The ...
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? ...
4
votes
1answer
91 views

Return whether a string is a palindrome if you can delete at most k elements

The task: Given a string which we can delete at most k, return whether you can make a palindrome. For example, given 'waterrfetawx' and a k of 2, you could delete f and x to get '...
3
votes
0answers
80 views

Async in Railway Oriented Programming in F#

In F# the Railway Oriented Programming pattern can be implemented synchronously using the Result<'t,'terr> type as result value for functions, so they can be ...
1
vote
1answer
28 views

Two-Part Hurdle Model Simulation with Bootstrapping

I am running a simulation study where I am using two-part hurdle modeling on different effect sizes. I tried to stream-line my code for the best optimization including foreach loops and ...
-1
votes
1answer
43 views

Perform KS-test on Random Number Generator [closed]

I've just started practicing Functional Programming in Scala in some days. This is the code I used to perform KS-test on a random number generator. ...
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 ...