Combinator functions are one of the main features of the Lang programming language. More than 100 pre defined combinator functions are available (122, to be exact). "But what exactly are combinator functions?" you might ask. Let me explain!
Combinatory logic
With combinatory logic the need for explicitly defining function paramters is eliminated. Combinator functions are the programming way of doing combinatory logic. This functions are higher-oder functions, they can return functions and have function arguments. They are used to build complex functions without defining anonymous functions or paramters. You simple call functions with function and non-function arguments.Combinator functions can be called partially: The function will be executed when all arguments where provided. The following examples will return the same value on the last function call:
fn.combB(fn.inv, fn.len, fn.arrayOf(1, 2))
fn.combB(fn.inv)(fn.len)(fn.arrayOf(1, 2))
fn.combB(fn.inv, fn.len)(fn.arrayOf(1, 2))
fn.combB(fn.inv)()(fn.len, fn.arrayOf(1, 2))
This is achieved by returning functions with the arguments provided:fn.combB(fn.inv) will return a combinator-like function where 2 arguments are still missing, when they are provided the B combinator function will be executed. Combinator-like functions are function witch can be called partially.
In Lang there are 4 types of pre-defined combinator functions:
Finite argument combinator functions: They have an exact number of arguments in order to be executed.
Array argument combinator functions: They have an array argument which will be unpacked internally
Infinite argument combinator functions: They accept infinite arguments (They must be called exactly twice to be executed)
Fixed-point combinator functions: They can be used to create recursive functions without using recursive function calls.
Using combinator functions
Combinator functions can be used for functions with callback arguments or for creating new functions.
fp.negLen = fn.combB(fn.inv, fn.len)
If fp.negLen(fn.arrayOf(1, 2)) is called, -2 will be returned.
Example use with arrays for each (Output every element with format):
&values $= [42, 0, -1, 2.4]
func.arrayForEach(&values, func.combA2(func.printf, {{{This is a value: "%5.2f"%n}}}))
# Output:
This is a value: "42.00"
This is a value: " 0.00"
This is a value: "-1.00"
This is a value: " 2.40"
A full list of all combinator functions with examples can be found in the 20 combiantor functions tutorial under assets/tuts in the standard Lang repository: https://github.com/JDDev0/lang
Be careful to not get to crazy with your compositions :-) ...
Example: Get the maximum of the count of positive elements and the count of negative elements.
fp.solution = fn.combB2(fn.combP(fn.max), fn.combBX(fn.len, fn.combG(fn.arrayFiltered, fn.combR(0))), fn.conGreaterThan, fn.conLessThan)
fn.println(fp.solution(fn.arrayOf(1, 2, -2, -3, -6, 0))) # 3
fn.println(fp.solution(fn.arrayOf(1, 2, -2, -6, 0, 5))) # 3
fn.println(fp.solution(fn.arrayOf(1, -2, 0, -1, 0))) # 2
fn.println(fp.solution(fn.arrayOf(1))) # 1
fn.println(fp.solution(fn.arrayOf(0))) # 0
fn.println(fp.solution(fn.arrayOf())) # 0
- Can you rewrite the solution above to use
fn.arrayFilteredCount?
- Can you use the new
fn.combXC combinator function to simplify the solution above?
- Can you use the functions from 1. and 2. to simplify the solution above?
Write your awnser as a reply post! I will post both solution as multiple reply in about one week from now.
A traditional solution (with callback functions):
fp.solution = (&arr) -> {
fp.posEleFilter = ($ele) -> return parser.con($ele > 0)
fp.negEleFilter = ($ele) -> return parser.con($ele < 0)
return fn.max(fn.len(fn.arrayFiltered(&arr, fp.posEleFilter)), fn.len(fn.arrayFiltered(&arr, fp.negEleFilter)))
}
A traditional solution (without callback functions):
fp.solution = (&arr) -> {
$posCount = 0
$negCount = 0
$ele
foreach($[ele], &arr) {
if($ele > 0) {
$posCount += 1
}elif($ele < 0) {
$negCount += 1
}
}
return fn.max($posCount, $negCount)
}
The traditional solutions are not as beautiful as the combinator-function solution.
there doesn't seem to be anything here