@ -16,17 +16,22 @@ rec {
* /
singleton = x : [ x ] ;
/* " F o l d " a b i n a r y f u n c t i o n ` o p ' b e t w e e n s u c c e s s i v e e l e m e n t s o f
` list' with ` nul' as the starting value , i . e . , ` fold op nul [ x_1
x_2 . . . x_n ] == op x_1 ( op x_2 . . . ( op x_n nul ) ) ' . ( This is
Haskell's foldr ) .
/* “ r i g h t f o l d ” a b i n a r y f u n c t i o n ` o p ' b e t w e e n s u c c e s s i v e e l e m e n t s o f
` list' with ` nul' as the starting value , i . e . ,
` foldr op nul [ x_1 x_2 . . . x_n ] == op x_1 ( op x_2 . . . ( op x_n nul ) ) ' .
Type :
foldr : : ( a -> b -> b ) -> b -> [ a ] -> b
Example :
concat = fold ( a : b : a + b ) " z "
concat = foldr ( a : b : a + b ) " z "
concat [ " a " " b " " c " ]
= > " a b c z "
# different types
strange = foldr ( int : str : toString ( int + 1 ) + str ) " a "
strange [ 1 2 3 4 ]
= > " 2 3 4 5 a "
* /
fold = op : nul : list :
foldr = op : nul : list :
let
len = length list ;
fold' = n :
@ -35,13 +40,25 @@ rec {
else op ( elemAt list n ) ( fold' ( n + 1 ) ) ;
in fold' 0 ;
/* L e f t f o l d : ` f o l d o p n u l [ x _ 1 x _ 2 . . . x _ n ] = = o p ( . . . ( o p ( o p n u l
x_1 ) x_2 ) . . . x_n ) ' .
/* ` f o l d ' i s a n a l i a s o f ` f o l d r ' f o r h i s t o r i c r e a s o n s */
# FIXME(Profpatsch): deprecate?
fold = foldr ;
/* “ l e f t f o l d ” , l i k e ` f o l d r ' , b u t f r o m t h e l e f t :
` foldl op nul [ x_1 x_2 . . . x_n ] == op ( . . . ( op ( op nul x_1 ) x_2 ) . . . x_n ) ` .
Type :
foldl : : ( b -> a -> b ) -> b -> [ a ] -> b
Example :
lconcat = foldl ( a : b : a + b ) " z "
lconcat [ " a " " b " " c " ]
= > " z a b c "
# different types
lstrange = foldl ( str : int : str + toString ( int + 1 ) ) " "
strange [ 1 2 3 4 ]
= > " a 2 3 4 5 "
* /
foldl = op : nul : list :
let
@ -52,7 +69,7 @@ rec {
else op ( foldl' ( n - 1 ) ) ( elemAt list n ) ;
in foldl' ( length list - 1 ) ;
/* S t r i c t v e r s i o n o f f o l d l .
/* S t r i c t v e r s i o n o f ` f o l d l ' .
The difference is that evaluation is forced upon access . Usually used
with small whole results ( in contract with lazily-generated list or large
@ -140,7 +157,7 @@ rec {
any isString [ 1 { } ]
= > false
* /
any = builtins . any or ( pred : fold ( x : y : if pred x then true else y ) false ) ;
any = builtins . any or ( pred : foldr ( x : y : if pred x then true else y ) false ) ;
/* R e t u r n t r u e i f f f u n c t i o n ` p r e d ' r e t u r n s t r u e f o r a l l e l e m e n t s o f
` list' .
@ -151,7 +168,7 @@ rec {
all ( x : x < 3 ) [ 1 2 3 ]
= > false
* /
all = builtins . all or ( pred : fold ( x : y : if pred x then y else false ) true ) ;
all = builtins . all or ( pred : foldr ( x : y : if pred x then y else false ) true ) ;
/* C o u n t h o w m a n y t i m e s f u n c t i o n ` p r e d ' r e t u r n s t r u e f o r t h e e l e m e n t s
of ` list' .
@ -219,7 +236,7 @@ rec {
= > { right = [ 5 3 4 ] ; wrong = [ 1 2 ] ; }
* /
partition = builtins . partition or ( pred :
fold ( h : t :
foldr ( h : t :
if pred h
then { right = [ h ] ++ t . right ; wrong = t . wrong ; }
else { right = t . right ; wrong = [ h ] ++ t . wrong ; }