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
1
vote
1
answer
227
views
How should domain models be designed — rich domain models with encapsulated logic vs. anemic models with separate service/util layers?
I'm learning Domain-Driven Design (DDD) and studying different architecture patterns, and I’ve come across two seemingly conflicting design philosophies around domain modeling.
1. Rich Domain Model ...
2
votes
1
answer
261
views
Is it okay to mix OOP and modular approaches when building a backend with TypeScript/JavaScript?
I’m designing a backend in TypeScript (could also apply to JavaScript), and I’m wondering about the architectural approach.
Is it better to stick to a single paradigm (e.g., fully object-oriented or ...
3
votes
6
answers
609
views
Is OOP really beneficial for enterprise-scale business software compared to procedural languages like ABAP or COBOL?
I'm currently drafting a Python coding standard for internal enterprise use, primarily targeting business applications that involve heavy data access, reporting, and transactional logic.
In this ...
4
votes
3
answers
320
views
The applicability of functional core - imperative shell to a cli program which contains a wrapper around a binary
Not long ago while I was exploring for solutions to test without mocks I've found out about the functional core, imperative shell architecture. It sounds great, I also think that it would play nicely ...
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 ...
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 ...
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. ...
13
votes
8
answers
6k
views
How do compilers work in a language that doesn't allow recursion?
I'm recently learning the programming language, and I wonder how compilers work when the language itself does not allow recursion, like how the compiler or the runtime checkers makes sure that there ...
18
votes
5
answers
6k
views
Is there a non-deterministic function without side effects?
By definition, a pure function is deterministic + no side effect.
Is there any example for a function which has no side effects, but is non-deterministic? I.e., a function without side effects, but ...
187
votes
15
answers
90k
views
What is the advantage of currying?
I just learned about currying, and while I think I understand the concept, I'm not seeing any big advantage in using it.
As a trivial example I use a function that adds two values (written in ML). ...
64
votes
10
answers
31k
views
Why would a program use a closure?
After reading many posts explaining closures here I'm still missing a key concept: Why write a closure? What specific task would a programmer be performing that might be best served by a closure?
...
4
votes
5
answers
507
views
Anemic Models vs. Rich Models: When to Use?
I'm working on an application and have encountered two different approaches for organizing business logic. There's a general consensus that application rules should be handled in higher layers, so I ...
5
votes
3
answers
708
views
Are "pipelines" in functional programming bad for time complexity?
This question is not meant to be a critique of functional programming, but more hoping for resources and opinions.
I am refactoring some historically messy code so that it follows functional ...
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 ...
4
votes
2
answers
446
views
How to handle file I/O in a for loop?
(this question is written using javascript for examples)
Say I have a bunch of files that I need to access sequentially:
1.json
2.json
3.json
4.json
...
Say I want to transform the data in all of ...
3
votes
3
answers
353
views
How to "pass through" data in a functional programming pipeline so that it's accessible later on in the pipeline
I am trying to refactor some JavaScript code to use functional programming principles.
I have some functions that I want to use in a series of maps.
const transformedData = rawData
.map(...
118
votes
3
answers
116k
views
What is the name of a function that takes no argument and returns nothing? [closed]
In Java 8's java.util.function package, we have:
Function: Takes one argument, produces one result.
Consumer: Takes one argument, produces nothing.
Supplier: Takes no argument, produces one result.
.....
0
votes
3
answers
261
views
How to filter "locally and remotely" in functional programming
My example applies to reading and deleting files (I/O), but this is probably a common scenario (eg, keeping local and global state in sync in functional programming).
I am reading in files from a ...
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 ...
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 ...
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 ...
0
votes
1
answer
190
views
(functional programming) What do you call a function that abstracts away a group of function calls?
(this question is written from a functional javascript point of view)
say you have some pure functions:
function a (arg) {
...
return result
}
function b (arg) {
...
return result
}
function ...
81
votes
14
answers
37k
views
Why are side-effects considered evil in functional programming?
I feel that side effects are a natural phenomenon. But it is something like taboo in functional languages. What are the reasons?
My question is specific to functional programming style. Not all ...
6
votes
2
answers
1k
views
Object immutability and persistence
While I was learning functional programming I have encounterd two for me very similar terms: immutability and persistence. Also I have read simular questions from stackoverflow, but I am still ...
11
votes
6
answers
3k
views
Is ad-hoc polymorphism a good practice in functional programming?
I am developing a utils data engineering package in python, and for the sake of reusability and readability, I chose the functional programming (FP) approach.
Assume a key task of converting data from ...
30
votes
18
answers
6k
views
What would you do if your client required you not to use object-oriented programming?
I am writing a program to simulate the activity of ants in a grid (PDF). The ant can move around, pick up things and drop things.
The problem is while the action of the ants and the positions of each ...
71
votes
12
answers
9k
views
"Everything is a Map", am I doing this right?
I watched Stuart Sierra's talk "Thinking In Data" and took one of the ideas from it as a design principle in this game I'm making. The difference is he's working in Clojure and I'm working in ...
31
votes
3
answers
6k
views
Definition of "functor"; Haskell vs. C++
I'm trying to understand whether the Haskell and C++ communities mean different things by the word "functor", or if there's some underlying concept that unifies the two meanings.
My ...
58
votes
4
answers
13k
views
What is the origin and meaning of the phrase “Lambda the ultimate?”
I've been messing around with functional programming languages for a few years, and I keep encountering this phrase. For example, it is a chapter of "The Little Schemer, which certainly predates ...
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(...
48
votes
7
answers
28k
views
What do you call a function where the same input will always return the same output, but also has side effects?
Say we have a normal pure function such as
function add(a, b) {
return a + b
}
And then we alter it such that it has a side effect
function add(a, b) {
writeToDatabase(Math.random())
return a +...
40
votes
4
answers
14k
views
What is referential transparency?
I have seen that in imperative paradigms
f(x)+f(x)
might not be the same as:
2*f(x)
But in a functional paradigm it should be the same. I have tried to implement both cases in Python and Scheme, ...
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 ...
23
votes
11
answers
8k
views
If we can do functional programming with Python, do we need a specific functional programming language? [closed]
Using generators and lambda, we can do functional programming with Python. You can also achieve the same thing with Ruby.
So the question is: why do we need specific functional programming languages ...
1
vote
3
answers
2k
views
Are "Distributed Enums" an Anti-pattern in non-OOP like they seem to be considered in OOP?
I have recently read about the so-called "distributed enum anti-pattern." In particular, as it relates to using enums in conditional statements. (The idea is apparently that if you were to ...
65
votes
12
answers
21k
views
Why isn't functional programming more popular in the industry? Does it catch on now? [closed]
During my four years at university we have been using much functional programming in several functional programming languages. But I have also used much object oriented programming to, and in fact I ...
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.
...
1
vote
1
answer
863
views
How could I apply the strategy pattern to a react component? [closed]
For the following component, how would I extract the unit logic to allow for composition? In my actual app I am trying to reduce the amount of logic encoded in the component and I have decided that ...
2
votes
1
answer
333
views
Did the term "decorator" originate with OOP design patterns?
The Decorator pattern allows behaviour to be dynamically added to an existing object, effectively "decorating" it with new behaviour. While the pattern as formalised and named seems to have ...
6
votes
1
answer
520
views
Terse finite state machines in Haskell
I'm writing a parser for a markup language in haskell, and a finite state machine fell out of my ideal API. I have code that looks a bit like this:
Token = BoldWord String | Word String | ...
59
votes
12
answers
12k
views
What programming language generates fewest hard-to-find bugs? [closed]
What language, in your opinion, allows the average programmer to output features with the least amount of hard-to-find bugs? This is of course, a very broad question, and I'm interested in very broad ...
7
votes
1
answer
540
views
Can functional programming languages have deadlock conditions?
I am reading through "Clean Architecture: A Craftsman's Guide to Software Structure and Design" and it says that:
All race conditions, deadlock conditions, and concurrent update problems are ...
0
votes
2
answers
807
views
Should I use unnecessary function for readability sake
I am implementing if/else statement, by using function "inCase" to make it more readable:
const size = 'small'
const equals = str1 => str2 => str2 === str1
const inCase = (obj) => ...
1
vote
3
answers
510
views
Scala Option vs. conditional branches
If I'm writing Scala functions and have to check for nulls in situations where I can't avoid it (say, working with Spark UDFs and some legacy Java types), is it better to turn things into Option or to ...
19
votes
5
answers
8k
views
What makes functional programming languages declarative as opposed to Imperative?
On many articles, describing the benefits of functional programming, I have seen functional programming languages, such as Haskell, ML, Scala or Clojure, referred to as "declarative languages" ...
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 ...
0
votes
2
answers
548
views
How to Represent Functional Boolean
.NET Boolean type usually makes if else pair all over the code.
Functional Boolean should be more like Either type. Ideally represented as Either<Unit, Unit>.
However, my issues with Either type ...
88
votes
6
answers
62k
views
Where are all the functional programming design patterns? [closed]
OO programming literature is full of design patterns. Most books on object oriented programming dedicate a chapter or two to design patterns like factories and decorators. So what are the equivalent ...
-1
votes
1
answer
1k
views
Should I use function-based exports/imports or classes?
What would make more sense, to use functions for exports/imports:
// globals/helpers.js
exports.fnOne = () => {}
// features/user.js
const {fnOne} = require()
Or class-based approach with static ...
63
votes
4
answers
23k
views
What are combinators and how are they applied to programming projects? (practical explanation)
What are combinators?
I'm looking for:
a practical explanation
examples of how they are used
examples of how combinators improve the quality/generality of code
I'm not looking for:
explanations of ...