nydp 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d9a87eb0c014a51f391ddd1ba6686165a01ab48
4
- data.tar.gz: 8c3f3146c0ae8dc18ac6e86324274423d75be651
3
+ metadata.gz: 858b22d67ba13ec86efe8a00fd10b5fa813aee8c
4
+ data.tar.gz: 24876cc3c6e1389f656640d3548574d7241b3725
5
5
  SHA512:
6
- metadata.gz: f946a10304dd7422fb73243b9cd3ef40fab33aa9ff4f0b53568278761b64905d00714ed0cf7401db29b8c3613d8a683460ed48feef3f174ab0c724dd3e5f922e
7
- data.tar.gz: 1925438dd0eca5a718e6f41ff39ef9f35e1a9ed18279e58f2ebfa88c284cb46526f4864932db706dc346662a59cb153fbec70fca1047aeddc742740de971dab5
6
+ metadata.gz: 2c0094a86976f2272a70b9c5ea8b76e36face05f089ad9c7a0d51c3de251081230ce85d37e11f9ccaf23cd3d0433a655464d9193dfba37f1e1526fd929e03a13
7
+ data.tar.gz: 9eb38db6a571768e8da0bdddbd6420e7627f492748aa64aac0e8e48e08c9af416da84c5be0b8e3828559d1988de250f9f72eaa3d25166f5001c7a1896a9d8203
@@ -1,5 +1,24 @@
1
1
  (def privately () nil)
2
2
 
3
+ (assign comments nil)
4
+
5
+ (def fetch-and-clear-comments ()
6
+ ((fn (c) (assign comments nil) c) (rev comments)))
7
+
8
+ (def filter-comments (form acc)
9
+ (if (no form)
10
+ (rev acc)
11
+ (pair? form)
12
+ (filter-comments (cdr form)
13
+ (if (if (pair? (car form))
14
+ (eq? (caar form)
15
+ 'comment))
16
+ acc
17
+ (cons (filter-comments (car form)
18
+ nil)
19
+ acc)))
20
+ form))
21
+
3
22
  ((fn (this-chapter-name chapters chapter-new chapter-build chapter-add-to-chapter)
4
23
  (assign chapters (hash))
5
24
 
@@ -171,10 +190,10 @@
171
190
 
172
191
  (def define-def-expr (name args body-forms)
173
192
  ; used internally by 'def
174
- `(do (def-assign ,name (fun ,args ,@(hash-get body-forms nil)))
193
+ `(do (def-assign ,name (fun ,args ,@(filter-comments (hash-get body-forms nil))))
175
194
  (dox-add-doc ',name
176
195
  'def
177
- ',(map car (hash-get body-forms 'comment))
196
+ ',(+ (fetch-and-clear-comments) (map car (hash-get body-forms 'comment)))
178
197
  ',args
179
198
  '(def ,name ,args ,@(hash-get body-forms nil))
180
199
  ',(map car (hash-get body-forms 'chapter)))))
@@ -183,3 +202,7 @@
183
202
  ; define a new function in the global namespace
184
203
  (chapter nydp-core)
185
204
  (define-def-expr name args (filter-forms (build-def-hash (hash)) body)))
205
+
206
+ (mac comment (txt)
207
+ (assign comments (cons txt comments))
208
+ nil)
@@ -41,6 +41,9 @@
41
41
  (dox-add-doc 'inspect 'def '("return a string representing 'arg, potentially (but not always) in a way that can be parsed back in to get the original object") '(arg) nil '(nydp-core))
42
42
  (dox-add-doc 'comment 'def '("does nothing at all." "Intercepted inside 'def and 'mac and used for documentation") '(arg) nil '(nydp-core))
43
43
  (dox-add-doc 'parse-in-string 'def '("parse the given string assuming a string-open delimiter has already been encountered") '(str) nil '(nydp-core))
44
+ (dox-add-doc 'rand 'def '("return a random number ; with no args, between 0 and 1"
45
+ "with 1 arg, an integer less than arg"
46
+ "with two args, an integer >= arg 0 and < arg 1") 'args nil '(math))
44
47
  (dox-add-doc 'random-string 'def '("return a random string of length 'len (default 10)") '(len) nil '(string-manipulation))
45
48
  (dox-add-doc 'to-string 'def '("return a human-readable string representation of 'arg") '(arg) nil '(string-manipulation))
46
49
  (dox-add-doc 'string-length 'def '("return the length of 'arg") '(arg) nil '(string-manipulation))
@@ -78,6 +81,8 @@
78
81
  (dox-add-doc 'chapter-current 'def '("Get the name of the chapter in progress right now - this is normally the last value sent to 'chapter-start") nil nil '(nydp/documentation))
79
82
  (dox-add-doc 'chapter-delete 'def '("Remove the named chapter") '(name) nil '(nydp/documentation))
80
83
  (dox-add-doc 'chapter-find 'def '("Get the named chapter") '(name) nil '(nydp/documentation))
84
+ (dox-add-doc 'set-intersection 'def '("return the intersection of the given lists") 'args nil '(list-manipulation))
85
+ (dox-add-doc 'set-union 'def '("return the union of the given lists") 'args nil '(list-manipulation))
81
86
  (dox-add-doc 'dox-add-doc 'def '("Store the provided documentation item."
82
87
  "'name is the name of the item"
83
88
  "'what is the type of the item ('def or 'mac or 'thingy ... this is user-definable, not related to 'type-of)"
@@ -164,6 +164,19 @@
164
164
  (bm-or-lex-lex-lex 1 2 3)
165
165
  (bm-or-lex-lex-lex nil 2 3))
166
166
 
167
+ (def build-mapsum-data (count data)
168
+ (if (> count 0)
169
+ (build-mapsum-data (- count 1)
170
+ (cons { mappit (rand 10) }
171
+ data))
172
+ data))
173
+
174
+ (let mapsum-data (build-mapsum-data 1000)
175
+ (def bm-mapsum () (mapsum &mappit mapsum-data))
176
+ (def bm-mapreduce () (mapreduce &mappit + mapsum-data 0)))
177
+
178
+
179
+
167
180
  (def bm-repeat (f n)
168
181
  ; used in benchmarking
169
182
  (for b 1 n (f)))
@@ -184,7 +197,9 @@
184
197
 
185
198
  (def rbs (name)
186
199
  (let summary nil
187
- (push (bm "cond with OR " bm-faster-or 10 40000) summary)
200
+ (push (bm "mapsum " bm-mapsum 10 100) summary)
201
+ (push (bm "mapreduce " bm-mapreduce 10 100) summary)
202
+ ;; (push (bm "cond with OR " bm-faster-or 10 40000) summary)
188
203
  ;; (push (bm "cond with OR " bm-cond-9 10 40000) summary)
189
204
  ;; (push (bm "cond with OR " bm-cond-9 10 100000) summary)
190
205
  ;; (push (bm "cond with OR " bm-cond-lex-lit-lit 10 100000) summary)
@@ -15,13 +15,21 @@
15
15
  (map λx(hash-get tmp x)
16
16
  (sort:hash-keys tmp)))))
17
17
 
18
- (def mapsum (f things)
19
- ; map 'f over 'things and sum the resulting list
20
- (apply + 0.0 (map f things)))
21
-
22
- (def hash-values (h)
23
- ; return values for each key in hash 'h
24
- (map (fn (k) h.,k) (hash-keys h)))
18
+ (def mapreduce (fmap freduce things)
19
+ ; same as (reduce freduce (map fmap things))
20
+ ; returns the resulting list
21
+ (if (pair? things)
22
+ (freduce (fmap (car things))
23
+ (mapreduce fmap freduce (cdr things)))
24
+ things
25
+ (freduce (map fmap things))
26
+ (freduce)))
27
+
28
+ ; map 'f over 'things and sum the resulting list
29
+ (def mapsum (f things) (mapreduce f + things))
30
+
31
+ ; return values for each key in hash 'h
32
+ (def hash-values (h) (map (fn (k) h.,k) (hash-keys h)))
25
33
 
26
34
  (def seen? ()
27
35
  ; returns a new function f which takes a parameter x
@@ -33,9 +41,8 @@
33
41
  (let seen (hash)
34
42
  λx(returning seen.,x (= seen.,x t))))
35
43
 
36
- (def uniqify (things)
37
- ; return a list containing all the elements of 'things, but with no duplicates
38
- (reject (seen?) things))
44
+ ; return a list containing all the elements of 'things, but with no duplicates
45
+ (def uniqify (things) (reject (seen?) things))
39
46
 
40
47
  (def group-by (f things)
41
48
  ; return a hash of 'things keyed by (f thing) for
@@ -54,9 +61,8 @@
54
61
  (let mi (m2i anchor)
55
62
  (map λm(i2m (+ mi m)) mm))))
56
63
 
57
- (mac auto-hash names
58
- ; (auto-hash a b c) same as { a a b b c c }
59
- `(brace-list ,@(flatten:map λn(list n n) names)))
64
+ ; (auto-hash a b c) same as { a a b b c c }
65
+ (mac auto-hash names `(brace-list ,@(flatten:map λn(list n n) names)))
60
66
 
61
67
  (mac accum (accfn-name . body)
62
68
  (w/uniq acc
@@ -65,18 +71,19 @@
65
71
  ,@body
66
72
  (rev ,acc)))))
67
73
 
68
- (mac ++ (place inc)
69
- `(= ,place (+ ,place ,(or inc 1))))
74
+ ; increment the value at 'place by 'inc (default 1)
75
+ (mac ++ (place inc) `(= ,place (+ ,place ,(or inc 1))))
70
76
 
77
+ ; return a function that returns 'start on first invocation,
78
+ ; and 'start + n * 'incr for each nth invocation
71
79
  (def seqf (start incr)
72
80
  (let i (or incr 1)
73
81
  (fn () (returning start (++ start i)))))
74
82
 
75
- (def mapply (f args)
76
- ; like 'map, but assumes each item in 'args is a list
77
- ; of parameters for 'f. Effectively, calls (apply f item)
78
- ; for each item in 'args
79
- (map λa(apply f a) args))
83
+ ; like 'map, but assumes each item in 'args is a list
84
+ ; of parameters for 'f. Effectively, calls (apply f item)
85
+ ; for each item in 'args
86
+ (def mapply (f args) (map λa(apply f a) args))
80
87
 
81
88
  (mac def/cycler (name things)
82
89
  ; create a function called 'name ; each invocation of the function will
@@ -44,3 +44,33 @@
44
44
  (this)
45
45
  (that))))
46
46
  ((("hello") ("more details") ("very rigourous")) ((this) (that)))))
47
+
48
+ (examples-for filter-comments
49
+ ("returns nil for nil"
50
+ (filter-comments nil nil)
51
+ nil)
52
+
53
+ ("returns atom for atom"
54
+ (filter-comments 12 nil)
55
+ 12)
56
+
57
+ ("returns list for list"
58
+ (filter-comments '(a b c d) nil)
59
+ (a b c d))
60
+
61
+ ("returns list without comment for list with comment"
62
+ (filter-comments '(a b (comment "hahaha") c d) nil)
63
+ (a b c d))
64
+
65
+ ("returns nothing for comment"
66
+ (filter-comments '((comment "hahaha")) nil)
67
+ nil)
68
+
69
+ ("weeds out nested comments"
70
+ (filter-comments '(1 2 (3 4
71
+ (comment "yoyo") 5
72
+ (6 7 (comment "eight") 8 9)
73
+ { 10 (comment "eleven") 11 12 13 }
74
+ (comment "fourteen") 14) (comment "fifteen") 15
75
+ (if 16 (comment "seventeen") 17 (comment "eighteen") 18 19)))
76
+ (1 2 (3 4 5 (6 7 8 9) (brace-list 10 11 12 13) 14) 15 (if 16 17 18 19))))
@@ -0,0 +1,10 @@
1
+ (examples-for mapreduce
2
+ ("maps a function over a list and sums the result"
3
+ (mapreduce car + '((2 3) (5 7) (11 13)) 0)
4
+ 18)
5
+
6
+ ("extracts an attribute and sums the result"
7
+ (mapreduce &age + (list { name "a" age 10 }
8
+ { name "b" age 7 }
9
+ { name "c" age 12 }) 0)
10
+ 29))
@@ -0,0 +1,16 @@
1
+ (examples-for ⋂
2
+ ("returns a single arg"
3
+ (⋂ '(a b c))
4
+ (a b c))
5
+
6
+ ("returns intersection of two args"
7
+ (⋂ '(a b c) '(a b d))
8
+ (a b))
9
+
10
+ ("returns intersection of three args"
11
+ (⋂ '(a b c d e) '(a b c d f) '(z b c d g))
12
+ (b c d))
13
+
14
+ ("returns intersection of four args"
15
+ (⋂ '(a b c d e) '(a b c d f) '(z b d c g) '(z y g c d))
16
+ (c d)))
@@ -0,0 +1,16 @@
1
+ (examples-for ⋃
2
+ ("returns a single arg"
3
+ (⋃ '(a b c))
4
+ (a b c))
5
+
6
+ ("returns union of two args"
7
+ (⋃ '(a b c) '(a b d))
8
+ (a b c d))
9
+
10
+ ("returns union of three args"
11
+ (⋃ '(a b c d e) '(a b c d f) '(z b c d g))
12
+ (a b c d e f z g))
13
+
14
+ ("returns union of four args"
15
+ (⋃ '(a b c d e) '(a b c d f) '(z b d c g) '(z y g c d))
16
+ (a b c d e f z g y)))
@@ -0,0 +1,18 @@
1
+ class Nydp::Builtin::Rand
2
+ include Nydp::Builtin::Base, Singleton
3
+
4
+ def builtin_invoke_1 vm ; vm.push_arg rand ; end
5
+ def builtin_invoke_2 vm, a ; vm.push_arg rand(a) ; end
6
+ def builtin_invoke_3 vm, a0, a1 ; vm.push_arg(a0 + rand(a1 - a0)) ; end
7
+ def builtin_invoke vm, args ;
8
+ if Nydp::NIL.is? args
9
+ builtin_invoke_1 vm
10
+ else
11
+ case args.size
12
+ when 1 ; builtin_invoke_2 vm, args.car
13
+ when 2 ; builtin_invoke_3 vm, args.car, args.cadr
14
+ else ; raise "rand: 0, 1 or 2 args expected, got #{args.length}"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ class Nydp::Builtin::SetIntersection
2
+ include Nydp::Builtin::Base, Singleton
3
+
4
+ def builtin_invoke_2 vm, a ; vm.push_arg a ; end
5
+ def builtin_invoke_3 vm, a, b ; vm.push_arg(a & b) ; end
6
+ def builtin_invoke_4 vm, a, b, c ; vm.push_arg(a & b & c) ; end
7
+ def builtin_invoke vm, args ; vm.push_arg args.reduce &:& ; end
8
+ end
@@ -0,0 +1,8 @@
1
+ class Nydp::Builtin::SetUnion
2
+ include Nydp::Builtin::Base, Singleton
3
+
4
+ def builtin_invoke_2 vm, a ; vm.push_arg a ; end
5
+ def builtin_invoke_3 vm, a, b ; vm.push_arg(a | b) ; end
6
+ def builtin_invoke_4 vm, a, b, c ; vm.push_arg(a | b | c) ; end
7
+ def builtin_invoke vm, args ; vm.push_arg args.reduce &:| ; end
8
+ end
@@ -45,6 +45,7 @@ module Nydp
45
45
  Symbol.mk(:p, ns).assign(Nydp::Builtin::Puts.instance)
46
46
  Symbol.mk(:PI, ns).assign 3.1415
47
47
  Symbol.mk(:nil, ns).assign Nydp::NIL
48
+ Symbol.mk(:rand, ns).assign Nydp::Builtin::Rand.instance
48
49
  Symbol.mk(:sort, ns).assign Nydp::Builtin::Sort.instance
49
50
  Symbol.mk(:sqrt, ns).assign Nydp::Builtin::Sqrt.instance
50
51
  Symbol.mk(:t, ns).assign Nydp::T
@@ -74,11 +75,15 @@ module Nydp
74
75
  Symbol.mk(:"pre-compile" , ns).assign Nydp::Builtin::PreCompile.instance
75
76
  Symbol.mk(:"script-run" , ns).assign Nydp::Builtin::ScriptRun.instance
76
77
  Symbol.mk(:"**" , ns).assign Nydp::Builtin::MathPower.instance
77
- Symbol.mk(:"⌊" , ns).assign Nydp::Builtin::MathFloor.instance
78
- Symbol.mk(:"math-floor" , ns).assign Nydp::Builtin::MathFloor.instance
79
- Symbol.mk(:"⌈" , ns).assign Nydp::Builtin::MathCeiling.instance
80
- Symbol.mk(:"math-ceiling", ns).assign Nydp::Builtin::MathCeiling.instance
81
- Symbol.mk(:"math-round" , ns).assign Nydp::Builtin::MathRound.instance
78
+ Symbol.mk(:"⌊" , ns).assign Nydp::Builtin::MathFloor.instance
79
+ Symbol.mk(:"math-floor" , ns).assign Nydp::Builtin::MathFloor.instance
80
+ Symbol.mk(:"⌈" , ns).assign Nydp::Builtin::MathCeiling.instance
81
+ Symbol.mk(:"math-ceiling" , ns).assign Nydp::Builtin::MathCeiling.instance
82
+ Symbol.mk(:"math-round" , ns).assign Nydp::Builtin::MathRound.instance
83
+ Symbol.mk(:"⋂" , ns).assign Nydp::Builtin::SetIntersection.instance
84
+ Symbol.mk(:"set-intersection", ns).assign Nydp::Builtin::SetIntersection.instance
85
+ Symbol.mk(:"⋃" , ns).assign Nydp::Builtin::SetUnion.instance
86
+ Symbol.mk(:"set-union" , ns).assign Nydp::Builtin::SetUnion.instance
82
87
  end
83
88
  end
84
89
  end
@@ -22,6 +22,9 @@ class Nydp::Pair
22
22
  def + other ; copy.append other ; end
23
23
  def size ; 1 + (cdr.is_a?(Nydp::Pair) ? cdr.size : 0) ; end
24
24
  def inspect ; "(#{inspect_rest})" ; end
25
+ def & other ; self.class.from_list((Set.new(self) & other).to_a) ; end
26
+ def | other ; self.class.from_list((Set.new(self) | other).to_a) ; end
27
+ def proper? ; Nydp::NIL.is?(cdr) || (cdr.is_a?(Nydp::Pair) && cdr.proper?) ; end
25
28
 
26
29
  # returns Array of elements after calling #n2r on each element
27
30
  def to_ruby list=[]
@@ -55,10 +58,6 @@ class Nydp::Pair
55
58
  (NIL != other) && (other.respond_to? :car) && (self.car == other.car) && (self.cdr == other.cdr)
56
59
  end
57
60
 
58
- def proper?
59
- Nydp::NIL.is?(cdr) || (cdr.is_a?(Nydp::Pair) && cdr.proper?)
60
- end
61
-
62
61
  def each &block
63
62
  yield car
64
63
  cdr.each(&block) unless Nydp::NIL.is?(cdr)
@@ -1,3 +1,3 @@
1
1
  module Nydp
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -0,0 +1,45 @@
1
+ require "spec_helper"
2
+
3
+ describe Nydp::Builtin::Rand do
4
+ let(:vm) { Nydp::VM.new(ns) }
5
+
6
+ let(:randf) { Nydp::Builtin::Rand.instance }
7
+
8
+ def get_rand *args
9
+ randf.invoke vm, pair_list(args)
10
+ vm.pop_args(1).first
11
+ end
12
+
13
+ describe "zero args" do
14
+ it "returns values between 0 and 1" do
15
+ numbers = (0..100).map { |i| get_rand }
16
+ expect(numbers.all? { |n| n >= 0 && n < 1 })
17
+ avg = numbers.reduce &:+
18
+ expect(avg).to be_between 40, 60
19
+ distinct = Set.new numbers
20
+ expect(distinct.count).to be > 90
21
+ end
22
+ end
23
+
24
+ describe "one arg" do
25
+ it "returns values between 0 and arg" do
26
+ numbers = (0..200).map { |i| get_rand 10 }
27
+ expect(numbers.all? { |n| n >= 0 && n < 10 })
28
+ avg = numbers.reduce &:+
29
+ expect(avg).to be_between 800, 1200
30
+ distinct = Set.new numbers
31
+ expect(distinct.count).to eq 10
32
+ end
33
+ end
34
+
35
+ describe "two arg" do
36
+ it "returns values between arg 0 and arg 1" do
37
+ numbers = (0..200).map { |i| get_rand 10, 20 }
38
+ expect(numbers.all? { |n| n >= 10 && n < 20 })
39
+ avg = numbers.reduce &:+
40
+ expect(avg).to be_between 2800, 3200
41
+ distinct = Set.new numbers
42
+ expect(distinct.count).to eq 10
43
+ end
44
+ end
45
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nydp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Conan Dalton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-20 00:00:00.000000000 Z
11
+ date: 2017-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -147,6 +147,7 @@ files:
147
147
  - lib/lisp/tests/list-gsub-examples.nydp
148
148
  - lib/lisp/tests/list-match-examples.nydp
149
149
  - lib/lisp/tests/list-tests.nydp
150
+ - lib/lisp/tests/mapreduce-examples.nydp
150
151
  - lib/lisp/tests/mapsum-examples.nydp
151
152
  - lib/lisp/tests/none-examples.nydp
152
153
  - lib/lisp/tests/orequal-examples.nydp
@@ -162,6 +163,8 @@ files:
162
163
  - lib/lisp/tests/returning-examples.nydp
163
164
  - lib/lisp/tests/rfnwith-tests.nydp
164
165
  - lib/lisp/tests/seqf-examples.nydp
166
+ - lib/lisp/tests/set-intersection-examples.nydp
167
+ - lib/lisp/tests/set-union-examples.nydp
165
168
  - lib/lisp/tests/sort-examples.nydp
166
169
  - lib/lisp/tests/string-tests.nydp
167
170
  - lib/lisp/tests/syntax-tests.nydp
@@ -202,8 +205,11 @@ files:
202
205
  - lib/nydp/builtin/pre_compile.rb
203
206
  - lib/nydp/builtin/puts.rb
204
207
  - lib/nydp/builtin/quit.rb
208
+ - lib/nydp/builtin/rand.rb
205
209
  - lib/nydp/builtin/random_string.rb
206
210
  - lib/nydp/builtin/script_run.rb
211
+ - lib/nydp/builtin/set_intersection.rb
212
+ - lib/nydp/builtin/set_union.rb
207
213
  - lib/nydp/builtin/sort.rb
208
214
  - lib/nydp/builtin/sqrt.rb
209
215
  - lib/nydp/builtin/string_match.rb
@@ -256,6 +262,7 @@ files:
256
262
  - spec/nydp_spec.rb
257
263
  - spec/pair_spec.rb
258
264
  - spec/parser_spec.rb
265
+ - spec/rand_spec.rb
259
266
  - spec/spec_helper.rb
260
267
  - spec/string_atom_spec.rb
261
268
  - spec/string_token_spec.rb
@@ -298,6 +305,7 @@ test_files:
298
305
  - spec/nydp_spec.rb
299
306
  - spec/pair_spec.rb
300
307
  - spec/parser_spec.rb
308
+ - spec/rand_spec.rb
301
309
  - spec/spec_helper.rb
302
310
  - spec/string_atom_spec.rb
303
311
  - spec/string_token_spec.rb