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 ...
9
votes
2answers
897 views
Functional programming, compared to the process of a computer [duplicate]
In functional programming, it is considered bad practice (at least from my observations) to use state changes. Since computers operate in an imperative-language-like matter (performing one operation ...
2
votes
1answer
46 views
Time complexity of update and lookup in binary random access list
I'm trying to get through one of the exercises in Okasaki's "Purely Functional Data Structures," where he presents a zeroless binary numbers as a structure for random-access list, and asks to
9.6 ...
12
votes
2answers
302 views
How different is garbage collection in pure languages?
In a pure language like Haskell, all data is immutable and no existing data structures can be changed in any way. Additionally, many algorithms on immutable data and functional programming patterns ...
1
vote
2answers
182 views
Why do Haskell and Scheme use singly-linked lists?
A doubly linked list has minimal overhead (just another pointer per cell), and allows you to append to both ends and go back and forth and generally have a lot of fun.
1
vote
1answer
101 views
Shared data in functional programming
I have been working on a project in JavaScript which requires a data structure (read only), to be shared between two functions.
var mySharedData = ['hours', 'minutes', 'seconds'];
Now I have two ...
3
votes
3answers
253 views
How does Functional Programming's immutability feature work with CQS?
Given immutability (which is often encouraged and said to be one of building blocks of functional programming) and CQS (which says that commands should not return a value other than void/unit), how do ...
25
votes
5answers
2k views
What is it about functional programming that makes it inherently adapted to parallel execution? [duplicate]
I've been reading over and over that functional languages are ideal (or at least very often useful) for parallelism. Why is this? What core concepts and paradigms are typically employed and which ...
3
votes
2answers
441 views
Using 'reduce' vs 'for' loop for returning boolean [closed]
I was wondering what people think about using a reduce function vs loop for returning true if a condition exists.
Example:
var a = [1, 5, 7, 4, 2, 5, 3];
var greaterThan5 = a.reduce(function(prev, ...
4
votes
2answers
300 views
Can higher order functions ever be pure?
I was thinking about pure functions especially in the context of C++, which of course is not a purely functional language, and was wondering if higher order functions in C++ can ever be considered ...
5
votes
3answers
152 views
Why don't “multi-infinite” list comprehensions work with lazy evaluation?
As a simple demonstration of the efficiency of Haskell style, I thoughtlessly ran the following:
take 100 [(a, b, c) | a <- [1..], b <- [1..], c <- [1..], a^2 + b^2 == c^2]
This should be a ...
2
votes
2answers
353 views
Program like NASA?: Margaret Hamilton's Three Primitive Control Structures [closed]
On slide 19 in a presentation Margaret Hamilton describes three primitive control structures. My goal is to reduce the probability of errors in my code by working with this "design framework". I am ...
2
votes
1answer
88 views
Patterns for sharing context variables between functions
I am looking for ways of passing around a set of contextual variables that are required by functions.
As a Python programmer, right now I can see three ways of solving the problem: passing them ...
2
votes
2answers
159 views
Does function pointer have the same expressive power as function as parameter?
In functional programming language, a function can be passed as an argument to another function. In programming language like C/C++, a function pointer referring to a function can be passed to a ...
8
votes
3answers
199 views
Is the benefit of the IO monad pattern for handling side effects purely academic?
Sorry for yet another FP + side effects question, but I couldn't find an existing one which quite answered this for me.
My (limited) understanding of functional programming is that state/side effects ...
7
votes
3answers
262 views
Is there a Haskell idiom for trying several functions and stop as soon as one succeeds?
In Haskell, I can use the type a -> Maybe b to model a function that either returns a value of type b, or returns nothing (it fails).
If I have types a1, ..., a(n+1) and functions f1, ..., fn, ...
1
vote
1answer
84 views
The difference between bind and _.curry
So JavaScript's bind supports currying, but most people use some other library like lodash or ramda to do currying.
From first impression It seems like bind supports context changing, since that is ...
5
votes
2answers
115 views
What is the difference between currying and partial function application in practice
I understand the difference between partial function application and a curried function (f(X x Y x Z) -> N vs f(X -> (Y -> (Z -> N)))), but I do not see what the consequence of this ...
31
votes
2answers
1k views
What is the meaning of “doesn't compose”?
I see a lot of texts, especially functional programming texts, claim that certain CS concepts "don't compose". Examples are: locks don't compose, monads don't compose.
I've been having a hard time ...
2
votes
2answers
96 views
Is the function using python list comprehension, stateless?
Below is the function count_leaf, that appends mutable list branch_counts, which is not stateless.
def count_leaf(tree):
if is_leaf(tree):
return 1
branch_counts = list()
for b in ...
0
votes
0answers
58 views
How to ensure this constraint at compile time?
I have an interesting constraint/invariant and I would love to hear people's input on how to maintain it. Big bonus if it can be ensured at compile time...
I have the types below, and the following ...
1
vote
2answers
133 views
What is the advantage of global functions when writing functional code
I am a Swift developer and am trying to adopt a functional / reactive style in my code. I have been using ReactiveCocoa in all my projects and I have started giving RAC 3.0 a try. One thing I have ...
1
vote
3answers
114 views
Using delegates to avoid duplicate creation of resources
I'm writing a PCL that uses an HttpClient to go visit a few sites and extract data from them. My initial code looked like this:
public static class Download
{
public async static ...
2
votes
0answers
66 views
How to handle complex calculated fields in an ORM
In our API we've got a few central datatypes which need to be "decorated" (so to speak) after retrieval from the database with calculated values. The database is accessed through an ORM which follows ...
2
votes
2answers
243 views
How do I make this functional DSL written in an imperative language more efficient?
Suppose I create a simple functional Domain-specific language (DSL) using an imperative language, in this case C++. Here is a simple implementation of a DSL that can has the notion of a simple value ...
15
votes
3answers
2k views
What problem do algebraic data types solve?
Fair warning, I'm new to functional programming so I may hold many bad assumptions.
I've been learning about algebraic types. Many functional languages seem to have them, and they are fairly useful ...
14
votes
5answers
957 views
Is the semantic contract of an interface (OOP) more informative than a function signature (FP)?
It is said by some that if you take SOLID principles to their extremes, you end up at functional programming. I agree with this article but I think that some semantics are lost in the transition from ...
30
votes
6answers
6k views
Why do programs use call stacks, if nested function calls can be inlined?
Why not have the compiler take a program like this:
function a(b) { return b^2 };
function c(b) { return a(b) + 5 };
and convert it into a program like this:
function c(b) { return b^2 + 5 };
...
4
votes
2answers
146 views
Are (basic) SQL queries semantically equivalent to Higher Order Functions?
Is SQL basically a domain specific instance of map + fold + filter?
It seems to me that the following SQL:
SELECT name
FROM fruits
WHERE calories < 100
is just syntactic sugar for the ...
8
votes
1answer
200 views
Is it possible to have currying and variadic function at the same time?
I am thinking about making currying and variadic functions both available in a dynamically-typed functional programming language, but I wonder if it is possible or not.
Here are some pseudocode:
sum ...
32
votes
9answers
4k 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?
...
1
vote
2answers
106 views
distinguish requests from the same vehicle
I am implementing an small app to track buses based on Crowdsorcing. The riders send data long, lat, mac, route to the server as JSON string.
In my database I have table bus to insert the transmitted ...
1
vote
1answer
218 views
Why can a constructor be used without `new` keyword in Javascript?
I found Date can be used without the new keyword.
Date(1)
> "Thu May 28 2015 15:54:20 GMT+0800 (CST)"
new Date(1)
> Thu Jan 01 1970 08:00:00 GMT+0800 (CST)
I was wondering whether there is ...
4
votes
2answers
132 views
Functional Programming - Functions defining specific evaluation of functions passed to it for optimization
Firstmost, I am just getting started with functional programming so I would appreciate corrections in any terminology I may have used incorrectly.
Story time, While doing a Project Euler Problem 1 in ...
5
votes
2answers
221 views
How to design for good abstractions using algebraic data type?
Every now and then I have peaked at Haskell Tutorials and found the Algebraic data types quite interesting. I took their purpose to be to represent types that have completely separable states. Sadly, ...
6
votes
3answers
187 views
What is the functional programming answer to type-based invariants?
I am aware that the concept of invariants exists across multiple programming paradigms. For example, loop invariants are relevant in OO, functional and procedural programming.
However, one very ...
7
votes
4answers
510 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" ...
3
votes
3answers
481 views
Functional vs object-oriented style in C#
I'm learning functional programming and face the following confusion when applying it to my C# projects:
I begin by writing pure, static functions and use function composition.
After the code ...
7
votes
3answers
262 views
Functional programming and Text adventures
This is mostly a theoretical question about FP, but I'll take text adventures (like old-school Zork) to illustrate my point. I'd like to know your opinions on how would you model a stateful simulation ...
2
votes
1answer
277 views
Should all functions be fully self-contained (is it bad practice to share a variable between functions)?
There are two ways to do the same thing (pseudo code)
Define databaseHandle in the parent function, and use it as a global in this scope:
function API() {
function openDatabase() {
...
1
vote
1answer
358 views
What aspects of Haskell led to its rise in popularity among experts?
20 years ago, the Functional Programming world was all about Lisp and Scheme. When I went to college in 2001, my Fall Semester Freshman CS 101 course was taught in OCaml.
However, these days the ...
1
vote
1answer
154 views
Haskell types for functions
I don't understand the answer to this question:
Q: Can Haskell find a type for the function selfapply defined by: selfapply f = f f
A: The function selfapply is not typeable in the simple ...
1
vote
1answer
281 views
What is the “->” symbol called?
I have seen the -> operator/symbol in Java 8 predicates recently and wondered what its name is. I know that it is used in lambda expressions, but I know that the symbol for lambda is λ, so that's ...
2
votes
2answers
117 views
Dealing with states in a immutable approach [closed]
I want to know how to deal when you have some states in a program, with functions that depends on them, but with a immutable approach. I read some examples and questions, but all focus in a small ...
0
votes
3answers
184 views
Why python function programming functions are not collection methods? [duplicate]
In other words, is there a Python design related reason for it to be so?
Functions like map, filter, reduce etc. are just plain functions.
Is it just a poor design choice (if it is a good one, ...
9
votes
2answers
433 views
Maintaining State without assignment
I am learning functional programming and I have trouble understanding how some particular scenarios are implemented without the use of assignment. The following simple problem pretty much sums up my ...
1
vote
1answer
310 views
Advantages of the imperative style over the functional style [duplicate]
There's a lot of hype over functional languages right now, and I've spent the last year studying Haskell as my intro to FP as a result. Seeing the advantages FP provides is easy (such as referential ...
4
votes
1answer
147 views
Pattern matching against two similar types
What is the best way to handle pattern matching in the following situation?
sealed trait Metadata
final case class Metadata1() extends Metadata
final case class Metadata2() extends Metadata
final ...
0
votes
0answers
33 views
Accessing intermittently available resources with transactions and post-access cleanup in a generalized, functional, composable way
The following psuedo-code illustrates what I'd like to be able to do. Is there a way to accomplish something like this in Scala?:
trait IntermittentlyAvailableResource
trait ...
0
votes
1answer
70 views
FP: Capturing the characteristics of a process which blocks, causes side-effects, and may fail
I have a driver function modifyFile that interacts with many sources in the outside world (e.g. HTTP, filesystem). Let's say the code is as such:
def downloadFile(from: String, to: String): Try[Unit]
...
12
votes
2answers
363 views
Is higher-rank parametric polymorphism useful?
I'm pretty sure everyone is familiar with generic methods of the form:
T DoSomething<T>(T item)
This function is also called parametrically polymorphic (PP), specifically rank-1 PP.
Let's ...