File tree 10 files changed +44
-34
lines changed
10 files changed +44
-34
lines changed Original file line number Diff line number Diff line change @@ -15,13 +15,13 @@ DEPENDENCIES
15
15
16
16
This version of the sample code has been tested with:
17
17
18
- Clojure SVN revision 1279
19
- svn co -r 1279 http://clojure.googlecode.com/svn/trunk/ clojure
18
+ Clojure SVN revision 1299
19
+ svn co -r 1299 http://clojure.googlecode.com/svn/trunk/ clojure
20
20
21
- Clojure-Contrib SVN revision 459 :
22
- svn co -r 459 http://clojure-contrib.googlecode.com/svn/trunk/ clojure-contrib
21
+ Clojure-Contrib SVN revision 505 :
22
+ svn co -r 505 http://clojure-contrib.googlecode.com/svn/trunk/ clojure-contrib
23
23
24
- Compojure GIT commit 0c8eff9
24
+ Compojure GIT commit 0fa49377
25
25
26
26
-------------------------------------------------------------------------------
27
27
You can run the completed Compojure example with
Original file line number Diff line number Diff line change 54
54
:else (fib [0 1 ] 2 ))))
55
55
; END: fibo-series
56
56
57
- ; START: lazy-cons-fibo
58
- (defn lazy-cons-fibo []
59
- ((fn fib [a b] ; <label id="code.lazy-cons.fib"/>
60
- (lazy-cons a (fib b (+ a b)))) ; <label id="code.lazy-cons.recur"/>
61
- 0 1 )) ; <label id="code.lazy-cons.basis"/>
62
- ; END: lazy-cons-fibo
57
+ ; START: lazy-seq-fibo
58
+ (defn lazy-seq-fibo
59
+ ([]
60
+ (concat [0 1 ] (lazy-seq-fibo 0 1 )))
61
+ ([a b]
62
+ (let [n (+ a b)]
63
+ (lazy-seq
64
+ (cons n (lazy-seq-fibo b n))))))
65
+ ; END: lazy-seq-fibo
63
66
64
67
; START: head-fibo
65
68
; holds the head (avoid!)
80
83
81
84
; START: by-pairs
82
85
(defn by-pairs [coll]
83
- (let [pair (take 2 coll)] ; <label id="code.by-pairs.take"/>
84
- (when (= 2 (count pair)) ; <label id="code.by-pairs.count"/>
85
- (lazy-cons pair (by-pairs (rest coll)))))) ; <label id="code.by-pairs.lazy"/>
86
+ (let [take-pair (fn take-pair [c]
87
+ (when (next c) (take 2 c)))]
88
+ (lazy-seq
89
+ (when-let [pair (seq (take-pair coll))]
90
+ (cons pair (by-pairs (rest coll)))))))
86
91
; END: by-pairs
87
92
88
93
; START: count-heads-by-pairs
Original file line number Diff line number Diff line change 1
1
(ns examples.lazy-index-of-any )
2
2
3
- (defn logging-seq [s]
4
- (if s
5
- (do (println " Iterating over" (first s))
6
- (lazy-cons (first s) (logging-seq (rest s))))))
3
+ (defn logging-seq [coll]
4
+ (lazy-seq
5
+ (when-let [s (seq coll)]
6
+ (do (println " Iterating over" (first s))
7
+ (cons (first s) (logging-seq (rest s)))))))
7
8
8
9
(defn indexed [s] (map vector (iterate inc 0 ) s (logging-seq s)))
9
10
(defn index-filter [pred coll]
Original file line number Diff line number Diff line change 6
6
(defmulti replace-symbol coll-or-scalar ) ; <label id="code.replace-symbol.multi"/>
7
7
8
8
(defmethod replace-symbol :collection [coll oldsym newsym]
9
- (if ( empty? coll)
10
- coll
11
- (lazy- cons (replace-symbol (first coll) oldsym newsym) ; <label id="code.replace-symbol.lazy-cons"/>
12
- (replace-symbol (rest coll) oldsym newsym))))
9
+ (lazy-seq
10
+ ( when-let [s ( seq coll)]
11
+ (cons (replace-symbol (first coll) oldsym newsym) ; <label id="code.replace-symbol.lazy-cons"/>
12
+ (replace-symbol (rest coll) oldsym newsym) ))))
13
13
14
14
(defmethod replace-symbol :scalar [obj oldsym newsym]
15
15
(if (= obj oldsym) newsym obj))
Original file line number Diff line number Diff line change 19
19
(deftest test-fibo-series
20
20
(is (= ten-fibs (fibo-series 10 ))))
21
21
22
- (deftest test-lazy-cons -fibo
23
- (is (= ten-fibs (take 10 (lazy-cons -fibo )))))
22
+ (deftest test-lazy-seq -fibo
23
+ (is (= ten-fibs (take 10 (lazy-seq -fibo )))))
24
24
25
25
(deftest test-head-fibo
26
26
(is (= ten-fibs (take 10 head-fibo))))
Original file line number Diff line number Diff line change @@ -5,10 +5,10 @@ public static boolean isBlank(String str) {
5
5
return true ;
6
6
}
7
7
for (int i = 0 ; i < strLen ; i ++) {
8
- if ((Character .isWhitespace (str .charAt (i )) == false )) {
9
- return false ;
10
- }
8
+ if ((Character .isWhitespace (str .charAt (i )) == false )) {
9
+ return false ;
10
+ }
11
11
}
12
12
return true ;
13
13
}
14
- }
14
+ }
You can’t perform that action at this time.
0 commit comments