Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

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.

Filter by
Sorted by
Tagged with
0
votes
0answers
24 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) =...
0
votes
1answer
60 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
1answer
58 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 ...
2
votes
1answer
88 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 ...
0
votes
3answers
143 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 ...
3
votes
0answers
135 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
1answer
74 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
1answer
107 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
1answer
64 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 ...
3
votes
0answers
71 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 ...
2
votes
1answer
196 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
1answer
97 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
0answers
24 views

Alternative to bind for command validation

I am new to functional programming and I can't find the way to improve this pattern. I want my commands body as simple as this: firstValidation <| command >>= secondValidation >>= ...
0
votes
1answer
45 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 ...
2
votes
3answers
112 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
2answers
158 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). ...
-1
votes
1answer
90 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
1answer
73 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
3answers
227 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 ...
2
votes
2answers
297 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 ...
9
votes
5answers
607 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
2answers
94 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
2answers
128 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
3answers
233 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 ...
-1
votes
2answers
114 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
2answers
202 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=...
4
votes
2answers
349 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
1answer
104 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
4answers
122 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:...
8
votes
2answers
1k 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; ...
1
vote
1answer
170 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
2answers
378 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
5answers
398 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
1answer
88 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 ...
1
vote
1answer
542 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 ...
9
votes
3answers
548 views

Why usage of assignment operator or loops discouraged in functional programming?

If my function meets below two requirements, I believe function Sum to return the summation of items in a list where item evaluates to true for given condition qualifies to be referred as pure ...
0
votes
1answer
133 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
2answers
280 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
1answer
208 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
4answers
285 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
1answer
137 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 ...
3
votes
3answers
629 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 ...
2
votes
1answer
488 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 ...
1
vote
3answers
405 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 ...
0
votes
1answer
196 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 ...
0
votes
0answers
29 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 ...
2
votes
1answer
594 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
2answers
117 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
1answer
125 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 ...
1
vote
2answers
670 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: ...