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 programme state. In this style of programming, side effects and mutable data are deprecated and usually strictly isolated.
746 questions
7
votes
2
answers
2k
views
Functional Programming best practices
I don't have a particular coding problem on hand this is just an excercise to improve my thought process.
Few months back I started learning about functional programming( mostly in R) and I fell in ...
2
votes
1
answer
155
views
Dynamic *dll substitution?
I got an architectural problem here.
Let say there is an IShell. It mainly responsible to map user's commands (represented as a linux-like strings) to an appropriate IExecutable's.
Those ...
50
votes
9
answers
14k
views
Return considered harmful? Can code be functional without it?
OK, so the title is a little clickbaity but seriously I've been on a tell, don't ask (TDA) kick for a while. I like how it encourages methods to be used as messages in true object-oriented fashion. ...
5
votes
1
answer
898
views
Is using Option#get really a bad idea here?
I'm working on a Scala project and Wartremover shows an error about Option#get usage in my code:
Option#get is disabled - use Option#fold instead
While I do understand how get should often be avoided,...
3
votes
1
answer
433
views
Dependencies between functions-only modules: hardcoding vs alternatives
In switching from a procedural background to "FP in the small, OO in the large" I'm grappling with the following problem. Suppose there're modules, each only containing numerical math functions ...
6
votes
3
answers
420
views
Can referential transparency be assumed when dealing with floating-point arithmetic?
A pure function is assumed to produce the same outputs given the same inputs. Suppose an (otherwise) side-effects free function computes with floating-point numbers. Due to numerical error, these ...
5
votes
1
answer
1k
views
Should function composition and piping be tested?
Unit testing requires writing tests first then code, on the other hand in F# (and most of the functional languages) some codes are extremely short as follows:
let f = getNames
>> Observable....
4
votes
4
answers
503
views
Why are functional-style chained map operations considered hard to read?
In the comments to an answer of another question, I proposed the following Java code as a better way of writing a more procedural-style variant of the same operation:
employeeService.getEmployee()
...
1
vote
1
answer
1k
views
Does dependency injection fly in the face of functional programming?
I have the following pure function (f2 and f3 are pure too):
class X {
def f1(arg: Type1): Type2 = {
val x = f2(arg)
val y = f3(x)
y
}
def f2...
def f3...
}
Now, I would like ...
10
votes
1
answer
817
views
Why most anonymous functions can't be JIT compiled and will never be optimized?
I just read https://techfindings.one/archives/2652 about functional programming and came accross this:
anonymous functions can often not be JIT compiled and will never be
optimized
Can someone ...
1
vote
1
answer
211
views
What does it mean "state complects value and time"?
I'm rewatching Rich Hickey great talk "Simple Made Easy"
And around min 35:40 when talking about state, mentions that State complects value and time, but I'm not sure I'm understanding this ...
1
vote
0
answers
87
views
How Functional Programming addresses concurrent increment/decrement operations invoked by different users? [duplicate]
Using Functional language, How can 2 different parties achieve the result of increment/decrement operations concurrently?
For the below scenario, Let's say, I've 2 quantities in stock and 2 users in ...
1
vote
2
answers
921
views
How Functional Programming addresses concurrent increment/decrement operations invoked by different users?
Using Functional language, How can 2 different parties achieve the result of increment/decrement operations concurrently?
For the below scenario,
Let's say, I've 2 quantities in stock and 2 users in ...
37
votes
7
answers
9k
views
What are the functional equivalents of imperative break statements and other loop checks?
Let's say, I've the below logic. How to write that in Functional Programming?
public int doSomeCalc(int[] array)
{
int answer = 0;
if(array!=null)
{
for(...
2
votes
3
answers
969
views
Can functional programming used for solving problems which require randomness?
This older question tells us that in functional programming "true" randomness cannot be achieved since in FP functions are pure/idempotent and return the same value irrespective of number of ...
5
votes
2
answers
772
views
Has any language enforced Command–query separation?
As described here, a function can be said to be a query when it returns a value, and a command when it modifies a value. It also states that a function should not be both.
That a query should not be ...
0
votes
1
answer
543
views
java.util.function.Function as an interface for an mutable object
I have an interface with only a single method that could be replaced by java.util.function.Function from the JDK:
interface DataModel {
boolean apply(Map<String, Integer> input);
}
The ...
-2
votes
2
answers
2k
views
What does it mean to "wrap" a stored procedure?
My manager has asked me to "just wrap the stored procedure".
The requirements are to create a microservice that will do the following:
Accept parameters from the user.
Pass those parameters to the ...
2
votes
2
answers
797
views
What's the proper way to think about state monads?
I've been using a functional approach in my programming of late. I know that mutability and state changes are big no nos within the paradigm. I now find myself working with databases and other data ...
7
votes
1
answer
5k
views
Is there a better term for "functional method chaining"?
I'm writing a C# style guide for my team and I'm trying to describe the benefits of side-effect-free functional-style methods. I want to include online references to back up the suggestions, but I can'...
3
votes
5
answers
562
views
Is there any benefit to minimal functional style?
Playing with some 3d game stuff in my spare time in C++.
C++ doesn't really lend itself well to fast, simple functional programming, and games where there is a lot of necessarily shared state don't ...
3
votes
1
answer
257
views
Making mutable classes immutable
When trying to turn a class with mutable state into an immutable one I am regularly having a hard time choosing between two alternatives: I can 1)
either extract the state into another (immutable) ...
0
votes
1
answer
225
views
Short circuit operator on both sides. Any prior art?
I just debugged a problem which proved to be that "and" evaluated both arguments before checking whether either was false. That's fine, not all languages handle x = (will_return_true() || loop_forever(...
0
votes
2
answers
2k
views
Multiple layers of abstraction and chain calls of methods (Java functional like programming)
I am creating a multiple wrappers/layers API in Java that go like this
public class Layer1<T extends Layer1>
public class Layer2<T extends Layer2> extends Layer1<Layer2>
public ...
44
votes
2
answers
11k
views
Why are lists the data structure of choice in functional languages?
Most functional languages use linked lists as their primary immutable data structure. Why lists, and not e.g. trees? Trees can also reuse paths, and even model lists.
2
votes
4
answers
344
views
Transforming one array to another: a for loop or a functional map?
Aesthetically and mathematically a map(f, X) function seems great: in just two arguments it tells what is going to happen: we are going to use function f to transform each element from X to create a ...
3
votes
2
answers
309
views
Should I provide partialed functions based on API call in Clojure?
I am designing a library to wrap an API with Clojure. The API requires user credentials to authenticate user related calls.
My first approach was to have functions that do each task the API can do:
(...
12
votes
1
answer
577
views
What are some intuitions that support calling the Maybe constructor in Haskell "Just"?
The intuition of an optional type like Maybe Int is that either there is no Int (thus, there's Nothing there) or that there is some Int; there is something there.
It makes sense to me that we call the ...
1
vote
2
answers
452
views
How is it obvious that that (foldr Cons Nil) just copies a list?
I am currently reading Why Functional Programming Matters by John Hughes.
In the "Gluing Functions Together" section, after having explained that (foldr f a) is a function that replaces all ...
1
vote
1
answer
127
views
Simplified API one case class vs robust and multi ADT case class? [duplicate]
Below are oversimplified examples, but I wonder which route to take what is the best practice when designing API, the simple or the more complex and robust which is better?
This looks good and goes ...
0
votes
3
answers
253
views
Use case, design, and how to append to an immutable object in functional programming
This is not (supposed to be) an opinion question, but it is a newbie question, so if there is just a resource I haven't found that I need to read, point me there :)
I am in the design stages of a ...
13
votes
2
answers
3k
views
Do compilers like Javac automatically detect pure functions and parallelize them?
Pure functions are known to facilitate parellelizing.
What is it about functional programming that makes it inherently adapted to parallel execution?
Are compilers such as Javac smart enough to ...
35
votes
5
answers
6k
views
Why are discriminate unions associated with functional programming?
In many years of OO programming I've understood what discriminated unions are, but I never really missed them. I've recently been doing some functional programming in C# and now I find I keep wishing ...
17
votes
4
answers
3k
views
When programming in Functional style, do you have a single application state that you weave through the application logic?
How do I construct a system that has all of the following:
Using pure functions with immutable objects.
Only pass into a function data that the function it needs, no more (i.e. no big application ...
12
votes
1
answer
13k
views
Why is unit testing harder in Object Oriented programming compared to Functional programming?
I'm going through this series. The author mentions that since state is maintained in object oriented programming, it is harder to write unit tests. He also says that since functional programming doesn'...
2
votes
2
answers
546
views
Automatically convert mutations in imperative code into functional actions?
I wonder if somebody tried creating a language where you can write mutations in a straightforward imperative style, and the compiler transforms the changes automatically to pure, Redux-style actions. ...
55
votes
11
answers
12k
views
"Easy to reason about" - what does that mean? [closed]
I have heard a lot of times when other developers use that phrase to "advertise" some patterns or developing best practices. Most of the time this phrase is used when you are talking about benefits of ...
1
vote
3
answers
3k
views
Why is white box testing discouraged in OOP?
It seems the general consensus for unit testing classes is to test your object through its public interface only. So if you wanted to test the removeElement method on a LinkedList class you'd need to ...
1
vote
1
answer
428
views
Modelling relational database entities in a functional language
I'm working on a Scala project that uses DynamoDB for persistence, and does this by modelling the records as case classes.
This is becoming increasingly more relational, which means we have classes ...
137
votes
5
answers
38k
views
Am I too 'clever' to be readable by Jr. devs? Too much functional programming in my JS? [closed]
I'm a Sr. front-end dev, coding in Babel ES6. Part of our app makes an API call, and based on the data model we get back from the API call, certain forms need to be filled out.
Those forms are ...
12
votes
2
answers
3k
views
Can we really use immutability in OOP without losing all key OOP features?
I see the benefits of making objects in my program immutable. When I am really deeply thinking about a good design for my application I often naturally arrive at many of my objects being immutable. It ...
0
votes
1
answer
227
views
Memoization in case of recursive interdefined functions in Haskell/Functional Programming?
I was reading Memoization with recursion which tells how for a recursively defined function fun we can do memoization by:
-- Memoization
memoize f = (map f [0 ..] !!)
-- Base cases
g f 0 = 0
g f 1 = ...
2
votes
2
answers
439
views
Strategy for implementing Multiple Dispatch
This is a question regarding how Multiple Dispatch works.
Suppose that we have a type hierarchy like this:
Drawable -> Shape -> Polygon -> Rectangle
And there are three functions (This is ...
11
votes
6
answers
4k
views
How would this be programmed in non-OO? [closed]
Reading through a scathing article on the downsides of OOP in favour of some other paradigm I've run into an example that I can't find too much fault with.
I want to be open to the author's ...
7
votes
3
answers
1k
views
How is arrow operator an Applicative Functor in Haskell?
Note: Still learning Haskell, not reached Monoids, studying Applicative Functors.
I saw this implementation and I am not entirely clear on it:
instance Applicative ((->) r) where
pure x = (\...
4
votes
1
answer
784
views
functional programming with mutating request object
Following uses JavaScript code but F# is tagged for input from functional programmers.
I understand that in functional programming we shouldn't mutate the state or incoming parameters but I'm ...
6
votes
1
answer
2k
views
When should I create my own @FunctionalInterface rather than reuse the interfaces defined in java.util.function?
The functional interfaces in java.util.function cover the vast majority of common strategies one might want to apply, but it's also possible to define your own @FunctionalInterface instead. In cases ...
19
votes
3
answers
3k
views
How do the type systems in functional languages differ from those in OO languages? [duplicate]
I keep reading stories from developers who state that once they are able to get the FP programs written in languages like Haskell, Ocaml, Elm and even Rust, to compile without errors, they are pretty ...
0
votes
1
answer
636
views
Why isn't it common to hack partial function application in C#?
In many ways C# supports functional programming, but there is a (shrinking) list of features commonly found in statically-typed functional languages that are missing, such as tail recursion, partial ...
3
votes
2
answers
257
views
functional programming: impact of typedef-ing datatypes on code readability and maintenance
In functional programming languages, such as Scala, data types and structures, are really important. I am in two minds about the use of type-defs in helping with the readability of the code ...