Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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 ...

5
votes
2answers
182 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 ...
2
votes
5answers
330 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 ...
-2
votes
1answer
68 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
votes
0answers
67 views

Strategies for integrating a sequential program with an event driven API

For simplicity of the question, we have: Object Class: Data_1 Data_2 Data_3 FunctionRequest(); FunctionGet(); Program Loop: {//loop x.FunctionRequest(); //short wait ...
1
vote
1answer
197 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
490 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
118 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
272 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
63 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
3answers
170 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
106 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
382 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 ...
0
votes
0answers
153 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
111 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
69 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
25 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
203 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
112 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
121 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
317 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: ...
3
votes
1answer
230 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
1answer
119 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
2answers
352 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 ...
3
votes
2answers
283 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 ...
5
votes
4answers
336 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
6answers
3k 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 ...
8
votes
2answers
774 views

Do functional programming languages disallow side effects?

According to wikipedia, Functional programing languages, that are Declarative, they disallow side effects. Declarative programming in general, attempts to minimize or eliminate side effects. Also, ...
1
vote
0answers
110 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
2answers
416 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 ...
7
votes
3answers
2k 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
2answers
503 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
1answer
60 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 ...
7
votes
3answers
637 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
2answers
110 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 ...
0
votes
3answers
713 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 ...
2
votes
2answers
199 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) ...
7
votes
2answers
845 views

Functional Programming best practices

I don't have a particular coding problem on hand this is just an excercise to improve my thought process. Few months back I started learning about functional programming( mostly in R) and I fell in ...
2
votes
1answer
99 views

Dynamic *dll substitution?

I got an architectural problem here. Let say there is an IShell. It mainly responsible to map user's commands (represented as a linux-like strings) to an appropriate IExecutable's. Those ...
43
votes
8answers
11k 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 kick for a while. I like how it encourages methods to be used as messages in true object-oriented fashion. But this ...
4
votes
1answer
137 views

Is using Option#get really a bad idea here?

I'm working on a Scala project and Wartremover shows an error about Option#get usage in my code: Option#get is disabled - use Option#fold instead While I do understand how get should often be ...
2
votes
0answers
112 views

Dependencies between functions-only modules: hardcoding vs alternatives

In switching from a procedural background to "FP in the small, OO in the large" I'm grappling with the following problem. Suppose there're modules, each only containing numerical math functions ...
5
votes
3answers
247 views

Can referential transparency be assumed when dealing with floating-point arithmetic?

A pure function is assumed to produce the same outputs given the same inputs. Suppose an (otherwise) side-effects free function computes with floating-point numbers. Due to numerical error, these ...
4
votes
1answer
310 views

Should function composition and piping be tested?

Unit testing requires writing tests first then code, on the other hand in F# (and most of the functional languages) some codes are extremely short as follows: let f = getNames >> Observable....
3
votes
4answers
305 views

Why are functional-style chained map operations considered hard to read?

In the comments to an answer of another question, I proposed the following Java code as a better way of writing a more procedural-style variant of the same operation: employeeService.getEmployee() ...
1
vote
1answer
389 views

Does dependency injection fly in the face of functional programming?

I have the following pure function (f2 and f3 are pure too): class X { def f1(arg: Type1): Type2 = { val x = f2(arg) val y = f3(x) y } def f2... def f3... } Now, I would like ...
8
votes
1answer
382 views

Why most anonymous functions can't be JIT compiled and will never be optimized?

I just read https://techfindings.one/archives/2652 about functional programming and came accross this: anonymous functions can often not be JIT compiled and will never be optimized Can someone ...
1
vote
1answer
101 views

What does it mean “state complects value and time”?

I'm rewatching Rich Hickey great talk "Simple Made Easy" And around min 35:40 when talking about state, mentions that State complects value and time, but I'm not sure I'm understanding this well. Is ...
1
vote
0answers
75 views

How Functional Programming addresses concurrent increment/decrement operations invoked by different users? [duplicate]

Using Functional language, How can 2 different parties achieve the result of increment/decrement operations concurrently? For the below scenario, Let's say, I've 2 quantities in stock and 2 users in ...
0
votes
2answers
359 views

How Functional Programming addresses concurrent increment/decrement operations invoked by different users?

Using Functional language, How can 2 different parties achieve the result of increment/decrement operations concurrently? For the below scenario, Let's say, I've 2 quantities in stock and 2 users in ...
35
votes
6answers
6k 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(...