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
votes
1
answer
165
views
Reactive programming language [closed]
I had been using react-js and really like its concept so I want to know if there is a programming language/framework that sort of works like it. For instance, I want to define:
var A = something
var ...
2
votes
1
answer
304
views
Functional Approaches to Serializing Objects to Variable-length Byte Array Output
I have a large number of record types derived from a binary format specification. So far, I've already written a computation expression builder that let’s me read structures from the files easily:
...
1
vote
3
answers
258
views
What is a good method/practice I can employ to keep identical code snippits in two places in sync? Also, help documenting functionals
If I could get some input on the design of this, I would be grateful as well.
Note that I'm programming in python.
There's a function F that takes lots of data, runs some analysis on it (taking ...
3
votes
2
answers
3k
views
Java 8 - When should I use java.util.Stream instead of java.util.Collection?
I started studying functional programming with JavaScript. After this, I started to study it with Java 8 (streams, lambdas and method reference) and I realised that I tend to use streams as much as ...
13
votes
6
answers
3k
views
Best Practice - Wrapping if around function call vs Adding early exit if guard in function
I know this can be very use-case specific, but I find myself wondering this far too often. Is there a generally preferred syntax.
I'm not asking what is the best approach when in a function, I am ...
2
votes
3
answers
501
views
Swappable state object or decoupling data and functions
I come from OOP pradigm and I also know a bit about functional programming and its advantages. Over time I came to like the separation of data and transformations that are applied to it using pure ...
2
votes
2
answers
161
views
How should I call splitted functions? centralize them? or call it one next to one?
According to Is it OK to split long functions and methods into smaller ones even though they won't be called by anything else?, I should split long functions into smaller functions even if they ...
0
votes
3
answers
604
views
Should a method decide whether to execute or not? [closed]
I couldn't find a question on SE but it has probably been asked elsewhere already (in case, please mark it).
A method containing code to run just with specific external conditions can:
decide whether ...
-1
votes
2
answers
204
views
Are executable requirements the most advanced form of declarative code?
The more declarative code is, the less explicit technical details it contains and the closer it gets to requirements expressed in domain language.
In the extreme case, there is no more difference ...
4
votes
2
answers
223
views
Should I define more methods to update necessary UI only, or less methods but update some UI unnecessarily?
For example, suppose my input data and UI is not in 1 to 1 relationship:
html:
<script>
aChanged=function(){
};
bChanged=function(){
};
cChanged=function(){
};
</script>
a:<input id="...
6
votes
2
answers
1k
views
How does understanding category theory help a software engineer?
This question is related but doesn't directly answer my question.
I can imagine that understanding category theory helps someone who is designing a programming language, apparently in particular ...
1
vote
1
answer
220
views
best practice for data.table use in "formal" code
Consider a "large-ish" data set (~2-5M rows) that goes through multiple stages of cleaning/processing:
library(dplyr)
largedat %>%
mutate(
# overwrite v1 based on others
v1 = somefunc(...
0
votes
4
answers
255
views
Is there a mismatch between XSL and OOP? [closed]
context and background:
I prefer OOP for the most part and find it, largely, more intuitive -- this is my bias.
When I read that functional language x is better than OOP language y I think to myself:...
12
votes
2
answers
5k
views
A real-life example of using curry function? [closed]
I was struggled to find a real-life example of using curry function and get the benefit of using curry.
When I google curry function I often see the example like
let add = x => y => x + y;
let ...
0
votes
1
answer
249
views
Functional programming -- changing correlating elements during list mapping
Consider I have a list. I want to iterate over it and map it's elements -- but the mapping might also require to change others than the element I'm currently iterating over.
Let's say I have a switch ...
7
votes
2
answers
1k
views
Encapsulating moving parts in OO vs Minimizing moving parts in FP
I am from OO background just started learning FP paradigm. Came across quote by Michael Feathers - "OO makes code understandable by encapsulating moving parts. FP makes code understandable by ...
1
vote
5
answers
2k
views
Software design pattern for class method that only should be called once
Say I have a TypeScript class:
export class TypeCreator {
entities: Set<Whatever>
registerEntities(e: Set<Whatever>): Set<Whatever>{
return this.entities = e;
}
}
if ...
-1
votes
1
answer
158
views
Why are ReactiveX Operators considered functional?
I'm struggling to understand how a ReactiveX operator can be considered functional.
Operators are implemented as functions, but but with the exception of simple operators like map and reduce many of ...
2
votes
1
answer
2k
views
C++ immutable struct
I would like to be able to implement immutable data in C++. In short, given a C++ object in which I would like to modify a member variable, instead of modifying that member in place I would like to ...
12
votes
4
answers
3k
views
Why usage of assignment operator or loops discouraged in functional programming?
If my function meets the two requirements listed below, I believe that the function Sum returns the summation of the items in a list, where item evaluates as true for a given condition. Doesn't this ...
0
votes
1
answer
400
views
What's the benefit of separating specialised data from behaviour in an algorithm?
Functional programming strongly suggests to separate data from behaviours (functions). However, I can't see the benefit of this for an algorithm's implementation intrinsically tied with particular ...
4
votes
2
answers
315
views
Function that returns non parameter functions
I found a function in c# like this:
private Dictionary<string, Func<string>> ObtenerExtraCfgCampo(MsgDefCamp camp)
{
var extra = new Dictionary<string, Func<string>&...
3
votes
1
answer
498
views
REPL-based Workflow and Unit Testing
As a developer who has primarily been doing functional programming in F# for the last several years, I'm very attached to my REPL and use it run my code as I go, testing and refining each function as ...
1
vote
4
answers
499
views
In Haskell, is it a "violation" of functional programming to interact with something that was not a function parameter?
I'm sure this must have been asked before, but I can't find anywhere that actually answers my question, so apologies if I have simply overlooked this.
I am currently learning Haskell, and loving the ...
0
votes
1
answer
212
views
In what other locations besides infinite streams and infinite lists is memoized lazyness useful?
Haskell is one of the few non-strict languages out there.
In his paper Why Functional Programming Matters, John Hughes uses (memoized) lazy evaluation (as well as higher-order functions) to implement ...
5
votes
3
answers
2k
views
In functional programming, what it means "Order of evaluation does not matter" when function is pure?
In context of pure functions in FP, apart from several benefits mentioned like easy to reason about, testability it also says "order of evaluation does not matter" since output remains same for given ...
3
votes
1
answer
1k
views
How do functional programming advocates use a framework like React or Angular?
I read this blog post recently:
The Two Pillars of JavaScript
Part 1: How to Escape the 7th Circle of Hell, which is essentially a criticism of object oriented programming, and advocacy for funtional ...
2
votes
3
answers
3k
views
Is it allowed to throw a runtime exception from a java.util.Predicate
The java.util.Predicate interface contains the test(...) method, whose JavaDoc description states that it [evaluates] this predicate on the given argument and that it [returns] true if the input ...
2
votes
1
answer
906
views
What is the correct granularity for events in the context of designing a rule-based decision system?
Introduction
We need to design a system that, given a set of events that are happening in the source application, reacts to them and if some conditions have been met, actions can be triggered. Users ...
2
votes
0
answers
474
views
Should I use "Map" or "Select" for new functor-like operators in C#?
I'm designing a type in C# that will be used as an algebraic type in a functional style, but is not a collection type and does not implement IEnumerable. If this type is a Functor, should I name its ...
4
votes
1
answer
3k
views
Naming conventions for pure functions
Are there 'conventions' / best practices on naming pure functions?
For example:
adding numbers: add or sum?
calculating the square root: calcSqrt or sqrt?
reversing a list: reverse or reversed?
...
1
vote
2
answers
159
views
How can an iterative algorithm be controlled dynamically?
Suppose we need an iterative algorithm for mathematical optimisation. Each iteration takes a long and random time. After each iteration, a stopping condition is checked for the iterate x, based on ...
0
votes
1
answer
183
views
Is the usage of flip function a smell for bad design?
Everything is in the title, is the usage of flip function a smell for bad design ?
I'm coming from a JavaScript universe and used to work with lodash/fp or ramda.
Recently, I've written some stuff ...
2
votes
2
answers
2k
views
Patterns for tracking state in recursive Haskell code
A frequent pattern in my Haskell code is element-wise recursion for transformation of a list with some carried state generated using the data in the list. Usually, this looks something like this:
...
4
votes
1
answer
846
views
Are design principles of functional programming languages and current hardware (register-machines) contrary?
Functional languages seek to minimize accidental state (computationally-convenient but logically unnecessary data dependencies) by endorsing the most granularly modular, mathematically unambiguous ...
0
votes
1
answer
139
views
for-loops vs x.Times(...) extension methods
Using C# as an example, extensions on IEnumerable to allow code like collection.Each(i => i.DoStuff) rather than a foreach loop are generally frowned upon, mainly because they don't follow LINQ ...
3
votes
2
answers
744
views
In which programming paradigm can't code reuse be (easily) achieved?
In a video about software architecture that I'm watching, it's stated that the main program/subroutine architectural style
promotes modularity and function reuse
Code reuse is one of the main ...
4
votes
2
answers
2k
views
What is control abstraction in the context of functional programming?
I understand concept data abstraction as it is relevant to OO programming. However on contrary it seems Function Programming promotes or makes use of concept control abstraction.
I tried searching ...
4
votes
4
answers
975
views
Optimal Immutable Data Structure for Highly Dynamic Particle System
Trying to see if immutability is a good fit for highly dynamic objects like game entities (that constantly are moving around and changing) or particle systems, each which might have thousands or ...
9
votes
7
answers
4k
views
what can go wrong in context of functional programming if my object is mutable?
I can see the benefits of mutable vs immutable objects like immutable objects take away lot of hard to troubleshoot issues in multi threaded programming due to shared and writeable state. On the ...
11
votes
3
answers
5k
views
Do functional programming languages disallow side effects?
According to Wikipedia, Functional programming languages, that are Declarative, they disallow side effects. Declarative programming in general, attempts to minimize or eliminate side effects.
Also, ...
1
vote
0
answers
571
views
Understanding transducers, why this is not a transducer
I'm currently learning functional programming and trying to learn a new concept : transducers.
I'm actually getting the point of theory, like it's an advanced map/reduce that aims to provide better ...
4
votes
2
answers
776
views
This functional programming example and what it means
I've been reading Out of the Tar Pit by Ben Moseley and Peter Marks and in section 5.2.3 they discuss state in functional languages compared to procedural languages. The procedural example is as ...
17
votes
2
answers
10k
views
Sample code to explain Banana Monkey Jungle problem by Joe Armstrong [closed]
In the book Coders at work Joe Armstrong stated that:
I think the lack of reusability comes in object oriented
languages, not in functional languages. Because the problem with object oriented
...
3
votes
2
answers
2k
views
Microservices with OOP and Functional Programming [closed]
After reading and using different concepts(right or wrong) questions related to modularity have appeared.
I want to implement the microservice architecture in my projects the right way.
As from ...
2
votes
1
answer
199
views
Rendering and holding state changes in a web app with functional programming
Let's say you have a tic-tac-toe app.
There's the game state which can be represented by an array of size 9.
There's your "reducer" computes new state from old state and player actions.
Then there's ...
14
votes
3
answers
3k
views
Sum Types vs Polymorphism
This past year I took the leap and learned a functional programming language (F#) and one of the more interesting things that I've found is how it affects the way I design OO software. The two things ...
1
vote
2
answers
305
views
Comparing approaches of mapping data objects
Update: I added some diagrams to help understanding
I had a discussion with a colleague about two different approaches in mapping data objects. I'd like to get your opinion on pros and cons between ...
1
vote
3
answers
3k
views
DDD: Event handlers and aggregates in functional programming
When implementing a DDD driven system (based on event sourcing) using a functional programming language (Clojure), should one separate event handler functions from aggregate functions?
In my naive ...
1
vote
2
answers
274
views
Functional programming: does using a generic make a function impure?
public static Func<string, Task<T>> MyMethod<T>(
UserCredentials credentials,
Func<string, string, string, Task<T>> func
) =>
async (value) ...