nydp 0.1.10 → 0.1.11

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6eec33bbcb86a9efa22b6e3fe9be0ab2f792e54c
4
- data.tar.gz: 61bf6dde8f734b52a82779be3b8127714cd4775c
3
+ metadata.gz: e297c2db504d03a1ae69c9b084c42d2967c409f6
4
+ data.tar.gz: 14ef550b291308d9fe1b5804a17e82733df7d46e
5
5
  SHA512:
6
- metadata.gz: 47dd8b9bdd75f050da422385256417a2ed334c1b988dbb718c53f2be91905174af5390a37817b9938eddc09b67b056e718310932fa00375f622240607a33f06c
7
- data.tar.gz: d947fab3747a487f5d99db30ae90186ee8b2424fb3370b8463567430afc94ff533c1ec51b7770ecf93aeed1ed919dcdbc5b28886e92487aa1547168187454252
6
+ metadata.gz: 9b0ee4357faaf3d0d4f2a7be37df48eac75877c3e717c32ac05ab1e57372d36764f2f1d185ba4acbba90e34bb5f73ea8c1165fc8db4d640762ee42bdaedf6985
7
+ data.tar.gz: 36bd8b57e1b6dfb3634129806931b5d0b4e69caaa7bff59f0540c68a34ad9fc18d96b11d360069a1f38aa474e279b5e75db54fd198bd10fb66553fc73519d760
@@ -1,3 +1,12 @@
1
+ (mac and args
2
+ ; returns last arg if all 'args evaluate to non-nil
3
+ ; nil otherwise
4
+ (if args
5
+ (if (cdr args)
6
+ `(if ,(car args) (and ,@(cdr args)))
7
+ (car args))
8
+ 't))
9
+
1
10
  (def orf args
2
11
  ; evaluates each arg in 'args, returns the
3
12
  ; first non-nil value, or nil if they are
@@ -8,6 +17,12 @@
8
17
  (apply orf
9
18
  (cdr args)))))
10
19
 
20
+ (def caris (obj things)
21
+ ; returns true if 'things is a list and the first item of the
22
+ ; list is the given object
23
+ (and (isa 'pair things)
24
+ (eq? (car things) obj)))
25
+
11
26
  (mac unless (arg . body)
12
27
  ; evaluate 'body if 'arg is nil
13
28
  `(if (no ,arg) (do ,@body)))
@@ -56,13 +71,6 @@
56
71
  `(colon-syntax no ,@(cdar rest))
57
72
  `(colon-syntax no ,(car rest)))))
58
73
 
59
- (mac and args
60
- (if args
61
- (if (cdr args)
62
- `(if ,(car args) (and ,@(cdr args)))
63
- (car args))
64
- 't))
65
-
66
74
  (mac when (condition . body)
67
75
  `(cond ,condition (do ,@body)))
68
76
 
@@ -209,3 +217,8 @@
209
217
  (no (cdr args))
210
218
  `(brace-list-mono ,(car args))
211
219
  (brace-list-build-hash args)))
220
+
221
+ (mac returning (val . body)
222
+ ; stores ,val, executes ,@body, and returns ,val.
223
+ (w/uniq retval
224
+ `(let ,retval ,val ,@body ,retval)))
@@ -53,6 +53,10 @@
53
53
  (f items)
54
54
  items)))
55
55
 
56
+ (def reject (f things)
57
+ ; return all the items in 'things for which 'f returns nil
58
+ (collect !f things))
59
+
56
60
  (def nth (n things)
57
61
  ; returns the n-th item in the list 'things
58
62
  (if (eq? n 0)
@@ -72,12 +76,6 @@
72
76
  (mac just (arg) arg)
73
77
  (def quotify (arg) `(quote ,arg))
74
78
 
75
- (def caris (obj things)
76
- ; returns true if 'things is a list and the first item of the
77
- ; list is the given object
78
- (and (isa 'pair things)
79
- (eq? (car things) obj)))
80
-
81
79
  (def list-length (things)
82
80
  (if (no things) 0
83
81
  (atom? things) 1
@@ -14,3 +14,25 @@
14
14
  (apply joinlists
15
15
  (map λx(hash-get tmp x)
16
16
  (sort:hash-keys tmp)))))
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)))
25
+
26
+ (def seen? ()
27
+ ; returns a new function f which takes a parameter x
28
+ ; for each call to f with any value Z for x
29
+ ; f returns true if this f has previously seen Z
30
+ ; f returns nil otherwise.
31
+ ; Note that each call to 'seen? returns a new function with
32
+ ; its own history independent of previous calls to 'seen?
33
+ (let seen (hash)
34
+ λx(returning seen.,x (= seen.,x t))))
35
+
36
+ (def uniqify (things)
37
+ ; return a list containing all the elements of 'things, but with no duplicates
38
+ (reject (seen?) things))
@@ -49,7 +49,10 @@
49
49
 
50
50
  ("adds 1 float" (+ 4.2) 4.2)
51
51
  ("adds 2 floats" (+ 4.2 4.5) 8.7)
52
- ("adds 3 floats" (+ 1.1 2.2 3.3) 6.6))
52
+ ("adds 3 floats" (+ 1.1 2.2 3.3) 6.6)
53
+
54
+ ("adds days" (let d (date 2015 11 8) (to-string (+ d 1))) "2015-11-09")
55
+ ("adds more days" (let d (date 2015 11 8) (to-string (+ d 10))) "2015-11-18"))
53
56
 
54
57
  (examples-for -
55
58
  ("subtracts nothing" (-) 0)
@@ -59,7 +62,13 @@
59
62
 
60
63
  ("subtracts 1 float" (- 4.2) -4.2)
61
64
  ("subtracts 2 floats" (- 4.2 4.7) -0.5)
62
- ("subtracts 3 floats" (- 6.6 1.1 3.3) 2.2))
65
+ ("subtracts 3 floats" (- 6.6 1.1 3.3) 2.2)
66
+
67
+ ("subtracts a day" (let d (date 2015 11 18) (to-string (- d 1))) "2015-11-17")
68
+ ("subtracts more days" (let d (date 2015 11 18) (to-string (- d 5))) "2015-11-13")
69
+
70
+ ("subtracts two dates" (- (date 2015 11 18) (date 2015 11 2)) 16)
71
+ ("subtracts two dates" (- (date 2015 11 18) (date 2015 12 25)) -37))
63
72
 
64
73
  (examples-for *
65
74
  ("multiplies nothing" (*) 1)
@@ -26,3 +26,36 @@
26
26
  (collect (fn (x) (eq? (len x) 20))
27
27
  "zz")
28
28
  nil))
29
+
30
+ (examples-for reject
31
+ ("returns all non-matching items in a list"
32
+ (reject (fn (x) (eq? (len x) 2))
33
+ (list "foo" "bar" "xx" "pp"))
34
+ ("foo" "bar"))
35
+
36
+ ("returns everything when nothing matches"
37
+ (reject (fn (x) (eq? (len x) 20))
38
+ (list "foo" "bar" "xx" "pp"))
39
+ ("foo" "bar" "xx" "pp"))
40
+
41
+ ;; kind of pointless
42
+ ("returns non-nils in list if nil is the matching item"
43
+ (reject no (list "foo" "bar" nil "pp" nil))
44
+ ("foo" "bar" "pp"))
45
+
46
+ ("returns item if it's an atom and doesn't match"
47
+ (reject (fn (x) (eq? (len x) 3)) "zz")
48
+ "zz")
49
+
50
+ ("preserves structure of improper list"
51
+ (reject (fn (x) (eq? (len x) 2)) '("aa" "bbb" "cc" "ddd" . "eee"))
52
+ ("bbb" "ddd" . "eee"))
53
+
54
+ ("ignores last-cons if matching"
55
+ (reject (fn (x) (eq? (len x) 2)) '("aa" "bbb" "cc" "ddd" . "ee"))
56
+ ("bbb" "ddd"))
57
+
58
+ ("returns thing if it's an atom and doesn't match"
59
+ (reject (fn (x) (eq? (len x) 20))
60
+ "zz")
61
+ "zz"))
@@ -36,4 +36,8 @@
36
36
  ("recognises friday" (let d (date 2015 11 6) d.friday? ) t )
37
37
  ("recognises not friday" (let d (date 2015 11 7) d.friday? ) nil )
38
38
  ("recognises saturday" (let d (date 2015 11 7) d.saturday? ) t )
39
- ("recognises sunday" (let d (date 2015 11 8) d.sunday? ) t ))
39
+ ("recognises sunday" (let d (date 2015 11 8) d.sunday? ) t )
40
+ ("adds days" (let d (date 2015 11 8) (to-string (+ d 1))) "2015-11-09")
41
+ ("adds more days" (let d (date 2015 11 8) (to-string (+ d 10))) "2015-11-18")
42
+ ("subtracts a day" (let d (date 2015 11 18) (to-string (- d 1))) "2015-11-17")
43
+ ("subtracts more days" (let d (date 2015 11 18) (to-string (- d 5))) "2015-11-13"))
@@ -1,3 +1,8 @@
1
+ (examples-for hash-values
2
+ ("returns the list of values of the given hash"
3
+ (hash-values { a 1 b 2 c 'c d "hello" })
4
+ (1 2 c "hello")))
5
+
1
6
  (examples-for hash?
2
7
  ("nil for a cons" (hash? '(1 a 2 b 3 c)) nil)
3
8
  ("nil for a number" (hash? 1) nil)
@@ -18,3 +18,26 @@
18
18
  ("nil for 42 in list not containing 42"
19
19
  (include? 42 '(1 2 nil 3 (a b c) 4))
20
20
  nil))
21
+
22
+ (examples-for seen?
23
+ ("returns a function which returns t for every previously-seen argument, nil otherwise"
24
+ (map (seen?) '(a b c d a a e f b b g h c c i j d d k l a b c d))
25
+ (nil nil nil nil t t nil nil t t nil nil t t nil nil t t nil nil t t t t))
26
+
27
+ ("each 'seen? is independent"
28
+ (with (s1 (seen?) s2 (seen?) syms '(a b c a d b e c f))
29
+ (list (map s1 syms) (map s2 syms)))
30
+ ((nil nil nil t nil t nil t nil)
31
+ (nil nil nil t nil t nil t nil))))
32
+
33
+ (examples-for uniqify
34
+ ("returns a list with duplicates removed"
35
+ (uniqify '(a b c d a a e f b b g h c c i j d d k l a b c d))
36
+ (a b c d e f g h i j k l))
37
+
38
+ ("removes duplicate dates"
39
+ (let d (date 2004 3 12)
40
+ (joinstr " "
41
+ (uniqify (map λx(+ d x)
42
+ '(0 2 3 3 4 3 5 0 5) ))))
43
+ "2004-03-12 2004-03-14 2004-03-15 2004-03-16 2004-03-17"))
@@ -0,0 +1,10 @@
1
+ (examples-for mapsum
2
+ ("maps a function over a list and sums the result"
3
+ (mapsum car '((2 3) (5 7) (11 13)))
4
+ 18)
5
+
6
+ ("extracts an attribute and sums the result"
7
+ (mapsum &age (list { name "a" age 10 }
8
+ { name "b" age 7 }
9
+ { name "c" age 12 }))
10
+ 29))
@@ -0,0 +1,6 @@
1
+ (examples-for returning
2
+ ("it stores a value, executes body, returns the stored value"
3
+ (let x 2
4
+ (returning x
5
+ (= x 3)))
6
+ 2))
data/lib/nydp/date.rb CHANGED
@@ -24,7 +24,6 @@ module Nydp
24
24
  def to_ruby ; ruby_date ; end
25
25
  def inspect ; ruby_date.inspect ; end
26
26
  def nydp_type ; :date ; end
27
- def - other ; r2n(ruby_date - other.ruby_date, nil) ; end
28
27
  def + int ; r2n(ruby_date + int , nil) ; end
29
28
  def > other ; ruby_date > other.ruby_date ; end
30
29
  def < other ; ruby_date < other.ruby_date ; end
@@ -33,6 +32,14 @@ module Nydp
33
32
  def eql? d ; self == d ; end
34
33
  def hash ; ruby_date.hash ; end
35
34
 
35
+ def - other
36
+ if other.is_a? Nydp::Date
37
+ r2n(ruby_date - other.ruby_date, nil)
38
+ else
39
+ r2n(ruby_date - other, nil)
40
+ end
41
+ end
42
+
36
43
  @@pass_through = %i{ monday? tuesday? wednesday? thursday? friday? saturday? sunday? }
37
44
  @@keys = Set.new %i{
38
45
  year month week_day day
data/lib/nydp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nydp
2
- VERSION = "0.1.10"
2
+ VERSION = "0.1.11"
3
3
  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.1.10
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Conan Dalton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-22 00:00:00.000000000 Z
11
+ date: 2016-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -123,11 +123,13 @@ files:
123
123
  - lib/lisp/tests/isa-examples.nydp
124
124
  - lib/lisp/tests/len-examples.nydp
125
125
  - lib/lisp/tests/list-tests.nydp
126
+ - lib/lisp/tests/mapsum-examples.nydp
126
127
  - lib/lisp/tests/parser-tests.nydp
127
128
  - lib/lisp/tests/pre-compile-examples.nydp
128
129
  - lib/lisp/tests/pretty-print-tests.nydp
129
130
  - lib/lisp/tests/quasiquote-examples.nydp
130
131
  - lib/lisp/tests/range-examples.nydp
132
+ - lib/lisp/tests/returning-examples.nydp
131
133
  - lib/lisp/tests/rfnwith-tests.nydp
132
134
  - lib/lisp/tests/sort-examples.nydp
133
135
  - lib/lisp/tests/string-tests.nydp