Functional programming (FP) provides a lot of advantages and its popularity has been increasing as a result. However each programming paradigm comes with its own unique jargon and FP is no exception. By providing a glossary we hope to make learning FP easier.
This Project is a fork of Programming Jargon with Javascript ported to a Ruby Version
This should make this glossary as accessible and as possible and introduce features from the new revision.
Where applicable, this document uses terms defined in the Fantasy Land spec
- Arity
- Higher-Order Functions (HOF)
- Partial Application
- Currying
- Function Composition
- Purity
- Side effects
- Idempotent
- Point-Free Style
- Predicate
- Contracts
- Guarded Functions
- Categories
- Value
- Constant
- Functor
- Pointed Functor
- Lift
- Referential Transparency
- Equational Reasoning
- Lazy evaluation
- Monoid
- Monad
- Comonad
- Applicative Functor
- Morphism
- Isomorphism
- Setoid
- Semigroup
- Foldable
- Traversable
- Type Signatures
- Union type
- Product type
- Option
The number of arguments a function takes. From words like unary, binary, ternary, etc. This word has the distinction of being composed of two suffixes, "-ary" and "-ity." Addition, for example, takes two arguments, and so it is defined as a binary function or a function with an arity of two. Such a function may sometimes be called "dyadic" by people who prefer Greek roots to Latin. Likewise, a function that takes a variable number of arguments is called "variadic," whereas a binary function must be given two and only two arguments, currying and partial application notwithstanding (see below).
sum = ->(a, b) { a + b }
puts sum.arity #=> 2
# The arity of sum is 2A function which takes a function as an argument and/or returns a function.
filter = lambda do |pred, xs|
result = []
xs.each_with_index do |_, idx|
result << xs[idx] if pred.call(xs[idx])
end
result
endis = lambda do |type|
->(x) { x.instance_of? type }
end
filter.call(is.call(Fixnum), [0, '1', 2, nil]) #=> [0, 2]Partially applying a function means creating a new function by pre-filling some of the arguments to the original function.
# Helper to create partially applied functions
# Takes a function and some arguments
partial = lambda do |f, *args|
->(*moreArgs) { f.call *(args + moreArgs) }
end
# Something to apply
add3 = ->(a, b, c) { a + b + c }
# Partially applying `2` and `3` to `add3` gives you a one-argument function
fivePlus = partial.call(add3, 2, 3)
fivePlus.call(4) #=> 9Partial application helps create simpler functions from more complex ones by baking in data when you have it. Curried functions are automatically partially applied.
The process of converting a function that takes multiple arguments into a function that takes them one at a time.
Each time the function is called it only accepts one argument and returns a function that takes one argument until all arguments are passed.
sum = ->(a, b) { a + b }
curriedSum = lambda do |a|
->(b) { a + b }
end
curriedSum.call(40).call(2) #=> 42
add2 = curriedSum.call(2)
add2.call(10) #=> 12The act of putting two functions together to form a third function where the output of one function is the input of the other.
compose = lambda do |f, &g|
-> (a) { f.call g.call(a) }
end
floor_and_to_string = compose.call(->(val) { val.to_s }, &:floor)
floor_and_to_string.call(121.212121) #=> "121"A function is pure if the return value is only determined by its input values, and does not produce side effects.
greet = ->(name) { 'Hi, ' + name }
greet.call 'Brianne' #=> "Hi, Brianne"
name = 'Brianne'
greet = -> { greeting = 'Hi, ' + name }
greet.call #=> "Hi, Brianne"A function or expression is said to have a side effect if apart from returning a value, it interacts with (reads from or writes to) external mutable state.
require 'date'
different_every_time = Date.newputs 'IO is a side effect!'A function is idempotent if reapplying it to its result does not produce a different result.
#f(f(x)) = f(x)10.abs.abs #=> 10[2, 1].sort.sort.sort #> [1, 2]Writing functions where the definition does not explicitly identify the arguments used. This style usually requires currying or other Higher-Order functions. A.K.A Tacit programming.
# Given
map = lambda do |fn|
->(list) { list.map &fn }
end
add = lambda do |a|
->(b) { a + b }
end
# Then
# Not points-free - `numbers` is an explicit argument
increment_all = ->(numbers) { map.call(add.call(1)).call numbers }
increment_all.call([1, 2, 3, 4]) #=> [2, 3, 4, 5]
# Points-free - The list is an implicit argument
increment_all_2 = map.call(add.call(1))
increment_all_2.call([1, 2, 3, 4]) #=> [2, 3, 4, 5]increment_all identifies and uses the parameter numbers, so it is not points-free. increment_all_2 is written just by combining functions and values, making no mention of its arguments. It is points-free.
A predicate is a function that returns true or false for a given value. A common use of a predicate is as the callback for array filter.
predicate = ->(a) { a > 2 }
[1, 2, 3, 4].select &predicate #=> [3, 4]TODO
TODO
Objects with associated functions that adhere to certain rules. E.g. Monoid
Anything that can be assigned to a variable.
5
{name: 'John', age: 30}
->(a) { a }
[1]
nilA variable that cannot be reassigned once defined.
#TODO Constants are referentially transparent. That is, they can be replaced with the values that they represent without affecting the result.
An object that implements a map function which, while running over each value in the object to produce a new object, adheres to two rules:
object = [Object.new]and
# composable
g = ->(x) { x }
f = ->(y) { y }
object.map{ |x| f.call(g.call(x)) } === object.map(&g).map(&f) #=> true(f, g be arbitrary functions)
A common functor in Ruby is Array since it abides to the two functor rules:
[1, 2, 3].map{|x| x} #=> [1,2,3]and
f = ->(x) { x + 1 }
g = ->(x) { x * 2 }
[1, 2, 3].map{|x| f.call(g.call(x)) } #=> [3, 5, 7]
[1, 2, 3].map(&g).map(&f) #=> [3, 5, 7]An object with an of function that puts any single value into it.
ES2015 adds Array.of making arrays a pointed functor.
Array.of(1) # [1]Lifting is when you take a value and put it into an object like a functor. If you lift a function into an Applicative Functor then you can make it work on values that are also in that functor.
Some implementations have a function called lift, or lift_a2 to make it easier to run functions on functors.
An expression that can be replaced with its value without changing the behavior of the program is said to be referentially transparent.
Say we have function greet:
greet = -> { 'Hello World!' }Any invocation of greet() can be replaced with Hello World! hence greet is
referentially transparent.
When an application is composed of expressions and devoid of side effects, truths about the system can be derived from the parts.
Lazy evaluation is a call-by-need evaluation mechanism that delays the evaluation of an expression until its value is needed. In functional languages, this allows for structures like infinite lists, which would not normally be available in an imperative language where the sequencing of commands is significant.
An object with a function that "combines" that object with another of the same type.
One simple monoid is the addition of numbers:
1 + 1 #=> 2In this case number is the object and + is the function.
An "identity" value must also exist that when combined with a value doesn't change it.
The identity value for addition is 0.
1 + 0 #=> 1It's also required that the grouping of operations will not affect the result (associativity):
1 + (2 + 3) === (1 + 2) + 3 #=> trueArray concatenation also forms a monoid:
[1, 2].concat [3, 4] #=> [1, 2, 3, 4]The identity value is empty array []
[1, 2].concat([]) #=> [1, 2]If identity and compose functions are provided, functions themselves form a monoid:
identity = -> a { a }
foo = -> b { b }
compose = -> (f, g) { ->(x) {f.call(g.call(x))}}
compose.call(foo, identity).call('param') == compose.call(identity, foo).call('param') #=> true
# and
compose.call(foo, identity).call('param') == foo.call('param') #=> trueA monad is an object with of and chain functions. chain is like map except it un-nests the resulting nested object.
# Implementation
class Array
def chain
reduce([]) {|acc, it| acc.concat(yield it)}
end
end
# Usage
['cat,dog', 'fish,bird'].chain{|a| a.split(',')} #=> ['cat', 'dog', 'fish', 'bird']
#Contrast to map
['cat,dog', 'fish,bird'].map{|a| a.split(',')} # => [["cat", "dog"], ["fish", "bird"]] of is also known as return in other functional languages.
chain is also known as flatmap and bind in other languages.
An object that has extract and extend functions.
class CoIdentity
def initialize(v)
@val = v
end
def extract
val
end
def extend(&f)
self.new(f.call(self))
end
endExtract takes a value out of a functor.
CoIdentity.new(1).extractExtend runs a function on the comonad. The function should return the same type as the comonad.
CoIdentity.new(1).extend{|co| co.extract() + 1} #=> CoIdentity.new(2)An applicative functor is an object with an ap function. ap applies a function in the object to a value in another object of the same type.
# Implementation
class Array
def ap(xs)
reduce([]) { |acc, f| acc.concat(xs.map(&f)) }
end
end
# Usage
[ lambda { |a| a + 1 } ].ap([1]) #=> [2]This is useful if you have multiple applicative functors and you want to apply a function that takes multiple arguments to them.
arg_1 = [1, 3]
arg_2 = [4, 5]
# function needs to be curried for this to work
add = -> (x) { -> (y) { x + y } }
partially_applied_adds = [add].ap(arg_1)# => [#<Proc:0x00000004bffcf8@(irb):497 (lambda)>, #<Proc:0x00000004bffc80@(irb):497 (lambda)>] This gives you an array of functions that you can call ap on to get the result:
partially_applied_adds.ap(arg_2) #=> [5, 6, 7, 8]A transformation function.
A pair of transformations between 2 types of objects that is structural in nature and no data is lost.
For example, 2D coordinates could be stored as an array [2,3] or hash {x: 2, y: 3}.
# Providing functions to convert in both directions makes them isomorphic.
pair_to_coords = -> (pair) { Hash(x: pair[0], y: pair[1]) }
coords_to_pair = -> (coords) { [coords[:x], coords[:y]] }
coords_to_pair.call(pair_to_coords.call([1, 2])) #=> [1, 2]
pair_to_coords.call(coords_to_pair.call({x: 1, y: 2})) #=> {:x=>1, :y=>2} An object that has an equals function which can be used to compare other objects of the same type.
Make array a setoid:
[1, 2].eql? [1, 2] #=> true
[1, 2].eql? [0] #=> false An object that has a concat function that combines it with another object of the same type.
[1].concat [2] #=> [1, 2] An object that has a reduce function that can transform that object into some other type.
sum = ->(list) { list.reduce(0){ |acc, val| acc + val }}
sum.call([1, 2, 3]) #=> 6 TODO
There's quite a bit of variance across the community but they often follow the following patterns:
# TODOIf a function accepts another function as an argument it is wrapped in parentheses.
# TODOThe letters a, b, c, d are used to signify that the argument can be of any type. The following version of map takes a function that transforms a value of some type a into another type b, an array of values of type a, and returns an array of values of type b.
# TODO- Ramda's type signatures
- Mostly Adaquate Guide
- What is Hindley-Milner? on Stack Overflow
A union type is the combination of two types together into another one.
Union types are also known as algebraic types, tagged unions, or sum types.
There's a couple libraries in JS which help with defining and using union types.
A product type combines types together in a way you're probably more familiar with:
# TODOIt's called a product because the total possible values of the data structure is the product of the different values.
See also Set theory.
Option is a union type with two cases often called Some and None.
Option is useful for composing functions that might not return a value.
#TODOUse chain to sequence functions that return Options
#TODOOption is also known as Maybe. Some is sometimes called Just. None is sometimes called Nothing.