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
24
votes
4
answers
13k
views
Should functions that take functions as parameters, also take parameters to those functions as parameters?
I often find myself writing functions that look like this because they
allow me to easily mock data access, and still provide a signature that accepts parameters to determine what data to access.
...
5
votes
1
answer
583
views
FRP-style GUIs in Java
I recently built a small web application using riotjs, a library that facilitates creating UIs in the spirit of functional reactive programming, i.e. defining the UI as a pure function of the ...
8
votes
2
answers
2k
views
Are all deterministic functions free of side-effects (and vice versa)?
I'm reading about pure-functions in functional programming and am wondering, whether a function being deterministic implies that the function is also side-effect free? (and vice versa?)
1
vote
1
answer
208
views
Can I, Should I, How do I refactor for vuex - redux?
We need to get more much more rigorous about managing state in our business application. The heart of our problem is an object, lets call it an "TaxRefundApplication", that has about 20 properties, ...
4
votes
1
answer
489
views
What are some practical uses of Generalized Algebraic Datatypes? [closed]
All tutorials on GADTs that I've seen (in Haskell, Coq and Idris) use one same exapmle of a well-typed interpreter to show how GADTs can be useful, where you use the type index to encode the type of ...
4
votes
2
answers
508
views
What are the correct functional programming terms for what I'm doing here?
I recently needed a function to deep clone an object. I started with the code given as an accepted answer to this question: https://stackoverflow.com/a/728694/1253156
However, our SonarQube ...
1
vote
2
answers
2k
views
Functional programming, Domain Driven Design, and Test/Behavior Driven Development
I am posting because, first of all, I want to make sure my understanding on these 3 methodologies is correct. Secondly, I would like some clarification on how they could all fit in together. I see ...
9
votes
2
answers
810
views
How can I _read_ functional JavaScript code?
I believe I have learned some/many/most of the basic concepts underlying functional programming in JavaScript. However, I have trouble specifically reading functional code, even code I've written, and ...
4
votes
2
answers
1k
views
Java8: why two composition methods: andThen and compose?
As a beginner in both java8 and functional programming, I think I'm missing something when reading about function composition since I cannot find a reason why there are two methods that do this, ...
3
votes
1
answer
764
views
Why Functional Programming - When is it okay to get rid of efficiency for readability?
I was reading Death of for-loop article, and I thought maybe I should timeit the codes. I created this benchmark at jsperf which clearly shows that map-filter approach is about 90% slower.
So, why is ...
3
votes
1
answer
1k
views
Should I pass all arguments to a method explicitly in functional programming?
I wonder whether a method in a functional programming language should receive all variables from the argument list, or whether it is ok to use variables from the outer scope?
But let me explain the ...
4
votes
3
answers
902
views
Can an Impure Function be Referentially Transparent?
Is a total, impure function considered to be Referentially Transparent (RT)?
Example:
// Given an 'id', try to retrieve an optional Person
// from the database, i.e. side-effect.
// Note that '...
2
votes
2
answers
4k
views
Multiple arguments vs a tuple argument
In functional languages without type checking, is there any substantial disadvantage (apart from readability) to limiting all functions to take exactly one argument - that is, replacing multi-argument ...
3
votes
3
answers
1k
views
What is the core philosophy of functional programming? [closed]
I come from an object oriented programming background, therefore, I am pretty curious about the core philosophy of functional programming? Why does it exist at the first place, and what types problems ...
10
votes
1
answer
2k
views
How does functional style helps with mocking dependencies?
From the interview with Kent Beck in a recent Java Magazine issue:
Binstock: Let’s discuss microservices. It seems to me that test-first on microservices would become complicated in the sense that ...
2
votes
0
answers
197
views
combine reader monad with choice
I have this two types
open FSharpx
open FSharpx.Reader
type First =
{ Name: string
Items: string list }
type Second =
{ Name: string
Numbers: int list }
Using a Reader monad ...
-1
votes
1
answer
95
views
Need Help to build a Custom Notification read logic for Individual user basis
I'm building a custom Notification section, which provides notification or messages to multiple users on any particular object update, I've created a HTML page like Dashboard where I have added a ...
2
votes
1
answer
130
views
F# - Associating a function with the matching type of object
Let's say I'm programming a chess game. At some point I have to check, which moves are valid for a given piece. What would be the proper way to select the correct pathfinding function for a given ...
5
votes
3
answers
657
views
Mutability and objects; how to properly manage data?
Objects tend to confuse me some. In understand the concept and use, certainly, but I feel like the functional paradigm is somewhat restricting my use of them.
At the moment, I'm creating a simple ...
23
votes
5
answers
4k
views
What is the benefit of having "no runtime errors", like Elm claims?
Some languages claim to have "no runtime errors" as a clear advantage over other languages that has them.
I am confused on the matter.
Runtime error is just a tool, as far as I know, and ...
4
votes
1
answer
444
views
Can heavy use of the service pattern substitute pure functions without losing benefits?
There are huge benefits to pure functions in functional programming, but can the same benefits be obtained in imperative programming with heavy use of the service pattern?
I ask because I want to ...
6
votes
1
answer
629
views
Functional core for elevator system design
How can Gary Bernhardt's "Functional Core / Imperative Shell" architecture be used to design software for an elevator system?
Specifically, let's say there are a few elevators, each with call buttons ...
1
vote
0
answers
344
views
What are the types of tasks for which Functional Programming paradigm really wins over imperative one? [closed]
During its evolution C# gradually gets more and more features which belong to functional paradigm.
Subjectively these features allow (at least me) to be more productive, fluent and write maintainable ...
19
votes
4
answers
4k
views
What are the benefits of referential transparency to a programmer?
In programming, what are the benefits of referential transparency?
RT makes one of the major differences between functional and imperative paradigms, and is often used by advocates of the functional ...
3
votes
0
answers
441
views
Clojure: Decomposing Logging, Metrics, and Business Logic from a Function
I was reading the post A Modern Architecture for FP that included a code snippet that the author wanted to decompose further. I don't know Haskell but I recognize enough to know that I've written many ...
4
votes
1
answer
694
views
Scala Callback Pyramid of Doom
I would like to solicit some general design principles and best practices to avoid creating a callback pyramid of doom particularly in the Scala language.
Consider the following rudimentary and ...
2
votes
1
answer
758
views
Is a trace table useful in functional programming?
A trace table is a technique used to test algorithms.
"The table usually takes the form of a multi-column, multi-row table;
With each column showing a variable, and each row showing each number
...
11
votes
1
answer
8k
views
In the context of functional programming, what are 'total' functions and 'partial' functions?
I'm not finding via Google any explanation that my brain can grasp. Can someone explain this, and if possible, give an example of each using either pseudocode or C#?
The term 'total' function was ...
26
votes
4
answers
9k
views
Functional style exception handling
I've been told that in functional programming one is not supposed to throw and/or observe exceptions. Instead an erroneous calculation should be evaluated as a bottom value. In Python (or other ...
28
votes
5
answers
4k
views
Does catching/throwing exceptions render an otherwise pure method impure?
The following code examples provide context to my question.
The Room class is initialized with a delegate. In the first implementation of the Room class, there are no guards against delegates that ...
4
votes
1
answer
250
views
Is anything gained by making dependencies explicit via function argument lists when implementing pure methods?
This question is followup to this question.
Is there any benefit in avoiding the 'this' operator when implementing pure methods? That is, are there any advantages to making all dependencies explicit ...
27
votes
5
answers
4k
views
How to model a circular reference between immutable objects in C#?
In the following code example, we have an class for immutable objects that represents a room. North, South, East, and West represent exits into other rooms.
public sealed class Room
{
public Room(...
2
votes
6
answers
848
views
Function creating function, compiled languages equivalent
I'm new to compiled languages. I'm learning C. I'm used to coding in python.
I was wondering if there was any equivalent, or replacement method in compiled langues for functions able to create a ...
-7
votes
1
answer
155
views
In internal computer memory Char value ,string and integer how they differentiate time of storing and retrieving
In internal computer memory Char value ,string and integer how they differentiate time of storing and retrieving .
Char A =ASCI value 127
Int value 127
Binary is same then how computer understand ...
0
votes
2
answers
246
views
Is there a name for this kind of function?
I'm a data analyst, not a software developer, and I often find myself writing a function like this (shown in Python syntax here):
def apply_many(arg, *funcs):
return [func(arg) for func in funcs]
...
112
votes
3
answers
16k
views
Why do Trampolines work?
I've been doing some functional JavaScript. I had thought that Tail-Call Optimization had been implemented, but as it turns out I was wrong. Thus, I've had to teach myself Trampolining. After a bit of ...
6
votes
1
answer
840
views
What does "the standard stateless functional view of algorithms" mean?
From Kelvin Murphy's review on Algorithms (4th Edition) by Sedgewick and Wayne
For data structures, it is obviously natural to use classes, but they
also adopt this approach for many algorithms, ...
0
votes
3
answers
287
views
Is it possible to reason about memory just by looking at type signatures?
If I have pure functional programming language with* very very smart optimization* proccess, is it possible to reason about memory usage, just by looking at type signature?
add :: Int -> [Int] -&...
16
votes
3
answers
5k
views
Naming convention for functions which have side effects? [closed]
I heard somebody say their language has a convention where the names of functions which mutate state must end with an exclamation point. I'm trying to write more functional code and I like the idea of ...
26
votes
1
answer
4k
views
Are generator functions valid in functional programming?
The questions are:
Do generators break the functional programming paradigm? Why or why not?
If yes, can generators be used in functional programming and how?
Consider the following:
function * ...
1
vote
1
answer
1k
views
When/how is it okay to redefine/declare variables? [functional]
I've been learning functional programming in javascript of late, and there's one thing that's been confusing me. I can't quite understand if it's ever okay to redefine variables.
Something like: a = ...
3
votes
2
answers
2k
views
When to use tail recursion?
I've recently gotten into functional programming. I asked a question earlier over here "'Remembering' values in functional programming" and learned a lot of things I hadn't even realized I wanted to ...
7
votes
1
answer
1k
views
Can event emitters be functional?
For instance, array.map accepts a callback that returns a value.
const newArray = oldArray.map(item => doSomethingTo(item));
Promises also accept callbacks that return a value.
const ...
21
votes
4
answers
6k
views
"Remembering" values in functional programming
I've decided to take upon myself the task of learning functional programming. So far it's been a blast, and I've 'seen the light' as it were. Unfortunately, I don't actually know any functional ...
2
votes
2
answers
655
views
Simulated functional programming in C -- passing the entire program state as a function argument
I have a struct called State which holds all the variables for the program. Rather than being modified by functions directly, it is the value returned.
Here is some code:
#define USERNAME_LENGTH 20
#...
3
votes
1
answer
284
views
Why the name MailboxProcessor in F#?
It seems that the standard practice is to immediately alias MailboxProcessor<'T> to Agent<'T>. So why the name in the first place anyways? Why don't they just call it Agent<'T>, if ...
2
votes
1
answer
349
views
Pure functions and the outer scope [duplicate]
Excuse my ignorance, I come from the C family of languages but zero exposure to functional languages. I've read that pure functions only generate output based on a given input. Same input gives the ...
17
votes
4
answers
4k
views
Do non-OOP paradigms support concepts such as encapsulation?
One of the important concepts in Object Oriented Programming is Encapsulation. However, lately the software world seems to be tilting in favour of other paradigms like functional programming.
It ...
0
votes
1
answer
126
views
Functional reactive ideomatic 'OR' function
I'm would like to know your opinion of what is an ideomatic way writing a OR function in functional reactive programming.
That is, if I have x number of signals which can return a truthy or falsy ...
2
votes
2
answers
853
views
Unknown number of arguments in currying
Hypothetical situation - can a currying function have an unknown number of arguments (kind of like varargs)
Eg in Python:
addByCurrying(1)(2)(3)(4)
Should equal 10
addByCurrying(5)(6)
Should equal ...