functional module

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.

Functions Index

L (func ( int index ))
L ( Object[] array )
L ( java.util.List list )
_ (elem1, elem2, ... )

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.

show ( list )

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...]"
take (int n , list )

Returns a sub-list containing the first n elements from the list.

take(10, L(function(i) i))
   ==> [0,1,2,3..., 9]
takeWhile ( predicate , list )

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]
drop (int n , list )

Returns a sub-list containing list with the first n elements missing.

drop(7, take(10, L(function(i) i)))
   ==> [7,8,9]
dropWhile ( predicate , list )

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]
head (list )

Returns the head (first element) of the list.

head([1,2,3])
   ==> 1
tail (list )

Returns a list minus the first element (head).

tail([1,2,3])
   ==> [2,3]
last (list )

Returns the last element of the list.

last([1,2,3])
   ==> 3
init (list )

Returns a list minus its last element.

init([1,2,3])
   ==> [1,2]
length (list )

Returns the length of the list. If the list is Infinite, returns -1.

length([1,2,3])
   ==> 3
Null (list )

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
Break ( predicate, list )

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]]
span ( predicate, list )

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]]
splitAt ( int n, list )

Splits the list into two lists at element n.

splitAt(3, [1,2,3,4,5,6])
  ==> [[1,2,3], [4,5,6]]
repeat ( Object obj )

Returns a infinite list that contains obj.

repeat(1)
   ==> [1,1,1,...]
replicate ( int n, Object obj )

Returns a list containing n times the element obj.

replicate(3, 1)
   ==> [1,1,1]
cycle (list )

Returns a cyclic list that contains list.

cycle([1,2,3])
   ==> [1,2,3,1,2,3,1,2,3...]
integers ( { int start { , int step { , int end } } } )

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]
iterate ( f (element) , x )

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,...]
append ( list1, list2 )

Concatenates two lists into one list.

append([1,2,3], [4,5,6])
  => [1,2,3,4,5,6]
concat ( list of lists )

Concatenates lists together into one list.

concat([_(1,2,3),_(4,5,6)])
  => [1,2,3,4,5,6]
concatMap ( func , list of lists )

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])
filter ( predicate, list )

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.

map ( func ( obj ) , list )

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,...]
reverse ( list )

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]
foldl ( f (p1, p2) , arg, list )

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
foldl1 ( f (p1, p2) , arg, list )

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
foldr ( f (p1, p2) , arg, list )

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
foldr1 ( func (p1, p2) , arg, list )

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
scanl ( f (p1, p2) , arg, list )

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]
scanl1 ( f (p1, p2) , list )

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]
scanr ( func (p1, p2) , arg, list )

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]
scanr1 ( func (p1, p2) , list )

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]
inc ( number )

Increases the value passed by 1

inc(1)
  ==> 2
Double ( number )

Doubles the passed value. Note the capital D, so as not to clash with the Pnuts's special variable 'double'.

Double(2)
  ==> 4
compose ( func1 (x) , func2 (x) )

Returns a function that returns func2 (func1 (x)).

compose(inc, abs)(-10)
  ==> 9
or ( list of boolean )

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
and ( list of boolean )

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
any ( predicate, list )

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
all ( predicate, list )

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
elem ( x, list )

Returns true is x is present in list. You probably should not do this with infinite lists.

elem(2, [1,2,3])
  ==> true
notElem ( x, list )

Returns true if x is not present in list. You should not do this with infinite lists.

notElem(2, [1,1,3])
  ==> true
minimum ( list )

Returns the minimum value in list. You should not do this with a infinite list.

minumum([1,2,3,4,5,6])
  ==> 1
maximum ( list )

Returns the maximum value in list. You should not do this with a infinite list.

maximum([1,2,3,4,5,6])
  ==> 6
sum ( list of numbers )

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
product ( list of numbers )

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
zip ( list1 , ... )

Returns the list of arrays contains each element of list1, ....

zip([1,2,3], [1,2,3])
  ==> [[1,1],[2,2],[3,3]]
zip3 ( list1 , list2 , list3 )

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]]
zipWith ( func , list1 , ... )

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]
zipWith3 ( func , list1 , list2 , list3 )

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]
curry ( func , int nargs )

Converts an uncurried function to a curried function.

times = curry(function (x, y) x * y, 2)
dbl = times(2)
dbl(100)
  ==> 200
uncurry ( func , int nargs )

Converts an curried function to a uncurried function.

times = curry(function (x, y) x * y, 2)
times2 = uncurry(times, 2)
times2(2, 3)
  ==> 6
curryApply ( func , arg1 , ... )

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 ( String text )
words ( String text )
unlines ( list )
unwords ( list )

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"
id ( obj1 )
eq ( obj1 , obj2 )
notEq ( obj1 , obj2 )
odd ( number )
even ( number )
inc ( number )
abs ( number )
square ( number )
flip ( function(x, y) )
max ( obj1 , obj2 )
min ( obj1 , obj2 )
quot ( number1 , number2 )
div ( number1 , number2 )
rem ( number1 , number2 )
subtract ( number1 , number2 )
negate ( number )

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