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
0answers
54 views

Optimize Function in Python [closed]

I have the following function in Python that works properly. How can I improve the syntax so that it performs better or summarizes the number of lines? ...
4
votes
3answers
2k views

Return the shortest substring containing all the characters in the set

The task: Given a string and a set of characters, return the shortest substring containing all the characters in the set. For example, given the string "figehaeci" and the set of characters ...
2
votes
0answers
25 views

Functional Dependency Injection Abstraction

The F# web-development framework Giraffe supports a functional style of Dependency Injection using a form of the Reader monad, as explained in this GitBook. I would like to use this pattern more ...
2
votes
1answer
237 views

Longest Substring Without Repeating Characters in Scala

Question is taken from Leetcode and solution works for their test cases. I do see a scope of improvement and make it more functional (getting rid of vars and mutables data structures). Please suggest ...
1
vote
1answer
38 views

Clean up function in Javascript Event Listener

I am trying to refactor a chess game I am currently creating so the code is more flexible and maintainable, while doing this, I came across an event listener which I have no idea on how to make "...
2
votes
1answer
68 views

Assigning jobs in a functional style

I'm new to F# and functional programming in general (coming from an object oriented background). I just wanted to get some criticism on an algorithm for assigning jobs to crew members. I want to be as ...
0
votes
1answer
39 views

Functional vs Object Oriented Approach for UI codes

I have the following snippets. which formats error messages based on a map. I can do it as a simple function call the function for transforming the error. or create a class and handle it in that way. ...
2
votes
0answers
42 views

Immutable-Object-like Structure in PHP

I made a closure-based PHP structure that behaves mostly like a classical object. To change the object's state, one must derive a new copy by passing the altered state to the ...
7
votes
2answers
843 views

Simple converter: Is it better to use a function within a function?

Below is an example of a simple converter. In the first example temperature I have used a function within a function. In the second example ...
3
votes
0answers
65 views

Functional Programming approach to a REST API example

I would like to know if there are any things I could improve with the following code regarding writing it in a FP style: server.js: ...
2
votes
0answers
75 views

LINQ extensions for sequence comparing

The desired behavior would be: ...
-1
votes
1answer
54 views

Interlock of some equipments in electrical power system using simple Arduino Uno code [closed]

I'm trying to implement some logic functions essential to control a small power system consisting of 2 Transformers labeled T1 and T2, 2 Generators (G1 and G2) and a bus tie (BT). I'm not going to ...
10
votes
1answer
68 views

C-style `switch` statement implementation using decorators

Yes I know context managers are a much better way to do this, but I spent four hours working on implementing a switch construct using generic decorators, and I'd like to hear feedback on how I did. I'...
2
votes
0answers
27 views

Finite automaton library in Scala including subset construction and minimization

I built a scanner generator like flex or jflex in Scala for a class last year. Part of this project is a small library for handling and manipulating finite automata. It works completely as intended, ...
-1
votes
1answer
49 views

Functional filter unique elements in sequence is not as good as imperative [closed]

Can you please help me fulfill the following? Requirements: Create an iterable with unique elements. Their order should be preserved: a | b | a | c → a | b | c ...
4
votes
1answer
207 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 ...
1
vote
0answers
73 views

Cartesian product in racket (Is append-map okay?)

I've read several places that say it's really bad to use append, and I understand why. I read somewhere that it was never even correct to use ...
7
votes
2answers
325 views

Chaining statistics in a dice queue

I've previously came up with the question "Rolling dice in a method chain" and was interested if this could be improved. It could, so now I have this class: ...
8
votes
3answers
736 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 ...
4
votes
2answers
60 views

User defined function call on a stack based interpreter for concatenative language

I would like to receive some advice on how to improve a small concatenative stack-based interpreter, executing a joy-like programming language. It is really minimal, and is in fact a subset of a ...
3
votes
0answers
23 views

How would you convert this code to best take advantage of CSS transform for panning/zooming?

I am using react-flow-chart for a job project, but it doesn't implement zoom, and I would like to take the reigns and try applying it myself. I took a linear ...
12
votes
2answers
1k 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: ...
4
votes
0answers
96 views

Implement Sudoku using LINQ

WiKi and GitHub. Sudoku rules are being checked with the following tests: ...
6
votes
1answer
250 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 ...
1
vote
2answers
71 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 ...
3
votes
1answer
140 views

Naive JS implementation of tidyr's gather function

Tidyr is an R package for creating tidy data. (tl;dr) In short, it provides an interface (gather) for restructuring a table into a "tidy" format. For our purposes ...
1
vote
1answer
47 views

Group items by types: replace nested for loops with reduce

I have a working code that groups items by their type values. The types can be one or multiple, however they are always returned as an array. To illustrate: if an item has types with values ...
4
votes
1answer
66 views

Paginate IEnumerable<T> sequence

I used the following to split sequence into batches: ...
12
votes
3answers
4k 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 ...
5
votes
1answer
146 views

Timeout Error in Fraudulent Activity Notification HackerRank

I am solving this problem: Fraudulent Activity Notifications on HackerRank. I am done with my code and is working, but it is inefficient as well for very large inputs. I don't know but after all ...
7
votes
1answer
304 views

String Format object extension

I wrote an extension to create a string-representation by an object: ...
2
votes
0answers
26 views

Reorder items in a map

I'm attempting to create a Module, I've called it Timeline, that resembles a drag n' drop list. Given a Timeline, someone could continually add items (I'm not including removing or updating Items here)...
6
votes
1answer
264 views

Merge Intervals in JavaScript

This is a task taken from Leetcode - Given a collection of intervals, merge all overlapping intervals. Example 1: ...
6
votes
2answers
13k views

Palindrome test in Haskell

I am new to Haskell and, coming from a OO/procedural programming background, I want to make sure my code follows the functional way of doing things. I built this short module to test if a given string ...
2
votes
1answer
85 views

Given time intervals determine if a person could attend all meetings

The task is taken from LeetCode Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...
4
votes
2answers
65 views

Reducing a list of points into a list of pairs of points

I have some data… let points = [(718, 620), (4596, 1280), (410, 333), (4597, 993), (410, 337), (4597, 996), (428, 337), (4597, 1000), (431, 335), (4599, 1044)] …...
3
votes
0answers
51 views

Node Based Code Flow Builder

Background In my development of a game engine, I realized the need to be able to construct processing units in a serializable way (code below does not show serialization, just the wiring). I broke ...
10
votes
2answers
6k views

Reading input from console in F# (as a sequence of lines)

Consuming input line-by-line until the end is often useful. In C# I would write the following loop: while ((line = Console.ReadLine()) != null) { // ... I ...
8
votes
2answers
243 views

c++17 compatible std::bind_front alternative

One of the things that has been really exciting me in c++20 is std::bind_front. Using placeholders with std::bind and ...
1
vote
1answer
39 views

Join a JSON object of arrays and reduce down to one key [closed]

Given that I have a dataset as follows: { 1: ['Extremely Tall'], 2: ['Tall'], 3: ['Medium', 'Low'], 4: ['Dwarf'] } and I receive a value (e.g. Tall or Medium) I ...
8
votes
1answer
169 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 ...
6
votes
3answers
164 views

Generic method to call API functions with simple retrial on errors

I have to develop a small security management application that relies on a Web API. Based on that API, I have generated the REST client classes (Open API generator), but I still have to code on top to ...
5
votes
1answer
302 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
82 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
78 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
326 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
60 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
57 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 ...
2
votes
1answer
54 views

Emulating the pipe operator in javascript in a readable way

In this article I found this quite elegant looking Elixir code: ...