APL'ish operations Re:
[1/5] from: bhandley:zip:au at: 25-Jul-2000 21:42
There are two ways you could go, depending on what you want.
Change each element within the series, or return a new series with the
result of your operation applied to each element.
For the first approach...
values-block: [1 2 3]
while [not tail? values-block] [
values-block: change values-block values-block/1 * 7
]
values-block: head values-block ; Sets the series back to it's head.
print values-block
For the second approach....
>> source-block: [1 2 3]
== [1 2 3]
>> result-block: make block! 3
== []
>> foreach elt source-block [
[ append result-block elt * 7
[ ]
== [7 14 21]
>> print result-block
7 14 21
For something more advanced...
With due credit to the original authors Ladislav Mecir and Carl Sassenrath.
Below is a function that does it. This function is advanced in the sense
that it takes a function as an argument. Passing the function in is done in
a special way.
map: func [{Maps a function to all elements of a block}
f [any-function!] blk [block!]
/local result
][
result: make block! length? blk
foreach elem blk [
append/only result f :elem
]
result
]
Use it like this:
>> my-func: func[x][ x * 7] ; Here I declare my function. my-func is a
word that refers to the created function.
>> map :my-func [1 2 3] ; Here I call map, the colon gets the value of
my-func word - in this case the function I created.
== [7 14 21]
[2/5] from: rohitjain1:yaho:o at: 25-Jul-2000 6:04
--- [bhandley--zip--com--au] wrote:
> There are two ways you could go, depending on what
> you want.
<<quoted lines omitted: 10>>
> back to it's head.
> print values-block
This is what I tried. Doesn't work...
> For the second approach....
> >> source-block: [1 2 3]
<<quoted lines omitted: 7>>
> >> print result-block
> 7 14 21
This works! Thanks..
> For something more advanced...
> With due credit to the original authors Ladislav
<<quoted lines omitted: 24>>
> == [7 14 21]
> >>
Perfect. Just what I needed. Wish there was a way to
make simple one-off functions in a block like [ + 5]
etc.
[3/5] from: bhandley:zip:au at: 26-Jul-2000 10:43
> Perfect. Just what I needed. Wish there was a way to
> make simple one-off functions in a block like [ + 5]
> etc.
>
I think you can. Again according to what you need. This is where Rebol
shines. Rebol gives you the ability to interpret blocks (and strings) in way
other than the default provided by Rebol. That is, according to your own
grammar. Rebol calls this a dialect. Admittedly this is not straight-forward
when you're beginning with Rebol. On the other-hand once you have done this
a few times, you will see opportunities for it everywhere and discover that
it really is not as complex as it sounds.
One way to achieve this is to interpret a block by stepping through it and
checking types then doing something. Another way is to use the parse
function of rebol. Parse takes a string or a block as input plus a grammar
specification that will interpret your input according to rules.
So to your example.
What would [+ 5] actually do and how would it be used?
Brett.
[4/5] from: rohitjain1:y:ahoo at: 26-Jul-2000 1:04
I was thinking in terms of function projection. Take a
binary function (like + ), fix one of the arguments
(5) and create a monadic (?) function that can be
applied to say a vector. Sorry if all this seems like
elementary stuff, I'm just starting with REBOL and
exploring the possibilities.
--- [bhandley--zip--com--au] wrote:
[5/5] from: lmecir::geocities::com at: 26-Jul-2000 19:21
Hi,
> I was thinking in terms of function projection. Take a
> binary function (like + ), fix one of the arguments
> (5) and create a monadic (?) function that can be
> applied to say a vector. Sorry if all this seems like
> elementary stuff, I'm just starting with REBOL and
> exploring the possibilities.
>
It is not that elementary, but with a help of
http://www.rebol.org/advanced/highfun.ryou can do:
block-add-5: mapper (do curry :add 1 5)
>> block-add-5 [1 2 3]
== [6 7 8]
Regards
Ladislav
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted