World: r3wp
[Rebol School] Rebol School
older newer | first last |
BrianH 2-Jan-2009 [995] | Many already have REBOL equivalents, named differently. REBOL was not designed by functional programming enthusiasts. |
Janko 2-Jan-2009 [996] | I got used to them so if something is can be cleanly solved by one of them and I have to write some temprorary variables and foreach loops feel bad :) |
BrianH 2-Jan-2009 [997] | R3 has major improvements to FOREACH - it will get used a lot more :) |
Janko 2-Jan-2009 [998x4] | yes, that's why I was asking, you have all sorts of interesting thigns like do, reduce (rebol's), I have seen apply in R3 , so I see there should be a lot of interesting stuff possible but don't have a clear picture what is and what isn't yet |
good | |
this seems to work as simple map fpmap: func [ series mod ] [ new: copy [ ] foreach item series [ append new mod item ] new ] | |
fpmap [ 1 2 3 ] func [a][ a * 100] == [100 200 300] | |
BrianH 2-Jan-2009 [1002] | For the simple case of an argument function taking one parameter, sure. |
Janko 2-Jan-2009 [1003x3] | well your R3 map will surelly be more powerfull, but it's nice to know I can make simple HOFs in R2 too |
I mean map that you made examples off | |
fpreduce: func [ series samp mod ] [ foreach item series [ samp: mod samp item ] samp ] fpreduce [ 1 2 3 ] 0 func [s a][ s + a] == 6 | |
BrianH 2-Jan-2009 [1006] | You can make REBOL-style control functions like R3's MAP in R2 too. |
Janko 2-Jan-2009 [1007x2] | really? that would be nice, I imagine I "do" the block , but before I have to somehow set those variables, I mean words |
but that I have no idea how ... probably something with set, but how do I take the words | |
Steeve 2-Jan-2009 [1009] | in R2: map: func [vars list exec /local res][res: copy [] do reduce [:foreach vars list compose [append res (exec)]] res] >> map [a b][1 2 3 4][a * b] == [2 12] >> map [a][1 2 3 4][a * a] == [1 4 9 16] |
Janko 2-Jan-2009 [1010x2] | hm, vary interesting |
vary = very | |
BrianH 2-Jan-2009 [1012] | That has a couple problems but you are on the right track, Steeve :) |
Janko 2-Jan-2009 [1013x2] | this is a little higher level rebol that I currently know, but very interesting to learn some new things |
wow, compose is very interesting function | |
Steeve 2-Jan-2009 [1015] | and if you always use the same vars (like x, y and z) you can simplify: map: func [list exec /local res][res: copy [] do reduce [:foreach intersect [x y z] exec list compose [append res (exec)]] res] >> map [1 2 3 4][x * x] == [1 4 9 16] >> map [1 2 3 4][x * y] == [2 12] |
BrianH 2-Jan-2009 [1016] | Oh, that is "fixing" it in the wrong direction :( |
Steeve 2-Jan-2009 [1017] | ahah, i know, you can't anymore use inner parents or brackets ;-) |
BrianH 2-Jan-2009 [1018] | Nope, I was talking about 'res capture the first time, but you have added keywords in the second. |
Steeve 2-Jan-2009 [1019x2] | don't see your point, what do you mean with res ? |
oh you say i can't use res as var ? | |
BrianH 2-Jan-2009 [1021x2] | In your first version, you have a problem if the vars list includes the vars 'append or 'res. You need to compose your loop differently. |
In your second version, everything after the "and if" part of the message was a bad idea for a library function :) | |
Steeve 2-Jan-2009 [1023x2] | oh i see... |
map: func [vars list exec /local res][res: copy [] do reduce [:foreach vars list compose [append (res) (exec)]] res] is that ok now ? | |
BrianH 2-Jan-2009 [1025] | Still has the problem with 'append. |
Steeve 2-Jan-2009 [1026x2] | ahah, you want 'append as var too ? |
map: func [vars list exec /local res][res: copy [] do reduce [:foreach vars list compose [(:append) (res) (exec)]] res] | |
BrianH 2-Jan-2009 [1028x2] | Let me look at it for a sec (unless you want to do it - this is the School group). |
Start with this spec from R3: map: func [ {Evaluates a block for each value(s) in a series and returns them as a block.} 'word [word! block!] "Word or block of words to set each time (local)" data [block!] "The series to traverse" body [block!] "Block to evaluate each time" ] [...] | |
Steeve 2-Jan-2009 [1030] | hum seems it's bugous, right... |
BrianH 2-Jan-2009 [1031] | It's native in R3, but if we get this right it could be a mezz backport in a future R2 release. |
Janko 2-Jan-2009 [1032x2] | (this is beyond me but I will hapily watch) |
( aha, 'word is how you can recieve word without evaluation ... a: func [ 'word val ][ set word val ] -- cool ) | |
BrianH 2-Jan-2009 [1034x2] | OK, the words are set using block set semantics: >> set [a b] [1] == [1] >> a == 1 >> b == none |
>> map [a b] [1 2 3] [a * b] ** Script error: * does not allow none! for its value2 argument ** Where: map ** Near: map [a b] [1 2 3] [a * b] | |
Steeve 2-Jan-2009 [1036x2] | i saw... |
simpler: map: func [vars list exec][do reduce [:foreach vars list reduce [:append [] :do exec]]] | |
BrianH 2-Jan-2009 [1038] | Note that all functions of this type have bind/copy overhead of the code block (including FOREACH). |
Steeve 2-Jan-2009 [1039x2] | >> map [x][1 2 3 4][x * x] == [1 4 9 16] >> map [x y][1 2 3 4][x * y] == [2 12] |
yes Brian | |
BrianH 2-Jan-2009 [1041] | Try to source that last version after running it a few times. |
Steeve 2-Jan-2009 [1042x2] | i see nothing... |
there no need of copy , it's copied by the reduce | |
BrianH 2-Jan-2009 [1044] | No, by the foreach. |
older newer | first last |