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
1
vote
1answer
28 views

Name for mconcat implementation for applicative monoid

I have implemented a function mcollect which is effectively mconcat only for applicative monoids: ...
2
votes
1answer
36 views

Re-write custom feature encoding function python

I have a question regarding code quality and best practices. My task - to write a feature encoding function that will encode categorical labels, boolean labels as one-hot encoding, timestamps for ...
3
votes
1answer
49 views

Longest Substring without Repeating Characters Problem - Kotlin Recursion

I am practicing coding questions on leetcode. I have been an OOP my whole career, and I am trying to wade into the dark abyss that is functional programming. So I am trying to do things purely ...
0
votes
0answers
36 views

Implementing Currying

I'm trying to learn functional programming (using Javascript), so I took a stab at implementing a curry function to properly grok it. Here's my code: Curry.js ...
1
vote
0answers
38 views

Write a wrapper to REST API with asyncio

I want understand how to correctly structure a functional asyncio-based program. The code below wraps two external APIs to provide the client a simple ...
2
votes
0answers
55 views

“Rock, Paper, Scissors +” in F#

During a discussion in a development chat, a user suggested to another (in the context of C# originally), UserA: Challenge: Create an implementation of RPS, and then show how it can be extended to ...
2
votes
0answers
128 views

generic implementation approaches for a recursive function

Let's take the famous fibonacci problem as an example but this is a generic problem. Also, taking scala as the language as it's rather feature-rich. I want to know which solution you'd prefer. we all ...
2
votes
1answer
56 views

Trying to convert imperetive code to functional -Haskell

I'm very new to Haskell and I was trying to implement a DFT which is very imperative into Haskell and I wanted to get a feedback. More specially how can I avoid so many helper functions and I avoid ...
1
vote
0answers
41 views

Javascript validation Class & Functions

I wrote javascript code for form validtion. The goals is to implement fast validation with data attributes on HTML input elements. I'd like to hear from you how i can improve the class or function. ...
1
vote
2answers
82 views

A function to scan user input as string

I know that multiple functions are already available. However, I thought of writing my own because I wanted to learn the logic (and also because I thought there wasn't enough confusion :P). Please ...
5
votes
1answer
80 views

Program for calculating and ranking scores

Would love feedback on a vanilla JS challenge I've recently completed. It is a method for calculating and ranking scores. If possible, I would love to make it better. Any suggestions welcome. ...
-2
votes
1answer
53 views

python - class not returning the expected result. I am using function and a class [closed]

I am trying to pass 2 values from a function to the class and get a result from the class. For example, if I send value "dog" I should get the result as "dog runs" as a returned result. Need help with ...
3
votes
0answers
30 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 ...
1
vote
1answer
41 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 "...
0
votes
1answer
42 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
43 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 ...
2
votes
0answers
80 views

LINQ extensions for sequence comparing

The desired behavior would be: ...
2
votes
1answer
69 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 ...
3
votes
0answers
69 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: ...
-1
votes
1answer
64 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 ...
7
votes
2answers
863 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 ...
2
votes
0answers
33 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
51 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 ...
10
votes
1answer
69 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'...
1
vote
0answers
82 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
327 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
745 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 ...
3
votes
0answers
24 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
97 views

Implement Sudoku using LINQ

WiKi and GitHub. Sudoku rules are being checked with the following tests: ...
3
votes
0answers
85 views

Simple queue on F#

...
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 ...
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
67 views

Paginate IEnumerable<T> sequence

I used the following to split sequence into batches: ...
7
votes
1answer
307 views

String Format object extension

I wrote an extension to create a string-representation by an object: ...
5
votes
1answer
204 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 ...
2
votes
0answers
27 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)...
3
votes
0answers
53 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 ...
4
votes
2answers
67 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)] …...
8
votes
2answers
296 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 ...
6
votes
3answers
165 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
303 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
83 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. ...
2
votes
1answer
61 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 ...
4
votes
1answer
107 views

Function composition in C++

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

Emulating the pipe operator in javascript in a readable way

In this article I found this quite elegant looking Elixir code: ...
5
votes
1answer
75 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 ...
1
vote
1answer
103 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 ...
1
vote
0answers
83 views

Building maps for Dgraph integration tests in Java 8

I want to refactor this in Java to reduce my code duplication. ...

1 2 3 4 5 18