[REBOL] Updates to hof script
From: jan:skibinski:sympatico:ca at: 16-Nov-2002 0:58
Hi,
Several new functions has been added to hof script,
such as map-2, fold-2, inner-2, . and |
I'll just shortly describe map-2:
USAGE:
MAP-2 f xs ys
DESCRIPTION:
Mapping two series via binary function:
((a -+ b -+ c) -+ [a] -+ [b] -+ [c])
MAP-2 takes any binary function 'f :: (a -+ b -+ c)
and applies it binary-wise to elements of series [a] and [b],
producing as a result a series [c]. The types must match
obviously.
Map-2 is generic enough to support all kinds of basic
arithmetic operations on two vectors, or two matrices,
of any dimensions of course.
map-2 :+ [1 2 3] [4 5 6]
map-2 :subtract [1 2 3] [4 5 6]
map-2 :* [1 2 3] [4 5 6]
map-2 (func[u v][2 * u + (3 * v)]) [1 2 3] [4 5 6]
.: func[x y][sum map-2 :* x y]
square-root sum map-2 (func[x y][(x - y) ** 2]) [1 2 3] [4 5 6]
; Euclidean distance
sum map-2 (func[x y][abs (x - y)]) [1 2 3] [4 5 6]
; Manhattan distance
One can use it for other purposes too:
map-2 :xor "My secret message" "This is my not-really-random secret key"
Operator '| provides for overloading of operators for
numbers, vectors and matrices, if one really insists on the same
symbols.
For example:
| :+ 4 5
| :+ [1 2 3 4] [7 2 0 4]
| :+ [[1 2 3][4 5 6]] [[1 1 1][2 2 2]]
Needs some error handling, though.
Jan
P.S.
I do not know whether anyone reported it yet (Ladislav?),
but these few things really bug me:
:- confusion subtract/negate
:> syntax error
:< syntax error