-
Notifications
You must be signed in to change notification settings - Fork 1
/
scratch
176 lines (130 loc) · 2.65 KB
/
scratch
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
.msg1 arg1; .msg2 arg2 | each { echo $_ }
# functions have a context determined at runtime, called $_
# sort of like JS's `this`
# python-style namespaces with multimethods
import a/b/c
# types
type foo [
...
]
set Rectangle .type[
List String
]
.t{Enum a -> List a}
.syntax[
token {\w+} [: WORD ]
token {->} [: ARROW ]
production
]
.foo[a b c]
range 1 10 | each [ puts . ]
range 1 10 | each [ puts . ]
range 1 20 | each [ puts . ]
range 1 20 | each [ puts .[ + 1 . ] ]
range 1 20 | each [ puts .[ * . . ] ]
.def square [ . | * . ] # a little awkward in a simple example, but easier in larger examples
set square [ shift | do [ * . . ] ]
set square [ * . . ]
.list[1 2 3] | each [ puts . ]
.set[1 2 3] | contains? 2 | ? [
puts Yep.
] [
puts Nope.
]
range 1 10 | each [ puts . ]
file /tmp/foo.txt | each [ puts . ]
expr | inspect
class/instance/type
class [Enumerable a] {
# optional type annotation
@each :: (a -> b) -> Enumerable b
@map :: (a -> b) -> Enumerable b
}
instance Enumerable List {
@map :: (a -> b) -> List b
def map { fn.
[] | store out
each { $out | push .[. | fn] }
}
}
type
expr | b # evals expr, then calls $b in the context of the result
expr ; .b # evals expr, switches context to $b
$a | b # call b in context of $a
# . is the current context, pronounced "it".
# .foo is message passing
.class: Object [
def -> [ fn => .fn: .]
]
class Function [
def call: args inContext: context [
# internal
]
]
# stack-based multimethods
#
####################################################################
####################################################################
####################################################################
# macros
.list[1 2 3]
# function macro
.fn[foo bar .]
# evaluation macro
.eval[foo bar]
# shorthand for .eval
.[foo bar]
# text
.text[a b c]
# shorthand
{a b c}
file /tmp/foo | mv . /tmp/bar/ | each [puts .]
# variables
set a b
set a .[foo bar baz]
# equivalent
foo bar baz | set a .
# usage
quux .a
set eval .macro[
shift | do [
# read: lex it, parse it, eval it
lex . | parse . | eval-tree .
]
]
# hash syntax #1: commands
{ a 1; b 2; c 3 }
{
a 1
b 2
c 3
}
# hash syntax #2: json
{ a:1 b:2 c:3 }
# hash syntax #3: lua
{ a=1 b=2 c=3 }
# hash syntax #4: clojure
{ a 1 b 2 c 3 }
# hash syntax #5: slashes
set h %{ a/1 b/2 c/3 }
puts .h/a
puts .h/b
puts .h/c
get .h a
get .h b
get .h c
# list syntax
set l %[a b c]
puts .l/0
puts .l/1
puts .l/2
# set syntax - smalltalk?
set s %<1 2 3 3>
size s # => 3
has? s 2 # => true
has? s 4 # => false
to-list s # => %[1 2 3]
# quoting
edit '.vimrc
set foo '{.bar baz}
set that-last-command '{set foo '{.bar baz}}