The functional module makes Pnuts slightly more functional. It allows you to create an infinite array based on lazy evaluation (called 'list'), and apply some common operations on it.
Please refer to http://haskell.org/ for the theoritical background of Functional Programming.
L() returns a org.pnuts.functional.List object from either function, array, or java.util.List object.
_() returns a org.pnuts.functional.List object from arguments.
L(function(i) i) ==> [0,1,2,3...] _(1,3,4,5) ==> [1,3,4,5]
The rest of the functions that take a list as a parameter also accept an object array. An object array is automatically converted to a List object.
Returns a string representation of list.
show(L(function(x) x)) ==> "[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20...]"
Returns a sub-list containing the first n elements from the list.
take(10, L(function(i) i)) ==> [0,1,2,3..., 9]
Takes elements from the list while predicate(element) is true and returns the resulting list.
Note that predicate must be a function that takes one argument and returns a boolean value.
takeWhile(function (x) x < 10, integers()) ==> [0,1,2,3..., 9]
Returns a sub-list containing list with the first n elements missing.
drop(7, take(10, L(function(i) i))) ==> [7,8,9]
Drops elements from the head of list while predicate(element) is true and returns the resulting list.
dropWhile(function (x) x < 7, integers(0, 1, 9)) ==> [7,8,9]
Returns the head (first element) of the list.
head([1,2,3]) ==> 1
Returns a list minus the first element (head).
tail([1,2,3]) ==> [2,3]
Returns the last element of the list.
last([1,2,3]) ==> 3
Returns a list minus its last element.
init([1,2,3]) ==> [1,2]
Returns the length of the list. If the list is Infinite, returns -1.
length([1,2,3]) ==> 3
Returns whether or not the list is empty. Note the capital N, so as not to clash with the Pnuts token 'null'.
Null([1,2,3]) ==> false Null([]) ==> true
Splits list into two lists, the first containing the first few elements for which predicate(element) is false. Note the capital B, so as not to clash with the Pnuts token 'break'.
predicate must be a function that takes one argument and returns a boolean value.
Break(function (x) x == 0, [-3,-2,-1,0,1,2,3]) ==> [[-3,-2,-1], [0,1,2,3]]
Splits list into two lists, the first containing the first few elements for which predicate(element) is true.
Note that predicate must be a function that takes one argument and returns a boolean value.
span(function (x) x < 0, [-3,-2,-1,0,1,2,3]) ==> [[-3,-2,-1], [0,1,2,3]]
Splits the list into two lists at element n.
splitAt(3, [1,2,3,4,5,6]) ==> [[1,2,3], [4,5,6]]
Returns a infinite list that contains obj.
repeat(1) ==> [1,1,1,...]
Returns a list containing n times the element obj.
replicate(3, 1) ==> [1,1,1]
Returns a cyclic list that contains list.
cycle([1,2,3]) ==> [1,2,3,1,2,3,1,2,3...]
Returns a list of integers.
If start is specified, it becomes the first element. The default value of start is zero.
If step is specified, elements are generated by adding it to the previous one. The default value of step is 1.
If end is specified, the last element is the largest one that is not larger than end.
integers() ==> [0,1,2,3,...] integers(10) ==> [10,11,12,13,...] integers(10, 2) ==> [10,11,12,13,...] integers(10, 2) ==> [10,12,14,16,...] integers(10, 15) ==> [10,12,14]
This returns the infinite list (x, f (x), f (f (x)), f (f (f (x)))...) and so on.
iterate(function (x) x + 3, 0) => [0,3,6,9,...]
Concatenates two lists into one list.
append([1,2,3], [4,5,6]) => [1,2,3,4,5,6]
Concatenates lists together into one list.
concat([_(1,2,3),_(4,5,6)]) => [1,2,3,4,5,6]
Creates a list from a list generating function by application of func on all elements in a list passed as the second argument.
concatMap(function (x) L([x*x]), [1,2,3])
Returns the list of the elements in list for which predicate(elem) returns true.
Note that predicate must be a function that takes one argument and returns a boolean value.
Evaluates func for each element of the list and returns the list composed of the results of each such evaluation.
map(function (x) x * 2, integers()) => [0,2,4,6,...]
Returns a list containing the elements of list in reverse order. Note that reverse should not be done to infinite lists.
reverse([1,2,3]) ==> [3,2,1]
Applies function f to the pairs (arg, list[0]), (f (arg, list[0], list[1]), (f (f (arg, list[0], list[1])), list[2]) and so on. i.e. it folds from the left and returns the last value. Note that foldl should not be done to infinite lists.
For example, the following returns the sum of 1..6:
foldl(function (x, y) x + y, 0, [1,2,3,4,5,6]) ==> 21
This is a variant of foldl where the first value of list is taken as arg. Applies function f to the pairs (list[0], list[1]), (f (list[0], list[1], list[2]), (f (f (list[0], list[1], list[2])), list[3]) and so on. ie it folds from the left and returns the last value. Note that foldl should not be done to infinite lists.
foldl1(function (x, y) x + y, [1,2,3,4,5,6]) ==> 21
This is similar to foldl but is folding from the right instead of the left. Note that foldr should not be done to infinite lists.
For example, the following returns the sum of 1..6.
foldr(function (x, y) x + y, 0, [1,2,3,4,5,6]) ==> 21
This is similar to foldl1 but is folding from the right instead of the left. Note that foldr should not be done to infinite lists.
For example, the following returns the sum of 1..6 .
foldr1(function (x, y) x + y, [1,2,3,4,5,6]) ==> 21
Returns a list of all the intermedia values that foldl would compute. i.e. returns the list: arg, f (arg, list[0]), f (f (arg, list[0]), list[1]), f (f (f (arg, list[0]), list[1]), list[2]) and so on.
scanl(function (x, y) x + y, 0, [0,1,2,3,4,5,6]) ==> [0,1,3,6,10,15,21]
This is a variant of scanl where the first value of list is taken as arg. Returns a list of all the intermedia values that foldl would compute. i.e. returns the list: f (list[0], list[1]), f (f (list[0], list[1]), list[2]), f (f (f (list[0], list[1]), list[2]), list[3]) and so on.
scanl(function (x, y) x + y, [0,1,2,3,4,5,6]) ==> [0,1,3,6,10,15,21]
This is similar to scanl but is scanning and folding from the right instead of the left. Note that scanr should not be done on infinite lists.
scanr(function (x, y) x + y, 0, [0,1,2,3,4,5,6]) ==> [21,21,20,18,15,11,6,0]
This is similar to scanl1 but is scanning and folding from the right instead of the left. Note that scanr1 should not be done on infinite lists.
scanr1(function (x, y) x + y, [0,1,2,3,4,5,6]) ==> [21,21,20,18,15,11,6]
Increases the value passed by 1
inc(1) ==> 2
Doubles the passed value. Note the capital D, so as not to clash with the Pnuts's special variable 'double'.
Double(2) ==> 4
Returns a function that returns func2 (func1 (x)).
compose(inc, abs)(-10) ==> 9
Returns true if one of the elements in list is true. Returns false otherwise. You should not try to 'or' an infinite list.
or([false, false, true]) ==> true
Returns true if all the elements in list are true. Returns false otherwise. You should not try to 'and' an infinite list.
and([true, true, true]) ==> true
Returns true if one of predicate(element) are true. Returns false otherwise. You should not try to 'any' an infinite list.
Note that predicate must be a function that takes one argument and returns a boolean value.
any(function (x) x > 3, [1,2,3,4]) ==> true
Returns true if all of the predicate(element) is true. Returns false otherwise. You should not try to 'all' an infinite list.
Note that predicate must be a function that takes one argument and returns a boolean value.
all(function (x) x > 0, [1,2,3,4]) ==> true
Returns true is x is present in list. You probably should not do this with infinite lists.
elem(2, [1,2,3]) ==> true
Returns true if x is not present in list. You should not do this with infinite lists.
notElem(2, [1,1,3]) ==> true
Returns the minimum value in list. You should not do this with a infinite list.
minumum([1,2,3,4,5,6]) ==> 1
Returns the maximum value in list. You should not do this with a infinite list.
maximum([1,2,3,4,5,6]) ==> 6
Returns the sum of the elements of list. You should not do this with an infinite list.
sum([1,2,3,4,5,6,7,8,9]) ==> 45
Returns the products of the elements of list. You should not do this with an infinite list.
product([1,2,3,4,5,6,7,8,9]) ==> 362880
Returns the list of arrays contains each element of list1, ....
zip([1,2,3], [1,2,3]) ==> [[1,1],[2,2],[3,3]]
Returns the list of arrays contains each element of list1, list2, and list3.
zip3([1,2,3], [1,2,3], [1,2,3]) ==> [[1,1,1],[2,2,2],[3,3,3]]
Returns the list of arrays contains the results of func, passing each element of list1 ....
zipWith(function (x, y) x + y, [1,2,3], [1,2,3]) ==> [2,4,6]
Returns the list which contains the results of func, passing each element of list1, list2, and list3.
zipWith3(function (x, y, z) x + y + z, [1,2,3], [1,2,3], [1,2,3]) ==> [3,6,9]
Converts an uncurried function to a curried function.
times = curry(function (x, y) x * y, 2) dbl = times(2) dbl(100) ==> 200
Converts an curried function to a uncurried function.
times = curry(function (x, y) x * y, 2) times2 = uncurry(times, 2) times2(2, 3) ==> 6
Converts an uncurried function to a curried function and applies the rest of the arguments arg1 ... .
dbl = curryApply(function (x, y) x * y, 2) dbl(100) ==> 200
lines() creates an array of string from the original one, '\r' and '\n' serving as separators.
words() creates an array of string from the original one, regex pattern '\W+' serving as separators.
unlines() creates a string from an array of strings, it inserts new line characters between original strings.
unwords() creates a string from an array of strings, it inserts space characters between original strings.
lines("a\nb\nc")
==> ["a", "b", "c"]
unlines(["a", "b", "c"])
==> "a\nb\nc"
words("a b c")
==> ["a", "b", "c"]
unwords(["a", "b", "c"])
==> "a b c"
function id(x) x function eq(a1, a2) a1 == a2 function notEq(a1, a2) a1 != a2 function odd(x) x % 2 != 0 function even(x) x % 2 == 0 function inc(x) x + 1 function abs(x) if (x < 0) -x else x function square(x) x * x function flip(f) function (x, y) f(y, x) function min(x, y) if (x < y) x else y function max(x, y) if (x > y) x else y function quot(x, y) x / y function div(x, y) x / y function rem(x, y) x % y function subtract(x, y) y - x function negate(x) -x