lasp 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/EXAMPLES.md +77 -7
- data/lib/lasp/corelib.rb +18 -18
- data/lib/lasp/eval.rb +1 -1
- data/lib/lasp/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe53c128ac7fb00e1dcf9e1e89db3596e9557690
|
4
|
+
data.tar.gz: c7a42762434a1192b531bc980fc65ba5b84d06f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18a21269e6682b786c215d16b286ea6ff4533de458dea3099536e9f1b8f709c8cf568f0725a527c398469ccd941213f1c4a8c86f8b6c23f954a8f88c1e64cc44
|
7
|
+
data.tar.gz: 315b73b00df83f029848a3b36dd8b7e3a7fb03bf22109cd4298adff8b6b57eb6bee8afa9c81ba665735d6e5a6962a05f36943feb6481e42419cbdaf86f4f5f20
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# Läsp changelog
|
2
2
|
|
3
|
+
## v0.3.2
|
4
|
+
|
5
|
+
Fix bug in `do` - it accidentally returned part of the AST, now it correctly
|
6
|
+
returns the result of the last expression.
|
7
|
+
|
3
8
|
## v0.3.1
|
4
9
|
|
5
10
|
Make readline support actually work once released to rubygems by implementing it directly in Ruby.
|
data/EXAMPLES.md
CHANGED
@@ -10,6 +10,7 @@
|
|
10
10
|
;
|
11
11
|
; In this document output is shown with ;; and normal comments with ;.
|
12
12
|
|
13
|
+
; --- Data types ---
|
13
14
|
; Number types
|
14
15
|
1
|
15
16
|
1.5
|
@@ -29,7 +30,8 @@ nil
|
|
29
30
|
(list 1 2 3) ;; => [1, 2, 3]
|
30
31
|
(list) ;; => []
|
31
32
|
|
32
|
-
; Basic function
|
33
|
+
; --- Basic function calls ---
|
34
|
+
|
33
35
|
; inc is a function that increments its argument, and 1 is the single argument
|
34
36
|
(inc 1) ;; => 2
|
35
37
|
|
@@ -46,7 +48,7 @@ nil
|
|
46
48
|
; Boolean inversion
|
47
49
|
(not true) ;; => false
|
48
50
|
|
49
|
-
; List operations
|
51
|
+
; --- List operations ---
|
50
52
|
|
51
53
|
(head (list 1 2 3)) ;; => 1
|
52
54
|
(first (list 1 2 3)) ;; => 1
|
@@ -78,7 +80,9 @@ nil
|
|
78
80
|
; Ranges
|
79
81
|
(range 1 10) ;; => [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
80
82
|
|
81
|
-
; Hash-maps
|
83
|
+
; --- Hash-maps ---
|
84
|
+
|
85
|
+
; Create a hash-map
|
82
86
|
(hash-map :one 1 :two 2) ;; => {"one"=>1, "two"=>2}
|
83
87
|
|
84
88
|
; get also works with hash-maps
|
@@ -90,6 +94,8 @@ nil
|
|
90
94
|
; dissoc removes values
|
91
95
|
(dissoc (hash-map :one 1 :two 2) :one) ;; => {"two"=>2}
|
92
96
|
|
97
|
+
; --- More complex functions ---
|
98
|
+
|
93
99
|
; Apply a function to all items in a list
|
94
100
|
(map inc (list 1 2 3)) ;; => [2, 3, 4]
|
95
101
|
|
@@ -107,9 +113,16 @@ nil
|
|
107
113
|
; Filtering
|
108
114
|
(filter odd? (list 1 2 3)) ;; => [1, 3]
|
109
115
|
|
110
|
-
; Variables
|
111
|
-
|
112
|
-
|
116
|
+
; --- Variables ---
|
117
|
+
|
118
|
+
; Define and evaluate a variable
|
119
|
+
(def x (list 1 2 3))
|
120
|
+
x ;; => [1, 2, 3]
|
121
|
+
|
122
|
+
; Use it in a function
|
123
|
+
(sum x) ;; => 6
|
124
|
+
|
125
|
+
--- Misc ---
|
113
126
|
|
114
127
|
; Outputting to the terminal
|
115
128
|
(println "hello world!")
|
@@ -129,7 +142,8 @@ x ;; => [1, 2, 3]
|
|
129
142
|
;; nope!
|
130
143
|
;; => nil
|
131
144
|
|
132
|
-
;
|
145
|
+
; --- Creating functions ---
|
146
|
+
|
133
147
|
; A function has 2 forms, one with the parameters and one with the body.
|
134
148
|
; Here's a function that adds 10 to its argument
|
135
149
|
(fn (x) (+ 10 x))
|
@@ -140,4 +154,60 @@ x ;; => [1, 2, 3]
|
|
140
154
|
; You can give it a name yourself
|
141
155
|
(def add-ten (fn (x) (+ 10 x)))
|
142
156
|
(add-ten 50) ;; => 60
|
157
|
+
|
158
|
+
(def square (fn (x) (* x x)))
|
159
|
+
(square 5) ;; => 25
|
160
|
+
|
161
|
+
; --- Count the amount of 5:s in a list ---
|
162
|
+
|
163
|
+
; The list we will operate on
|
164
|
+
(def fives (list 1 5 2 3 5 8 5)) ; 5 occurs 3 times.
|
165
|
+
|
166
|
+
(reduce ; Reduce takes a function(1), a starting value(2) and a list(3).
|
167
|
+
(fn (acc x) ; (1) The function in turn takes an accumulator and an item in the list.
|
168
|
+
(if (= x 5) ; If the item is five:
|
169
|
+
(inc acc) ; We increment the accumulator and move on to the next item.
|
170
|
+
acc)) ; Otherwise we return the total as is.
|
171
|
+
0 ; (2) We start counting from 0
|
172
|
+
fives) ; (3) We operate over the previously defined `fives` list.
|
173
|
+
;; => 3
|
174
|
+
|
175
|
+
; --- Print a beautiful pyramid ---
|
176
|
+
|
177
|
+
; Print one row in the pyramid
|
178
|
+
(def print-row
|
179
|
+
(fn
|
180
|
+
(length)
|
181
|
+
(println (* "#" length)))) ; We use the fact that the host platform (Ruby) can multiply strings
|
182
|
+
|
183
|
+
; Print the entire pyramid
|
184
|
+
(def print-pyramid
|
185
|
+
(fn
|
186
|
+
(height current-width)
|
187
|
+
(if (= current-width height)
|
188
|
+
nil
|
189
|
+
(do
|
190
|
+
(print-row current-width)
|
191
|
+
(print-pyramid height (inc current-width))))))
|
192
|
+
|
193
|
+
; Do it!
|
194
|
+
(print-pyramid 10 1)
|
195
|
+
;; #
|
196
|
+
;; ##
|
197
|
+
;; ###
|
198
|
+
;; ####
|
199
|
+
;; #####
|
200
|
+
;; ######
|
201
|
+
;; #######
|
202
|
+
;; ########
|
203
|
+
;; #########
|
204
|
+
|
205
|
+
|
206
|
+
; --- Interoperability ---
|
207
|
+
|
208
|
+
; The . function allows for Ruby interoperability.
|
209
|
+
(. "01011101" :to_i 2) ;; => 93
|
210
|
+
|
211
|
+
(def parse_binary (fn (bin) (. bin :to_i 2)))
|
212
|
+
(parse_binary "01011101") ;; => 93
|
143
213
|
```
|
data/lib/lasp/corelib.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
module Lasp
|
2
2
|
CORELIB = {
|
3
|
-
:+ => -> (_, *args)
|
4
|
-
:- => -> (_, *args)
|
5
|
-
:* => -> (_, *args)
|
6
|
-
:/ => -> (_, *args)
|
7
|
-
:< => -> (_, *args)
|
8
|
-
:> => -> (_, *args)
|
9
|
-
:"=" => -> (_, *args)
|
10
|
-
:list => -> (_, *args)
|
11
|
-
:head => -> (_, list)
|
12
|
-
:tail => -> (_, list)
|
13
|
-
:cons => -> (_, item, list)
|
14
|
-
:"hash-map" => -> (_, *args)
|
15
|
-
:get => -> (_, key, a)
|
16
|
-
:assoc => -> (_, a, key, val)
|
17
|
-
:dissoc => -> (_, a, key)
|
18
|
-
:not => -> (_, arg)
|
19
|
-
:println => -> (_, output)
|
20
|
-
:"." => -> (_, obj,
|
3
|
+
:+ => -> (_, *args) { args.reduce(:+) },
|
4
|
+
:- => -> (_, *args) { args.reduce(:-) },
|
5
|
+
:* => -> (_, *args) { args.reduce(:*) },
|
6
|
+
:/ => -> (_, *args) { args.reduce(:/) },
|
7
|
+
:< => -> (_, *args) { args.sort == args },
|
8
|
+
:> => -> (_, *args) { args.sort.reverse == args },
|
9
|
+
:"=" => -> (_, *args) { args.uniq.count == 1 },
|
10
|
+
:list => -> (_, *args) { args },
|
11
|
+
:head => -> (_, list) { list.first },
|
12
|
+
:tail => -> (_, list) { list.drop(1) },
|
13
|
+
:cons => -> (_, item, list) { [item] + list },
|
14
|
+
:"hash-map" => -> (_, *args) { Hash[*args] },
|
15
|
+
:get => -> (_, key, a) { a[key] },
|
16
|
+
:assoc => -> (_, a, key, val) { a.dup.tap { |a| a[key]=val } },
|
17
|
+
:dissoc => -> (_, a, key) { a.dup.tap { |a| a.delete(key) } },
|
18
|
+
:not => -> (_, arg) { !arg },
|
19
|
+
:println => -> (_, output) { puts output },
|
20
|
+
:"." => -> (_, obj, m, *args) { obj.send(m, *args) },
|
21
21
|
}
|
22
22
|
end
|
data/lib/lasp/eval.rb
CHANGED
@@ -23,7 +23,7 @@ module Lasp
|
|
23
23
|
# Use env from context to properly scope closures
|
24
24
|
-> (_, *args) { eval(func, env.merge(Hash[params.zip(args)])) }
|
25
25
|
elsif head == :do
|
26
|
-
tail.
|
26
|
+
tail.map { |form| eval(form, env) }.last
|
27
27
|
elsif head == :if
|
28
28
|
conditional, true_form, false_form = tail
|
29
29
|
eval(conditional, env) ? eval(true_form, env) : eval(false_form, env)
|
data/lib/lasp/version.rb
CHANGED