Skip to content

Commit

Permalink
filter and Draw Something word finder.
Browse files Browse the repository at this point in the history
  • Loading branch information
schani committed Jul 8, 2012
1 parent 94b34f5 commit a47e94f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/cljc/cljc/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,11 @@
false
true))

(defn ^boolean empty?
"Returns true if coll has no items - same as (not (seq coll)).
Please use the idiom (seq x) rather than (not (empty? x))"
[coll] (not (seq coll)))

(defn ^boolean set?
"Returns true if x satisfies ISet"
[x]
Expand Down Expand Up @@ -566,6 +571,16 @@

(defn identity [x] x)

(defn filter
"Returns a lazy sequence of the items in coll for which
(pred item) returns true. pred must be free of side-effects."
([pred coll]
(when-let [s (seq coll)]
(let [f (first s) r (rest s)]
(if (pred f)
(cons f (filter pred r))
(filter pred r))))))

(defn flatten-tail
[coll]
(if-let [n (next coll)]
Expand Down
21 changes: 21 additions & 0 deletions test/clojurec/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,25 @@
1 2 3 4 0 -1 1 2 3 4 0 5 -1 1 2 3 4 0 5 6 -1
1 2 3 4 5 0 -1 1 2 3 4 5 0 6 -1]))))

(deftest programs
(testing "somewhat useful programs"
(is (= (core-run '(do
(defn remove-one [coll o]
(loop [rev ()
coll (seq coll)]
(if coll
(let [f (first coll)]
(if (= o f)
(concat rev (next coll))
(recur (cons f rev) (next coll))))
rev)))

(defn check-word [len chars word]
(and (= len (count word))
(empty? (reduce remove-one word chars))))

(pr (filter (fn [word] (check-word 3 "abcdef" word))
(list "bad" "leg" "its" "gig" "gag" "bag" "fad")))))
['(bad fad)]))))

;;(run-tests *ns*)

0 comments on commit a47e94f

Please sign in to comment.