Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve the exercises #979

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: clojure
lein: lein2
script: lein2 midje :config .midje-grading-config.clj
lein: lein
script: lein midje :config .midje-grading-config.clj
jdk:
- openjdk7
notifications:
Expand Down
33 changes: 21 additions & 12 deletions src/predicates.clj
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
(ns predicates)

(defn sum-f [f g x]
:-)
(+ (f x) (g x)))

(defn less-than [n]
:-)
(fn [k] (< k n)))

(defn greater-than [n]
(fn [k] (> k n)))

(defn equal-to [n]
:-)
(fn [k] (= 0 (compare k n))))

(defn key->predicate [a-key]
(fn [a-map] (contains? a-map a-key)))

(defn set->predicate [a-set]
:-)
(fn [x] (contains? a-set x)))

(defn pred-and [pred1 pred2]
:-)
(fn [x] (and (pred1 x) (pred2 x))))

(defn pred-or [pred1 pred2]
:-)
(fn [x] (true? (or (pred1 x) (pred2 x)))))

(defn whitespace? [character]
(Character/isWhitespace character))

(defn blank? [string]
:-)
(or (every? whitespace? string)
(empty? string)
(nil? string)))

(defn has-award? [book award]
:-)
(contains? (book :awards) award))

(defn HAS-ALL-THE-AWARDS? [book awards]
:-)
(every? #(has-award? book %) awards))

(defn my-some [pred a-seq]
:-)
(first (filter #(not (false? %)) (map pred a-seq))))

(defn my-every? [pred a-seq]
:-)
(= (count a-seq) (count (filter #(not (false? %)) (map pred a-seq)))))

(defn prime? [n]
:-)
(let [divisible-by? (fn [k] (= 0 (mod n k)))]
(not (some divisible-by? (range 2 n)))))
;^^