nendo 0.6.4 → 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,6 +32,37 @@
32
32
  ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
33
  ;;;
34
34
 
35
+ ;; for nendo.lazy
36
+ (define *ruby-lazy-enabled-platform*
37
+ (let ((arr (. Array new)))
38
+ (arr.respond_to? (. "lazy" to_sym)))) ;; Check the Emumerable#lazy method
39
+ (define (ruby-lazy-enabled-platform?) *ruby-lazy-enabled-platform*)
40
+
41
+
42
+ (define (ruby-lazy enumerable)
43
+ (if (and (ruby-lazy-enabled-platform?)
44
+ (enumerable.is_a? Enumerable))
45
+ (. enumerable lazy)
46
+ enumerable))
47
+
48
+ (define (ruby-lazy? v)
49
+ (if (ruby-lazy-enabled-platform?)
50
+ (v.is_a? Enumerator::Lazy)
51
+ #f))
52
+
53
+ (define (vector? v)
54
+ (or (v.is_a? Array)
55
+ (ruby-lazy? v)))
56
+
57
+ (define (vector->list v)
58
+ (let1 v (if (ruby-lazy? v)
59
+ (v.to_a)
60
+ v)
61
+ v.to_list))
62
+
63
+
64
+
65
+
35
66
 
36
67
  ;; Checking (Ruby's Enumerable?) and (not Nendo's list?)
37
68
  (define (%%enumerable? x)
@@ -88,10 +119,33 @@
88
119
  (%filter-original proc lst)))
89
120
 
90
121
 
122
+ (define (%%fold-able? proc knil lst)
123
+ (%%enumerable? lst))
124
+
125
+ (define (%%fold proc knil vec)
126
+ (vec.inject knil
127
+ (&block (result item)
128
+ (proc item result))))
129
+
130
+ (define (%fold-original proc knil lis1)
131
+ (define (null-list? l)
132
+ (cond ((pair? l) #f)
133
+ ((null? l) #t)
134
+ (else (error "null-list?: argument out of domain" l))))
135
+ (check-arg procedure? proc %fold-original)
136
+ (let lp ((lis lis1) (ans knil))
137
+ (if (null-list? lis) ans
138
+ (lp (cdr lis) (proc (car lis) ans)))))
139
+
140
+ (define (%fold proc knil lst)
141
+ (if (%%fold-able? proc knil lst)
142
+ (%%fold proc knil lst)
143
+ (%fold-original proc knil lst)))
144
+
91
145
  (define map %map)
92
146
  (define for-each %for-each)
93
147
  (define filter %filter)
94
-
148
+ (define fold %fold)
95
149
 
96
150
 
97
151
  ;; dis-assembler
@@ -197,3 +251,17 @@
197
251
  (raise nendo-syntax-error "let1 requires (let1 var expr body ...) form."))
198
252
  ((let1 var expr body ...)
199
253
  (%let ((var expr)) body ...))))
254
+
255
+
256
+ (define (readlines io)
257
+ (if (not (io.is_a? IO))
258
+ (raise ArgumentError "readlines expects IO object.")
259
+ (map
260
+ (lambda (x) (x.chomp))
261
+ (ruby-lazy (io.each_line)))))
262
+
263
+
264
+ (define (readchars io)
265
+ (if (not (io.is_a? IO))
266
+ (raise ArgumentError "readlines expects IO object.")
267
+ (ruby-lazy (io.each_char))))