+
2
|
list
|
skin
|
login
|
editor
α-wwwiki
::
map
user:none
(1810 bytes)
_h1 serie & map _p Two useful functions of lambdatalk : serie and map : _ul °°{serie start end}°° generates a serie of integer from start to end with increment 1. Todo : let the increment be choosen different then one. _ul °°{map func serie}°° applies a function (one argument) to a serie of values. It is an equivalent to the iterative structure {u for (var i=start; i< end; i++) func(i);} . The function can be a lambdatalk core function, a function defined in the page (or in another by [[include]]) or a lambda function. _h3 1) serie {pre {@ style="white-space:pre-wrap;"} °°{serie 1 20}°° -> {serie 1 20} } _h3 2) map a function on a serie {pre {@ style="white-space:pre-wrap;"} 1) using a predefined function °°{define sqr (x) return x*x; } °°-> {define sqr (x) return x*x; } °°{map sqr {serie 1 20}}°° -> {map sqr {serie 1 20}} 2) using a lambda function °°{map {lambda (x) return x*x; } {serie 1 20}}°° -> {map {lambda (x) return x*x; } {serie 1 20}} 3) another one °°{map {lambda (x) return Math.sqrt(x);} {serie 1 36}}°° -> {map {lambda (x) return Math.sqrt(x);} {serie 1 36}} } _h3 another one {define foo (x) °° function fac (n) { return (n< 1)? 1 : n*fac(n-1); } var css = "font:bold "+(x/3)+"em courier new;" + "background:rgb("+(25*x)+","+(250-25*x)+",0);" + "transform:rotate("+x+"deg);" + "-webkit-transform:rotate("+x+"deg);" + "border:1px solid white;" + "text-align:center;"; return '{div {@ style="'+css+'"}'+x+'! = '+fac(x)+'}'; °°} _p °°{map foo 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1}°° -> {map foo 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1}