r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Rebol School] Rebol School

Janko
2-Jan-2009
[969]
I wondered this for long time, and never found it... does rebol have 
a native equivalent of HL functions like map, reduce, filter ?
Steeve
2-Jan-2009
[970]
HL ?
Janko
2-Jan-2009
[971x3]
higher level ... ok sorry Higher Order Functions
functions that take functions as parameters, map, reduce, filter 
are basic functions for functional programming
I know I can write them on my own but because rebol is more of a 
functional language with all kinds of power features I am not sure 
if something like this isn't already in
Steeve
2-Jan-2009
[974x2]
huuu... don't see you point, as far i know, they are easy to simulate 
with some rebol instructions.
with which language are you comparing ?
Janko
2-Jan-2009
[976]
no specific language I will write map in rebol and show you , it 
should be a simple function
Steeve
2-Jan-2009
[977x2]
but what is your reference ? map can have many implementations depending 
the language...
*many specifications
BrianH
2-Jan-2009
[979]
REBOL doesn't include the standard higher-order functions, though 
they can be written in REBOL. Since there is no way to practically 
specify literal function values that work properly, the standard 
in REBOL is to use blocks for control function parameters rather 
than functions. Even though we are making the series mezzanines more 
friendly to function values, even in R3 the standard HOFs are being 
translated to REBOL style before inclusion.
Steeve
2-Jan-2009
[980]
hmm...
BrianH
2-Jan-2009
[981x2]
So R3 has a MAP, but it takes a word and a block of code as parameters 
instead of a function.
And REDUCE means something different.
Janko
2-Jan-2009
[983x3]
aha thanks , yes rebol's  reduce has a different meaning I know
if you are using blocks which seems fine as it's even shorter and 
more agile (and similar to quotations in factor), but how do blocks 
define which parameter is which , in case of map it must take 1 parameter 
in case of reduce 2 ?
I am not too deeply familiar of what blocks are, I suppose these 
are [ block of code ] that you can then "do"
BrianH
2-Jan-2009
[986]
The blocks take no parameters - the calling functions take additional 
parameters of the argument word(s).
Janko
2-Jan-2009
[987]
uh, I don't understand what is what.. I will rather wait and see 
how it's done in R3 :)
BrianH
2-Jan-2009
[988x2]
So R3's MAP takes 3 parameters:
- The data

- The word or block of words that will serve as "parameters" for 
the block.
- The block of code.

>> help map
USAGE:
        MAP 'word data body

DESCRIPTION:

        Evaluates a block for each value(s) in a series and returns them 
        as a block.
        MAP is a native value.

ARGUMENTS:

        word -- Word or block of words to set each time (local) (word! block!)
        data -- The series to traverse (block!)
        body -- Block to evaluate each time (block!)
>> map x [1 2 3] [x * x]
== [1 4 9]
Janko
2-Jan-2009
[990]
aha, I get it , that is nice
BrianH
2-Jan-2009
[991]
>> map [x y] [1 2 3 4 5 6] [x * y]
== [2 12 30]
Janko
2-Jan-2009
[992]
cool
BrianH
2-Jan-2009
[993]
We will see what other HOFs we will add to R3, in REBOL style. FOLD 
looks promising :)
Janko
2-Jan-2009
[994]
great, well map and fold are the most used ones by far
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.