Pooh program example 18-higher-order-func-lib.p

Test 18-higher-order-func-lib.p

Source of programm

# Higher order functions fold/filter/map - use built in library implementation.

sub sum( a, b )
  return a + b
end

sub sumsqr( a, b )
  return a * a + b
end

sub even( arg )
  return arg % 2 == 0
end

sub square( arg )
   return arg * arg
end

sub inc( arg )
   return arg + 1
end

# --------------------------------

a = [ 1, 2, 3, 4, 5 ]

println( ~msg 'fold left - sum: ' .. foldfirst2last( ~array a ~func sum ~initval 0 ) ) 
println( ~msg 'fold right - sum: ' .. foldlast2first( ~array a ~func sum ~initval 0 ) ) 

println( ~msg 'fold left - sum of squares: ' .. foldfirst2last( ~array a ~func sumsqr ~initval 0 ) ) 
println( ~msg 'fold right - sum of squares: ' .. foldlast2first( ~array a ~func sumsqr ~initval 0 ) ) 

println( ~msg 'filter - filter odd numbers: ' .. filtercopy( ~array a ~func even ) ) 

println( ~msg '*** mapcopy - square root of numbers ***' )

sqa = mapcopy( ~array a ~func square )

for v sqa 
  println( ~msg v )
end

println( ~msg '*** mapreplace - add one to array ***' )


mapreplace( ~array sqa ~func inc )

for v sqa 
  println( ~msg v )
end




Standard output for 18-higher-order-func-lib.p

fold left - sum: 15
fold right - sum: 15
fold left - sum of squares: 55
fold right - sum of squares: 55
filter - filter odd numbers: [ -> 2, -> 4 ]
*** mapcopy - square root of numbers ***
1
4
9
16
25
*** mapreplace - add one to array ***
2
5
10
17
26

Trace output for 18-higher-order-func-lib.p

025|a = [ 1 , 2 , 3 , 4 , 5] 
027|println( ~msg 'fold left - sum: ' .. foldfirst2last( ~array a:[ -> 1, -> 2, -> 3, -> 4, -> 5 ] ~func sub sum(~a , ~b) ~initval 0 )...
003| sub sum(~a , ~b)
004|  return (a:-> 1 + b:-> 0):1
003| end
003| sub sum(~a , ~b)
004|  return (a:-> 2 + b:-> 1):3
003| end
003| sub sum(~a , ~b)
004|  return (a:-> 3 + b:-> 3):6
003| end
003| sub sum(~a , ~b)
004|  return (a:-> 4 + b:-> 6):10
003| end
003| sub sum(~a , ~b)
004|  return (a:-> 5 + b:-> 10):15
003| end
027|println( ~msg 'fold left - sum: ' .. foldfirst2last( ~array a:[ -> 1, -> 2, -> 3, -> 4, -> 5 ] ~func sub sum(~a , ~b) ~initval 0 ):15 )...
028|println( ~msg 'fold right - sum: ' .. foldlast2first( ~array a:[ -> 1, -> 2, -> 3, -> 4, -> 5 ] ~func sub sum(~a , ~b) ~initval 0 )...
003| sub sum(~a , ~b)
004|  return (a:-> 5 + b:-> 0):5
003| end
003| sub sum(~a , ~b)
004|  return (a:-> 4 + b:-> 5):9
003| end
003| sub sum(~a , ~b)
004|  return (a:-> 3 + b:-> 9):12
003| end
003| sub sum(~a , ~b)
004|  return (a:-> 2 + b:-> 12):14
003| end
003| sub sum(~a , ~b)
004|  return (a:-> 1 + b:-> 14):15
003| end
028|println( ~msg 'fold right - sum: ' .. foldlast2first( ~array a:[ -> 1, -> 2, -> 3, -> 4, -> 5 ] ~func sub sum(~a , ~b) ~initval 0 ):15 )...
030|println( ~msg 'fold left - sum of squares: ' .. foldfirst2last( ~array a:[ -> 1, -> 2, -> 3, -> 4, -> 5 ] ~func sub sumsqr(~a , ~b) ~initval 0 )...
007| sub sumsqr(~a , ~b)
008|  return ((a:-> 1 * a:-> 1):1 + b:-> 0):1
007| end
007| sub sumsqr(~a , ~b)
008|  return ((a:-> 2 * a:-> 2):4 + b:-> 1):5
007| end
007| sub sumsqr(~a , ~b)
008|  return ((a:-> 3 * a:-> 3):9 + b:-> 5):14
007| end
007| sub sumsqr(~a , ~b)
008|  return ((a:-> 4 * a:-> 4):16 + b:-> 14):30
007| end
007| sub sumsqr(~a , ~b)
008|  return ((a:-> 5 * a:-> 5):25 + b:-> 30):55
007| end
030|println( ~msg 'fold left - sum of squares: ' .. foldfirst2last( ~array a:[ -> 1, -> 2, -> 3, -> 4, -> 5 ] ~func sub sumsqr(~a , ~b) ~initval 0 ):55 )...
031|println( ~msg 'fold right - sum of squares: ' .. foldlast2first( ~array a:[ -> 1, -> 2, -> 3, -> 4, -> 5 ] ~func sub sumsqr(~a , ~b) ~initval 0 )...
007| sub sumsqr(~a , ~b)
008|  return ((a:-> 5 * a:-> 5):25 + b:-> 0):25
007| end
007| sub sumsqr(~a , ~b)
008|  return ((a:-> 4 * a:-> 4):16 + b:-> 25):41
007| end
007| sub sumsqr(~a , ~b)
008|  return ((a:-> 3 * a:-> 3):9 + b:-> 41):50
007| end
007| sub sumsqr(~a , ~b)
008|  return ((a:-> 2 * a:-> 2):4 + b:-> 50):54
007| end
007| sub sumsqr(~a , ~b)
008|  return ((a:-> 1 * a:-> 1):1 + b:-> 54):55
007| end
031|println( ~msg 'fold right - sum of squares: ' .. foldlast2first( ~array a:[ -> 1, -> 2, -> 3, -> 4, -> 5 ] ~func sub sumsqr(~a , ~b) ~initval 0 ):55 )...
033|println( ~msg 'filter - filter odd numbers: ' .. filtercopy( ~array a:[ -> 1, -> 2, -> 3, -> 4, -> 5 ] ~func sub even(~arg) )...
011| sub even(~arg)
012|  return ((arg:-> 1 % 2):1 == 0):false
011| end
011| sub even(~arg)
012|  return ((arg:-> 2 % 2):0 == 0):true
011| end
011| sub even(~arg)
012|  return ((arg:-> 3 % 2):1 == 0):false
011| end
011| sub even(~arg)
012|  return ((arg:-> 4 % 2):0 == 0):true
011| end
011| sub even(~arg)
012|  return ((arg:-> 5 % 2):1 == 0):false
011| end
033|println( ~msg 'filter - filter odd numbers: ' .. filtercopy( ~array a:[ -> 1, -> 2, -> 3, -> 4, -> 5 ] ~func sub even(~arg) ):[ -> 2, -> 4 ] )...
035|println( ~msg '*** mapcopy - square root of numbers ***' )...
037|... = mapcopy( ~array a:[ -> 1, -> 2, -> 3, -> 4, -> 5 ] ~func sub square(~arg) )...
015| sub square(~arg)
016|  return (arg:-> 1 * arg:-> 1):1
015| end
015| sub square(~arg)
016|  return (arg:-> 2 * arg:-> 2):4
015| end
015| sub square(~arg)
016|  return (arg:-> 3 * arg:-> 3):9
015| end
015| sub square(~arg)
016|  return (arg:-> 4 * arg:-> 4):16
015| end
015| sub square(~arg)
016|  return (arg:-> 5 * arg:-> 5):25
015| end
037|sqa = mapcopy( ~array a:[ -> 1, -> 2, -> 3, -> 4, -> 5 ] ~func sub square(~arg) ):[ -> 1, -> 4, -> 9, -> 16, -> 25 ]
039|for v = 1
040| println( ~msg v:1 )...
039|end
039|for ...
039|for v = 4
040| println( ~msg v:4 )...
039|end
039|for ...
039|for v = 9
040| println( ~msg v:9 )...
039|end
039|for ...
039|for v = 16
040| println( ~msg v:16 )...
039|end
039|for ...
039|for v = 25
040| println( ~msg v:25 )...
039|end
039|for ...
039|end # finish for loop
043|println( ~msg '*** mapreplace - add one to array ***' )...
046|mapreplace( ~array sqa:[ -> 1, -> 4, -> 9, -> 16, -> 25 ] ~func sub inc(~arg) )...
019| sub inc(~arg)
020|  return (arg:-> 1 + 1):2
019| end
019| sub inc(~arg)
020|  return (arg:-> 4 + 1):5
019| end
019| sub inc(~arg)
020|  return (arg:-> 9 + 1):10
019| end
019| sub inc(~arg)
020|  return (arg:-> 16 + 1):17
019| end
019| sub inc(~arg)
020|  return (arg:-> 25 + 1):26
019| end
048|for v = 2
049| println( ~msg v:2 )...
048|end
048|for ...
048|for v = 5
049| println( ~msg v:5 )...
048|end
048|for ...
048|for v = 10
049| println( ~msg v:10 )...
048|end
048|for ...
048|for v = 17
049| println( ~msg v:17 )...
048|end
048|for ...
048|for v = 26
049| println( ~msg v:26 )...
048|end
048|for ...
048|end # finish for loop