-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhigher-order.qky
More file actions
85 lines (52 loc) · 1.94 KB
/
Copy pathhigher-order.qky
File metadata and controls
85 lines (52 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
[ this ] is higher-order.qky
( ------ Higher Order Functions ------
Copyright (C) 2022 Gordon Charlton
https://github.com/GordonCharlton )
( Glossary:
fold ( [ x --> x ):
fold cumulatively applies the word or nest x to the items in a
nest, taking the first item as the first partial result without
applying x to it. So
' [ 1 2 3 4 5 ] ' + fold
is equivalent to
1 2 + 3 + 4 + 5 +
and returns 15
If the nest is empty fold returns an empty nest.
If the nest has one item, fold returns that item.
x must consume two items and return one item.
foldr ( [ x --> x ):
Like fold, but applies x to the items of the nest in reverse
order, from right to left.
Incidentally, like map and filter, it is defined here in terms
of fold, trivially as [ reverse fold ], but for the benefit of
completists, reverse can be defined with fold as:
[ ' [ [ ] ] swap join
' [ nested swap join ]
fold ] is reverse ( [ --> [ ):
map ( [ x --> [ ):
map applies the word or nest x to each item in a nest. So
' [ [ 1 2 ] [ 3 4 ] [ 5 6 ] ] ' reverse map
would return [ [ 2 1 ] [ 4 3 ] [ 6 5 ] ]
If the nest is empty map returns an empty nest.
x must take one argument and return one item.
filter ( [ x --> [ ):
filter applies the test x to each item in a nest. It returns a
nest of the items for which the test returned true.
If the nest is empty filter returns an empty nest.
x must consume one item and return a Boolean.
)
[ over [] = iff drop done
dip
[ behead swap
' [ witheach ] ]
nested join do ] is fold ( [ x --> x )
[ dip reverse fold ] is foldr ( [ x --> x )
[ ' [ [ ] ] rot join swap
nested
' [ nested join ] join
fold ] is map ( [ x --> [ )
[ ' [ [ ] ] rot join swap
nested ' dup swap join
' [ iff [ nested join ]
else drop ] join
fold ] is filter ( [ x --> [ )