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
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 ...
1
vote
0answers
45 views

Creating and manipulating a map for a text based game F#

I have put my whole code so far below, but what I am most interested in is how best to implement the map and utilise it. Any tips on organising my code and keeping it clean would also be greatly ...
1
vote
1answer
304 views

Vert.x application quickstart

I have created this repository that implements this OAuth2 flow. I wanted to use Java and create a minimal implementation of this flow with these goals in mind: Develop unit and integration tests in ...
1
vote
2answers
118 views

Reducing array to average readings

Taking the payload below, I need to output the average of each reading. For example the readings for key 2 are 1, 4, 7. The average is (1+4+7)/3 = 4. The data is formatted in multi-dimensional ...
6
votes
1answer
2k views

“Mars Rover” challenge

I have written the solution of the Mars Rover problem in JavaScript, with a sort of functional approach. I would like to know how I can improve it and if there are any additional edge cases I missed ...
0
votes
1answer
6k views

Getting the grandparent directory of the current code [closed]

I have this example: from os.path import dirname, realpath dirname(dirname(dirname(realpath(__file__))) And the chained ...
1
vote
0answers
47 views

Code which buffers objects and schedules saving them from buffers, after applied some actions to it

Originally i have a function like this: ...
1
vote
1answer
306 views

Take a number and sum its digits raised to the consecutive powers

I am new to Scala have to solve this problem: If you have number "xy" should equal sum of consecutive powers of its digits xy = x^1 + y^2 For example, take the number 89 and you should ...
1
vote
0answers
51 views

Custom indexeddb modules while trying to maintain the functional paradigm as much as possible

I'm a Web Development student, and I just started learning indexeddb a little while ago. At present my teacher hasn't taught us Promises, and I'm not really sure he plans to. So, I took the initiative ...
3
votes
1answer
205 views

Sorting a list of phone numbers by teleconference provider and location

I'm looking to order an array of phone numbers with a primary sorting criteria of whether the teleconference provider is correct and a secondary criteria of whether the the location of the phone ...
5
votes
3answers
68 views

Aggregate summary counts for an array of error objects

I'm doing some refactoring of someone else's code, and just want a second opinion, because of course I think my work makes it better, but some validation (or correction) would be helpful. Starting ...
0
votes
3answers
323 views

Binary search algorithm in Clojure

I'm absolutely new to Clojure (started learning it yesterday). After I went through some tutorials and checked out the basics I implemented the following Binary search algorithm: ...
1
vote
1answer
67 views

Functional Implementation of Co-prime pairs in array

Program finds the number of pairs of co-primes in an array using functional concepts in Node.js ...
5
votes
2answers
206 views

General FizzBuzz in Scala

I wanted to create a FizzBuzz implementation that both showcases the strengths of Scala (Functional paradigm and collections) and is general enough to anticipate the typical interview follow-up ...
3
votes
1answer
176 views

Find an unknown length using sohcahtoa (Trigonometry)

Here is a calculator for an unknown side in a right-angle. Is there a more efficient way of doing this? ...
8
votes
3answers
838 views

Partial application in C++

Here is my code: ...
0
votes
2answers
141 views

Reduce a JavaScript array into named objects

The code below reduces an array into "named" objects (not sure if that's the correct terminology!) It works, but I'm sure the code could be improved. There is some repetition going on in the reduce. ...
1
vote
0answers
95 views

Folktale 2 skeleton workflow for resetting a forgotten password

I am learning more about functional programming via loop recur's online book Mostly Adequate Guide to Functional Programming, and Brian Londorf's video series on egg head. I started using ...
1
vote
2answers
87 views
4
votes
1answer
45 views

Implement clojure-a-like comp() function in python3

With a friend some time ago we wanted to write our own implementation of comp() clojure function in python3. we ended up writing the following function: ...
2
votes
1answer
340 views

First-class objects in Python

As a beginner, in order to cement my knowledge of functions in Python, I’ve prepared this Jupyter notebook First-Class Functions. The code in it is working, but before moving on to closures and ...
2
votes
1answer
82 views

Project Euler 23 in Scala (non-abundant sums)

I recently overhauled my solution to Project Euler Problem 23 (which asks for the sum of all positive integers which cannot be written as the sum of two abundant numbers) to move it from an imperative ...
1
vote
2answers
64 views

Getting a string from a range of possible paths

I have a scenario where I need to grab the first occurrence of a string from an object, but only if the match occurs in one of the path's that have been pre-defined. ...
0
votes
1answer
63 views

Report a status based on a list of results

In one of my services, I have the function as presented below: ...
4
votes
0answers
84 views

Simple wrapper function grouping and summarising variable

Background I've wrote a trivial wrapper function summarising variable by group for dplyr. I would like to handle quosures more efficiently. In particular, I've a hunch that ...
2
votes
0answers
86 views

React - Higher Order Component

My first go at a Higher Order Component in react. Aims to give a component the ability to call an url repeatedly with a given refresh interval. Seems to be working as I want. I'm not 100% confident ...
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 ...
0
votes
1answer
84 views

Promisifying a third-party JS library with minimal code overhead

Context and Existing Code I am trying to "promisify" a third-party authentication library (auth0-js) written in JavaScript. Right now it uses callback functions and there are no plans to expose an <...
3
votes
3answers
586 views

Array flatten with recursion

I'd been using this form for a while now with the functional methods map, filter, reduce: ...
2
votes
1answer
63 views

Clean up string and move the noun before the comma

I would like to know how I could improve the regex part of this script? I now chain 4 replace methods which is not optimal. Any ideas of other improvement I'm happy to receive. ...
7
votes
1answer
706 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 <...
-1
votes
2answers
83 views

javascript reduce list of a multi attribute list of objects on one key but returning more than one attribute

Is there a better way to do this? I have a list of objects. I want the list has two values: an ID and a NAME. Both Name & ID are distinct taken separately of together. ...
3
votes
1answer
136 views

Grab first successful pair from a list of maps

I have a function that parses geographic coordinates through a list of different providers. In this case I want to look up two addresses. The main problem of this question is - given a List of of ...
3
votes
2answers
334 views

Splice from Array if index matches

This code loops through an array called $rootScope.watchlist (uses AngularJS). It gets the current iteration of the loop to access that x iteration through ...
2
votes
1answer
60 views

Building objects in javascript, without “if(!a[k]) a[k] = []”

When building objects using reduce, I often have crappy code like this: ...
1
vote
1answer
261 views

Control class for an entity

For a Student object in the package entity, I have a control.studentpackage with classes ...
6
votes
2answers
622 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 ...
7
votes
1answer
253 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 ...
2
votes
0answers
169 views

Groovy Map/Reduce for Jenkins DSL

Jenkins DSL doesn't support collect and inject from what I can tell (I get missing method exceptions when I try), so I ...
4
votes
3answers
379 views

Counting votes for candidates in a category and region

With the following code, I want to count the Vote objects from a collection, filtering on one of their fields, Candidate. I have the feeling that I can get this done much more efficiently. ...
3
votes
2answers
63 views

Naming a not-quite-flatMap method

I recently created Argyle, a small library for parsing command-line arguments in Scala. I'd be happy for anyone to take a look and provide general feedback on it. But there is also one specific issue ...
0
votes
2answers
84 views

Extract value from scala collect case

I have the following code which reads data from a key/value data storage. At some stage I want to go through everything in the storage and check expiry times and remove values - as done in the code ...
4
votes
3answers
102 views

Compare ZIP codes, disregarding leading zeros

I have a function that compares ZIP codes. It also has to handle cases where the input ZIP code be missing leading zeros, have extraneous leading zeros. This is what I've come up with. I don't have ...
3
votes
1answer
372 views

Recursively find TypeScript files imported/exported from an entry point

I'd like to know what's the best practice as functional programming in JavaScript to work with arrays and passing them as an argument to a recursive function that may run itself within its branch. In ...
3
votes
1answer
696 views

JS calculator module using functional programming

All of the code is self-explanatory and pretty much complete. What the code does: Calculates using onscreen numerics and operators Validates any erroneous operations, such as 4 ** 8 or *9 Good ...
7
votes
1answer
238 views
1
vote
1answer
921 views

Conditionally applying and removing css based on a table row clicked

I've a added a click event to each row on a table element. Which I then use the $this context from the event to add some css styling to that clicked row. The behavior is as follows: ...
6
votes
1answer
1k views

Railway Oriented Programming (ROP) in C#

I am not sure if there are good ROP implementations already existing in the open because I couldn't find any, actually I found one by odytrice (https://github.com/odytrice/Operation), but I think the ...
3
votes
1answer
606 views

Summing the digits of a number (subtracting the first digit if it is negative)

I'm working on some practice problems using higher- order functions and while I was able to solve this problem. I can't help but think this code is ugly and not the most eloquent it could be. Is there ...