[REBOL] [RWEBOL] HOF and system exploration
From: jan:skibinski:sympatico:ca at: 23-Oct-2002 12:23
Hi All,
First, a bit of statistics to show how useful the HOF
(Higher Order Functions) are for system exploration.
Then a bit on 'signature function.
[Some of the idioms shown below might of general use.]
{How many words are defined in my system?}
length? first system/words
; == 2318
{How many words have any value?}
length? filter :value? (map :to-lit-word first system/words)
; == 645 .
{So what are the other words for? A garbage or contents of definition?}
{Set 'x to be the block of all literal meaningfull words}
x: filter :value? (map :to-lit-word first system/words)
{Convert from lit-words to regular words}
x: map :to-word x
{How many of those are any functions: native, op, function, action?}
is-any-function?: func [x][any-function? get x]
length? filter :is-any-function? x
;== 457
{How many of those are mezzanine functions?}
is-function?: func [x][function? get x]
length? filter :is-function? x
;== 211
{Get only the mezzanine functions}
x: filter :is-function? x
{Get a block of argument counts for mezzanine functions}
args: map :nargs reduce (map :to-get-word x)
; [1 0 1 1 0 1 2 1 3 1 2 1 2 1 2 2 1 1 2 ....]
{Get the maximum of the block of args}
maximum-of args
; == [5 5 4 4 3 3 ....]
{OOPS! Unexpected answer!}
{Sort the args in ascending order}
sort/reverse args
; == [5 5 4 4 3 3 3 ...]
{Yeah, that's what the 'maximum-of appears to be doing!}
{Group the functions according to number of arguments}
x0: filter (func[u] [0 == nargs (get u)]) x
x1: filter (func[u] [1 == nargs (get u)]) x
x2: filter (func[u] [2 == nargs (get u)]) x
x3: filter (func[u] [3 == nargs (get u)]) x
x4: filter (func[u] [4 == nargs (get u)]) x
x5: filter (func[u] [5 == nargs (get u)]) x
{List the count of arguments in each group and total}
map :length? reduce [x0 x1 x2 x3 x4 x5 x]
; == [35 125 34 11 2 2 209]
{Now set the 6 lists of function signatures}
s: copy []
foreach k [x0 x1 x2 x3 x4 x5] [append/only s (map :signature get k)]
{So what is the signature for?}
{For simplicity, let us assume this alias
ordered! = [number! char! date! money! time!]
is legal. Soe here are just two examples:}
signature even?
; == [even? [ordered!] [any!]]
{Which means: function 'even has a type from ordered! to any!}
{Typing can be improved a bit by declaring the result,
as in this "improved" version:}
signature even?'
; == [even?' [ordered!] [logic!]]
=======================================================
Why is a concept of signature important? Many reasons.
One example that immediately comes to mind is that the
function 'filter use in many examples above:
filter :some-function series
works only with a proper subset of functions of this
specific signature:
[some-function: [any-type!] [logic!]]
The filter would barf on anything else!!
If anyone is interested I can post the 'signature function
- together with somehow modified and well tested
'map and 'filter functions.
Jan