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
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
75 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
492 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
62 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
631 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
82 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
135 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
303 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
260 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
243 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
156 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
355 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
59 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
79 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
332 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
684 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
222 views
1
vote
1answer
800 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
569 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 ...
3
votes
1answer
68 views

Setting new values in array based on query results

I have some code which uses nested foreach loops to take the results of a query and drill down into the data, reassigning into a main data array at each level of the foreach nesting. The problem is ...
3
votes
3answers
756 views

Readability of array_* vs foreach in PHP for filtering and extracting

With functional programming all the rage, I feel I should be using PHP's array_* functions more. But when I compare these two identical implementations, I can't ...
3
votes
1answer
213 views

Using Ramda in point-free way to transform data into new format

Recently I asked on SO about using point free methods to rearrange some data The idea was to turn data in this format: ...
1
vote
0answers
305 views

Fetching weather data from API

I was experimenting with some code to fetch the current weather data as well as getting the hourly forecast. I could have done ...
0
votes
2answers
63 views

Updating persons in database with newly created addresses [closed]

I have the following code, and I'd like to refactor it to a more functional way: ...
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 ...
7
votes
3answers
257 views

Builder/Wizard API for immutable objects

There are a lot of parameters I would like to configure for my Analysis object, so having some kind of composition helpers is a big thing to simplify proper use of ...
4
votes
1answer
115 views

List replication problem from Hacker rank

Problem: Given a list, repeat each element in the list n amount of times. Solution: ...
3
votes
1answer
314 views

Lazy, functional streams library for Node.js

I’ve written a small library that implements lazy streams in (ES2016) JavaScript for use in Node.js. The library could work in the browser as well with some minor modifications, but that isn’t ...
4
votes
2answers
88 views

Find the minimum number of operations (concat, add and subtract) for the numbers 1-9 to equal 100

I am beginning to learn Scala and functional programming, and I came across this puzzle. This looked like good practice and I have a solution but just want some hints in terms of functional approach ...
1
vote
0answers
22 views

Retrieve compilation files of smart contracts asynchronously via Ajax and add to database

I need to retrieve two files that are labeled :abi and :bin from a server. I then want to add these files to a database in a ...
2
votes
0answers
29 views

Test Driven Development in JS and Sinon Stub

I am going through some TDD and BDD tutorials which guide one through implementing a simple beverage-ordering system. While I think I understand the material, I am still wondering about some ...
0
votes
1answer
70 views

Making a dynamic query based on a cookie change

I have a PHP function which sets/unsets something (category and tags) in the cookie and then makes a dynamic query based on it: ...
3
votes
0answers
76 views

Functional Merge Sort in Python with only boolean variables to control fall off logic

Merge Sort I have just implemented this merge_sort, and this is also my first algorithm implemented in Python, I am totally open to feedback about how to better use python. I am using Arrays instead ...
-1
votes
2answers
167 views

Sanitize object's values in idiomatic JavaScript way [closed]

Oftentimes my JavaScript/TypeScript code becomes riddled with annoying code flow structures like below. Is there a way to do concise [functional] one-liners without being messy? Input: ...
3
votes
0answers
121 views

React.JS functional paradigm

I have some (minimal) experience with the functional programming paradigm and I'm new to React.js and reactive programming. I'm trying to get a flavour for the React.js philosophy and I keep seeing ...
1
vote
1answer
295 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 ...
4
votes
2answers
62 views

Implementation of undoable arrange (SendToBack, SendBackward, …) operations

I am developing a WPF designer where visual elements (rectangles, ellipses, ...) can be inserted, moved, resized and so on. Similar to a simple PowerPoint control. All the actions should be undo-able,...
1
vote
0answers
87 views

Purely functional minecraft-like mesh compiler

I'm creating a minecraft clone (for practice), in scala, using largely functional programming. When a chunk doesn't have a mesh loaded into VRAM, it create a Future for the vertex and index arrays, ...
1
vote
1answer
108 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 ...
6
votes
1answer
471 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 ...
1
vote
2answers
114 views

Find the most frequent character in a string

I'm trying to solve this problem https://community.topcoder.com/stat?c=problem_statement&pm=40 Given a string made up of ONLY letters and digits, determine which character is repeated the ...
5
votes
1answer
100 views

Drawing a regression line with a timeseries

My code is using seaborn to draw a regression line with a timeseries data as the x axis, and whatever you want the y-axis to be. Since seaborn does not support this function directly, a dummy column ...
-3
votes
4answers
699 views

Dependency injection using function references in C# [closed]

I understand that following the dependency injection principle, a class should ask for abstractions in the constructor and should not create them itself. This way they are not dependent on concrete ...
2
votes
1answer
85 views

Using partial application to compose functions

I have an application that will provide notifications to clients. It has two configurations: Default: A single web api instance that notifies the appropriate subscribers Alternative: Under high load,...
4
votes
1answer
925 views

Monad Implementation in Typescript

Having been introduced to monads through Scala, I am trying to add the generalized design pattern to my toolkit by implementing it in a language I use more regularly (typescript). My tests pass (...
2
votes
1answer
542 views

Implement array element swap in a functional language

Sorting algorithms involve swapping element positions in an array. This is my implementation of swap: ...