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
2 votes
2 answers
206 views

Modularity vs pure functions

I often come across this dilemma in my own code and wondered if there is a term for this and if there is a single solution to it. Let me illustrate it with a pseudocode example of making a table from ...
  • 139
-2 votes
4 answers
181 views

Block structured iteration and recursion

Programming languages traditionally have blocks that specifically cater to controlling iteration. There is the simple while-loop: while (i < 3) {...}; Then there is the more complicated for-loop: ...
  • 4,479
1 vote
2 answers
248 views

In what language does a method not return a value and a function does?

I don't know from where I got this but in my head a function returns a value and a method does not. I know that with OOP a method is a function related to a class. But I am trying to remember where I ...
0 votes
2 answers
212 views

Dependency injection vs function parameter

I am working on a project that is structured in hexagonal architecture. It is a multi module gradle project where web layer is a separate module that depends on the domain module. Sample code related ...
  • 123
4 votes
2 answers
287 views

How to organize a chain of functions that share parameters, functional programming

When trying to follow a functional programming paradigm, I often find myself in a situation where I have a chain of functions that I would want to combine/compose somehow, but they all also take in a ...
  • 373
0 votes
2 answers
494 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) => ...
0 votes
2 answers
123 views

How to model transactions in a client-side functional DDD?

I'm new to DDD and I would like to clarify some concepts. I'm thinking about DDD in the client-side. The first one is regarding transactions: My understanding is that transactions are a responsibility ...
1 vote
1 answer
124 views

Private types, units of measure when is it a good idea?

My question is regarding using private type in F#. Consider the case we have some application and we have some library. One may consider using units within the library, and making them private behind ...
  • 111
1 vote
2 answers
133 views

Should I add functionality by adding a new method to a class - or should I "register" the new functionality into a data structure?

I have one large class that computes ~50 different metrics (each metric has no side effects). My code is similar to this: class ReportingMetrics: def __init__(self, data:pd.DataFrame, config:dict)...
  • 195
1 vote
0 answers
127 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 | ...
  • 281
0 votes
1 answer
280 views

How to test a function with several conditional nested side effects

In Python, consider a function like the following: def main(*args): value1 = pure_function1(*args) if condition(value1): value = side_effect1(value1) if value: ...
  • 109
4 votes
2 answers
265 views

Should I use classes instead of functions with a state needed for computation?

I have implemented the cows and bulls game in C++. The code: #include <cstdio> #include <cstdlib> #include <ctime> struct DigitMatches { int matches_in_right_positions; int ...
  • 51
0 votes
2 answers
105 views

How to convert vehicle schedule time window calculation algorithm to FP

I am working on an algorithm that optimizes utilitization of a vehicle. What I have is a list of paths the vehicle is planned to take and between which times (time windows) it has to be at a certain ...
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 ...
  • 221
1 vote
2 answers
152 views

Connecting classes by passing method references

I am trying to find a good way of allowing two objects that are separated by a intermediate object to communicate while keeping the architecture loosely coupled. A solution I have developed is to pass ...
  • 21
1 vote
0 answers
99 views

How to improve maintainability and testability of a service by refactoring it into a functional style

A customer of ours has a rather large (500+ LOC) service as central part of the project. Written in C# and consisting of rather clunky large imperative style functions, I was asked to improve the test ...
1 vote
3 answers
103 views

How to model a time-distributed process in functional programming style?

I'm searching for any formal/generic approach to modeling a process distributed in time with functional programming. Here is an example. Let's implement a very simple notification service. It accepts ...
  • 149
0 votes
2 answers
504 views

Should "functional core, imperative shell" approach canonically break "don't test private methods" rule?

In short, "functional core, imperative shell" can be summarized as: functional core implements logic; you unit test it. Your tests call real functions just like in production real functions ...
-1 votes
1 answer
64 views

Design suggestions for my simple data-analysis program

I need to create a program with the purpose of cross-referencing personal info from a spreadsheet(s), to check for conflicts of interest between clients of 3 different law firms. All of this client ...
  • 99
-1 votes
1 answer
69 views

Appropriate base type for simply typed lambda calculus

Given the following hypothetical programming language: Intended for practical programming A simply typed lambda calculus (STLC) All objects are functions, based on Church encodings I am aware that ...
5 votes
2 answers
2k views

Database access with functional programming

I'm interested in becoming more familiar with functional programming as a paradigm, so I'm trying to introduce a more functional style to some of my projects. I'm struggling to understand how to ...
  • 305
30 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 ...
78 votes
10 answers
15k 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 ...
  • 879
1 vote
2 answers
183 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 ...
  • 143
3 votes
1 answer
119 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 ...
  • 219
5 votes
2 answers
297 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
92 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 ...
  • 41
5 votes
3 answers
617 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 ...
  • 77
4 votes
3 answers
631 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 ...
7 votes
3 answers
596 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 ...
  • 179
16 votes
10 answers
6k 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 ...
  • 517
-1 votes
1 answer
112 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 ...
  • 109
-1 votes
2 answers
168 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 ...
  • 147
3 votes
1 answer
244 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
154 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
204 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 ...
  • 137
7 votes
4 answers
4k 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 ...
  • 179
1 vote
4 answers
669 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 ...
  • 129
-5 votes
1 answer
46 views

Choosing 2 or 3 values out 3 values after calculating their closeness

I want to find a specific algorithm to choose either 2 or 3 out of 3 given values. Like if I have a set of values 10,11,12 all three are close enough so I will calculate the mean of the value and ...
1 vote
2 answers
347 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
198 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
508 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://...
  • 213
5 votes
3 answers
291 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 ...
  • 332
36 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
2k 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 ...
  • 387
1 vote
1 answer
189 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 ...
2 votes
1 answer
397 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 ...
  • 235
6 votes
1 answer
2k 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, ...
  • 97
3 votes
3 answers
208 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
115 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 ...
  • 399

1
2 3 4 5
15