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
79
votes
10
answers
17k
views
How functional programming achieves "No runtime exceptions"
How does a functional programming language, such as Elm, achieve "No runtime exceptions"?
Coming from an OOP background, runtime exceptions have been part of whatever framework that is based ...
1
vote
2
answers
423
views
What is the name of "an IF-ELSE expression that returns a value"? [closed]
Some programming languages allow conditional value assignments that look like
x = (IF condition THEN a ELSE b)
My search-fu is failing me: how do we call such statements in the jargon of ...
3
votes
1
answer
200
views
Choosing the design of a scientific DSL: purely or impurely functional?
My aim is to create a language specific to the scientific field (which would be used mainly in the field of machine learning and physics) which would be based on the functional paradigm, a paradigm ...
5
votes
2
answers
470
views
Doesn't "Always test through the public interface" contradict testing of individual composed functions?
I'm currently reading "Composing Software" by Eric Elliott, which is about functional programming in JavaScript. He states that if you compose multiple functions together, and that these ...
1
vote
1
answer
126
views
Unclear as to the informal definition of foldr
I've encountered the infomral definition of foldr in a couple of books.
I've attached an image from Bird and Wadler's "Introduction to Functional Programming" (1988), but I've seen the same ...
5
votes
3
answers
736
views
How could a computer program do anything if everything is immutable?
I feel this is a bad question because I probably do not understand what I am talking about. In my effort to learn about functional programming, I became stumped on understanding the idea of immutable ...
4
votes
3
answers
1k
views
Is this extensive usage of closure a known (anti-)pattern? And does it have a name?
I often use function closures for storing data (e.g. database URL), which doesn't change between function calls. Is this an (anti-)pattern? Does it have a name?
While developing apps, which recieve ...
10
votes
4
answers
2k
views
Functional architecture with lots of I/O
I'm learning about "Functional Core, Imperative Shell" as espoused by Gary Bernhardt in his talk about "Boundaries". In reality, it seems like these ideas have been known for a ...
16
votes
10
answers
7k
views
Are immutable objects important only in multi-threaded applications and if so, how are shared immutable objects useful?
I think the answer to the first part of my question is, "yes" -- no point in making objects immutable in a single-threaded application (or I guess in a multi-threaded application if that ...
-1
votes
1
answer
242
views
Refactoring: Pythonic way of reducing the subclasses?
background: so, I am working on an NLP problem. where I need to extract different types of features based on different types of context from text documents. and I currently have a setup where there is ...
0
votes
2
answers
333
views
Trying to understand how this class representation truly represents Natural numbers in Scala
Following Martin Odersky's course on coursera - Functional Programming with Scala and I'm on Week 4 where we're learning about Types and Pattern Matching. In the video lecture, this is the ...
3
votes
1
answer
620
views
Applying Replace Conditional with Composition in functional programming
If I am writing in a functional style and want to use the Replace Conditional with Composition refactoring pattern on a switch or a long chain of if/else if/else if/..., how do I approach it?
I ...
0
votes
2
answers
230
views
Is a safe function returning Maybe partial or total?
The Elm Guide says to use Maybe for partial functions, but I was under the impression that returning Maybe solves the problem of partial functions and makes them total. It gives a value from the ...
1
vote
3
answers
284
views
Is it a good practice for functors to store outputs as instance attributes?
The question pretty much says all, but let's look into a simple example (I'm using C++, but the question is not strictly related to any particular language):
So, let's say that we have a functor that ...
10
votes
5
answers
9k
views
How to name a function that returns a function?
Let's say, for example, that I have a function makeFoo that makes another function foo:
function makeFoo(string) {
return () => string
}
const foo = makeFoo('bar');
I know that makeFoo is a ...
1
vote
4
answers
734
views
Can writing Object-Orientated Code in a Functional style improve performance?
Given a scenario where you have consecutive setters or a series of events where an object is modified, can it be more performant to instead write code in a way where a new state is returned rather ...
2
votes
2
answers
841
views
What's the value of IO Monad?
When I'm writing code in the form of IO Monad, I wonder what's real value of it.For example I have a function as def something(in: In): IO[Out]; my function is a pure function that **returns an impure ...
0
votes
1
answer
234
views
What does it mean for an OS to compile down to a function?
I was reading the Urbit docs and stopped at this paragraph (emphasis mine):
The main thing to understand about our ‘overlay OS’, as we call it, is that the foundation is a single, simple function. ...
1
vote
4
answers
927
views
Design pattern for a function class
I have been experimenting with the idea of function classes as explained in this article and Composition applied to function dependencies as described in the following questions:
https://stackoverflow....
5
votes
3
answers
433
views
What should a member function be called that applies an argument function to the object?
Apply is a fitting name for a member function of a function-type class that applies the function to the given arguments:
class Addition {
int apply(int a, int b) {
return a + b;
}
}
But ...
38
votes
7
answers
9k
views
Do you need to think about encapsulation if you can ensure immutability?
Encapsulation
In object-oriented programming (OOP), encapsulation refers to the
bundling of data with the methods that operate on that data, or the
restricting of direct access to some of an ...
9
votes
4
answers
4k
views
Functional programming - what to learn and who uses it
I'm a .Net and Angular developer who's been working with OO languages throughout my education and work history. Lately I've been thinking about spending some time with one of the functional ...
1
vote
1
answer
372
views
Is designing a generic parameterized class with methods of it accepting higher order functions a functional technique that we can use in Java 8?
Recently I have asked this question: How do you rewrite the code which using generics and functionals in Java 8 and mixing oop and functional programming by using only object-oriented? on ...
3
votes
1
answer
701
views
Does the visitor pattern prevent the need to modify existing code when adding new data types?
From what I understand, the visitor pattern is supposed to solve the expression problem (described here), where a program needs to support performing multiple operations on multiple types, ideally ...
7
votes
1
answer
7k
views
Is there a UML diagram for functional programming?
For modelling software implemented with the imperative or procedural programming paradigm we have Flowcharts, process diagrams, etc.
For object oriented we have UML class diagrams, object diagrams, ...
3
votes
3
answers
250
views
Precisely define "what to solve" and "how to solve" corollary in functional and imperative programming respectively
I am not sure if I ever clearly understood standard corollary "what to solve" and "how to solve" used to point out difference between functional (declarative) and imperative programming paradigm ...
-3
votes
2
answers
159
views
How can we handle inserts/delete cases using Map like data structures in FP?
Let's say, that we keep track of students entering the auditorium using their IDs(Key) and their check-in time(Value) in a Map. We delete the entries once they move out of the auditorium. I want to ...
2
votes
2
answers
425
views
Example of NOT associative (not composable) function
Bartosz Milewski, in his book "Category Theory for Programmers" says that:
Composition is associative. If you have three morphisms, f, g, and h,
that can be composed (that is, their objects match ...
5
votes
3
answers
3k
views
Significant difference between functional and procedural collection handling [closed]
I'm planning an empirical study on function passing - specifically lambdas aka anonymous functions aka arrow functions. Now, although functional or even object-oriented approaches are highly favored ...
2
votes
0
answers
76
views
Do MV* architectural patterns apply not only to OO languages but also functional languages?
I am learning about MV* architectural patterns, thanks to the help from several answers here. I learned that they are used for designing the architectures of user interactive applications.
I also ...
-2
votes
3
answers
612
views
Do functions make Java a functional programming language? [closed]
I have been struggling to understand programming paradigms.
OOP is a paradigm with sole aim of modeling complex (real-world) systems, and it got me thinking:
is OOP the only programming paradigm ...
1
vote
3
answers
453
views
what it means when someone says - "statements/instructions are not composable"?
I have been using c# and trying to learn FP. In context of FP I often hear that usage of basic assignment or return statements are not considered composable, hence their usage is not advised in FP ...
2
votes
3
answers
119
views
How should I best structure my app when I want to sort a "parent" model by a property computed on another "child" model?
Question about OO-programming, functional programming, NoSQL, SQL databases, and software architecture
Hi all,
I am a intermediate-level self-taught programmer, and have been dying to get an answer ...
10
votes
2
answers
890
views
Why are impure functions said to be non-composable?
I understand what pure functions are and when someone says pure functions are composable - I believe it means that the output of one function can be passed as an input to another function but same ...
5
votes
5
answers
634
views
Implicit reading/writing of state in OOP hurts readability, maintainability, and testability. Good way of mitigating this damage?
OOP makes state reads and writes implicit. For instance, in Python:
class Foo:
def bar(self):
# This method may read and/or write any number of self.attributes.
# There is no way ...
1
vote
1
answer
239
views
Representing mathematical tree structures using software in a compact manner
In my work I frequently come across systems of interdependent equations. I have contrived a toy example as follows. The terminal values w, x, y and z are given:
e(y) = A+B
A(y) = x*log(y)+y^z
B(y) =...
1
vote
1
answer
486
views
What do you call the state reducer pattern used by redux?
Redux uses a state reducer pattern where essentially you create a pure function that looks like:
function reducer (previousState, action) {
//...
return newState;
}
This looks similar to ...
-1
votes
1
answer
226
views
Why is it called a functional component?
A quick google search shows that the question I have has not been asked directly, and so I ask it here. For what its worth, I am learning react, and I come from a C++ background, where as far as I ...
6
votes
1
answer
752
views
What's the benefit of avoiding partial functions in Haskell?
AFAIK in Haskell it is heavily recommended to avoid partial functions; and if these seem unavoidable (eg head) then return a Maybe. At least, so the Haskell wiki says 1 2
What's the use of the ...
-1
votes
3
answers
217
views
How do you enforce rules for the members of a collection in a non-OOP way?
When everything seems to be a collection, how do you enforce rules for members of said collection without the use of an interface?
As far as I am aware, in languages that don't fully support OOP ...
4
votes
0
answers
613
views
How to avoid passing configuration across functions?
I am writing module that other people can use in their code. It's a calendar.
I want to allow users to set locale parameter so that they have month names and other data in language of their choice.
...
1
vote
1
answer
183
views
Structuring a pipe for function composition when an intermediate result is needed by later functions
I have a set of pure functions that can be composed almost trivially in a pipe as
initialValue -> [f] -> [g] -> [h] -> ... -> [m] -> [n] -> [o] -> outputValue
The problem I ...
0
votes
1
answer
127
views
Is duplicating, deleting, pushing, and/or an object to/between lists a code smell?
I'm thinking about a structure like this.
cardGame:
players:
- john:
hand:
- card:
id: 1
suit: diamond
...
-1
votes
1
answer
124
views
Which programming paradigm mixes well with reactive in java? [closed]
So I have the feeling that one can forget object oriented programming when reactive streams are in use, due to the lack of the async-await syntax (because with the call chaining, the state has to be ...
6
votes
3
answers
482
views
What's the use of interfaces vs classes in typescript?
In the company in which I work, it is customary to model entities using Interfaces when developing with Typescript.
To me, it seems much more effective to model entities with classes and then ...
5
votes
1
answer
2k
views
FP Free Monad vs OOP Dependency Injection
I've written my first moderately large project in functional style (in F#) and can see the advantages. The main challenge was to achieve the "Onion" architecture i.e. large and "smart" pure core / ...
1
vote
1
answer
353
views
Python - Paradigm to compute different formulas with the same function
I have different equations to compute the same quantity. For example, val = my_func(a, b, c) but also val = my_func(x, y), where my_func represents the quantity I would like to compute: it could be ...
0
votes
1
answer
325
views
F# function like reduce that return different type
I am new to functional programming and I can't find a function like reduce to build an AgreggateRoot given an EventStream:
List.reduce ((aggregate, event) -> aggregate) eventList
As I understand ...
8
votes
3
answers
3k
views
Is it preferable to "compose" monadic functions or "chain" them?
To the best of my understanding Monads were created to allow for composing functions with those that had potential side-effects - loosely speaking.
For me composition implies code like so:
f(g(h(x)))...
1
vote
2
answers
377
views
What is the difference in the implementation of a monad in a purely functional language with respect in an imperative language?
For a long time, the use of these Monad structures has been restricted to a very small circle of languages, many of them purely functional (mainly due to problems related to the management of the IO). ...