Questions tagged [functional-programming]

Functional programming is a programming paradigm based upon building abstractions using functions, avoiding side effects and change of state. Pure functional programming is thread-safe.

Functional programming is a programming paradigm that deals primarily with mathematical functions. In functional languages, functions are first-class values.

Functions take arguments and return results, but typically don't mutate state. This in contrast to , which primarily revolves around statements that change state. The advantage of avoiding mutable state is that you can safely compose functions, and you can use algebraic laws and "substitution of equals for equals" to simplify programs or improve their performance.

One consequence of this is that many common patterns in programming can be abstracted as higher-order functions, which use a user-supplied function that implements real functionality, and apply it to data in a certain way. This can make code more concise and simpler to reason about and understand.

Functional programming has grown out of a mathematical system called lambda calculus, which was developed in the 1930s. was the first programming language to be based on the lambda calculus.

Today, functional programming is getting more and more popular. The main reason for this is the provability of functional programs' properties, and security is very important nowadays. There are many use cases for functional programming, e.g. Computations or Concurrency handling. Use cases of functional programming.

Programming languages

These languages are listed in order of popularity in relation to the functional-programming tag.

Languages that are primarily functional, although some also support mutable state or other programming paradigms:

Languages that have some functional aspects (like support for first class functions) but are not considered functional languages per se:

History and concepts

The Conception, Evolution, and Application of Functional Programming Languages by Paul Hudak.

18902 questions
558
votes
4 answers

Index inside map() function

I am missing a option how to get the index number inside the map function using List from Immutable.js: var list2 = list1.map(mapper => { a: mapper.a, b: mapper.index??? }).toList(); Documentation shows that map() returns Iterable. Is…
Zygimantas
  • 8,547
  • 7
  • 42
  • 54
539
votes
10 answers

What is Haskell used for in the real world?

There is a lot of hype around Haskell, however, it is hard to get information on how it is used in the real world applications. What are the most popular projects / usages of Haskell and why it excels at solving these problems?
Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254
533
votes
22 answers

What is a 'Closure'?

I asked a question about Currying and closures were mentioned. What is a closure? How does it relate to currying?
Ben
  • 10,931
  • 9
  • 38
  • 47
491
votes
17 answers

What is the difference between Scala's case class and class?

I searched in Google to find the differences between a case class and a class. Everyone mentions that when you want to do pattern matching on the class, use case class. Otherwise use classes and also mentioning some extra perks like equals and hash…
Teja Kantamneni
  • 17,402
  • 12
  • 56
  • 86
434
votes
18 answers

What is a Y-combinator?

A Y-combinator is a computer science concept from the “functional” side of things. Most programmers don't know much at all about combinators, if they've even heard about them. What is a Y-combinator? How do combinators work? What are they good…
419
votes
15 answers

Is functional GUI programming possible?

I've recently caught the FP bug (trying to learn Haskell), and I've been really impressed with what I've seen so far (first-class functions, lazy evaluation, and all the other goodies). I'm no expert yet, but I've already begun to find it easier to…
shosti
  • 7,332
  • 4
  • 37
  • 42
414
votes
4 answers

Efficiency of purely functional programming

Does anyone know what is the worst possible asymptotic slowdown that can happen when programming purely functionally as opposed to imperatively (i.e. allowing side-effects)? Clarification from comment by itowlson: is there any problem for which the…
Opt
  • 4,774
  • 3
  • 28
  • 28
377
votes
23 answers

How to call reduce on an array of objects to sum their properties?

Say I want to sum a.x for each element in arr. arr = [ { x: 1 }, { x: 2 }, { x: 4 } ]; arr.reduce(function(a, b){ return a.x + b.x; }); // => NaN I have cause to believe that a.x is undefined at some point. The following works fine arr = [ 1, 2, 4…
YXD
  • 31,741
  • 15
  • 75
  • 115
373
votes
13 answers

Functional style of Java 8's Optional.ifPresent and if-not-Present?

In Java 8, I want to do something to an Optional object if it is present, and do another thing if it is not present. if (opt.isPresent()) { System.out.println("found"); } else { System.out.println("Not found"); } This is not a 'functional…
smallufo
  • 11,516
  • 20
  • 73
  • 111
366
votes
7 answers

How to use filter, map, and reduce in Python 3

This is how I am accustomed to filter, map, and reduce working in Python 2: >>> def f(x): return x % 2 != 0 and x % 3 != 0 >>> filter(f, range(2, 25)) [5, 7, 11, 13, 17, 19, 23] >>> def cube(x): return x*x*x >>> map(cube, range(1,…
Dick Lucas
  • 12,289
  • 14
  • 49
  • 76
355
votes
4 answers

What does "coalgebra" mean in the context of programming?

I have heard the term "coalgebras" several times in functional programming and PLT circles, especially when the discussion is about objects, comonads, lenses, and such. Googling this term gives pages that give mathematical description of these…
346
votes
47 answers

Why functional languages?

I see a lot of talk on here about functional languages and stuff. Why would you use one over a "traditional" language? What do they do better? What are they worse at? What's the ideal functional programming application?
MattBelanger
  • 5,280
  • 6
  • 37
  • 34
344
votes
11 answers

What is Scala's yield?

I understand Ruby and Python's yield. What does Scala's yield do?
Geo
  • 93,257
  • 117
  • 344
  • 520
342
votes
7 answers

Abusing the algebra of algebraic data types - why does this work?

The 'algebraic' expression for algebraic data types looks very suggestive to someone with a background in mathematics. Let me try to explain what I mean. Having defined the basic types Product • Union + Singleton X Unit 1 and using the shorthand…
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
334
votes
15 answers

What is referential transparency?

What does the term referential transparency mean? I've heard it described as "it means you can replace equals with equals" but this seems like an inadequate explanation.
Claudiu
  • 224,032
  • 165
  • 485
  • 680