First Class Citizen
An entity that can be passed around as an argument, returned from a function, modified and assigned to a variable.
First Class Function
A function that is treated as first class citizen.
Higher-Order Functions (HOF)
A function which takes function as an argument and/or that return a function.
Closure
A function that remembers its lexical scope even when the function is executed outside that lexical scope.
function greeterFn() : block() {
var name="World" //name is a local variable created by init
var greet = \-> print("Hello ${name}") //greet is an inner function
//that uses the variable declared
//in parent function.
return greet
}
var greeter = greeterFn()
greeter()
Currying
The process of converting a function that takes multiple arguments into a function that takes them one at a time.
var sum = \ a : int, b : int -> a + b
print(sum(1,2))
var curriedSum = \ a : int -> \ b : int -> a + b
print(curriedSum(1)(2))
Function Composition
The act of putting two functions together to form a third function where the output of one function is the input of the other.
uses java.lang.Integer
uses java.lang.Double
uses java.lang.Math
var compose = \ output : block(out : Integer) : String, func : block(param: Double) : Integer -> \ arg : Double -> func(output(arg)) //Definition
var floorToString = compose(\ out -> out.toString(), \ param -> Math.floor(param)) //Usage
print(floorToString(121.212121))
Continuation
The part of the code that's yet to be executed.
uses java.lang.Double
var printAsString = \ num : Double -> print("Given ${num}")
var addOneAndContinue = \ num: Double, cc : block(___num : Double) -> {
var result = num + 1
cc(result)
}
addOneAndContinue(2, printAsString)
Purity
A function is pure if the return value is only determined by its input values, and does not produce side effects.
var greet = \ name: String -> print("Hello ${name}")
greet("World")
The following is not pure since it modifies state outside of the function:
var greeting : String
var greet = \ name: String -> {greeting ="Hello ${name}"}
greet("World")
print(greeting)
Side Effects
A function or expression is said to have a side if apart from returning a value, it interacts with (reads from or writes to) external mutable state.
var currentDate = java.util.Date.CurrentDate //Retrieves the date from the system.
print(currentDate)
gw.api.util.Logger.forCategory("side-effect").info('IO is a side effect!')
Idempotent
A function is idempotent if reapplying it to its result does not produce a different result.
print(java.lang.Math.abs(java.lang.Math.abs(10)))
Point-Free Style (Tacit Programming)
Write functions where the definition does not explicitly identify the arguments used. This style usually requires currying or other higher order functions.
uses java.lang.Integer
// Given
var map = \ fn : block(item : int) : int -> \ list : List<Integer> -> list.map<Integer>(\ item -> fn(item))
var add = \ a : int -> \ b : int -> a + b
var nums : List<Integer> = (0..5).toList()
// Not points-free - 'numbers' is an explicit argument
var incrementAll = \ numbers : List<Integer> -> map(add(1))(numbers)
print(incrementAll(nums))
// Points-free - The list is an implicit argument
var incrementAll2 = map(add(1))
print(incrementAll2(nums))
Predicate
A function that returns true or false for a given value.
var predicate = \ a : int -> a > 2
print((1..4).where(\ a -> predicate(a)))
Lambda
An anonymous function that can be treated like a value.
(\ a : int -> a + 1)(1)
Lambda can be assigned to a variable
var add1 = \ a : int -> a + 1
print(add1(1))
Reference
https://github.com/hemanth/functional-programming-jargon
https://en.wiktionary.org/wiki/second-class_object#English
https://en.wiktionary.org/wiki/third-class_object#English
Recent Comments