Permalink
Browse files

Port validator from amalloy-utils at last

  • Loading branch information...
1 parent 612091d commit 561efc7dd10243eef6f7484e11d69610a3f39b6a @amalloy amalloy committed Nov 8, 2011
Showing with 15 additions and 0 deletions.
  1. +10 −0 src/useful/fn.clj
  2. +5 −0 test/useful/fn_test.clj
View
10 src/useful/fn.clj
@@ -2,6 +2,16 @@
(def ! complement)
+(defn validator
+ "Create a version of a predicate that only tests its output for truthiness,
+ returning the original input value if the predicate evaluates to anything
+ truthy, and nil otherwise. ((validator even?) 10) => 10, even though
+ (even? 10) is true."
+ [pred]
+ (fn [x]
+ (when (pred x)
+ x)))
+
(defn decorate
"Return a function f such that (f x) => [x (f1 x) (f2 x) ...]."
[& fs]
View
5 test/useful/fn_test.clj
@@ -1,6 +1,11 @@
(ns useful.fn-test
(:use clojure.test useful.fn))
+(deftest test-validator
+ (is (= [0 2 4 6 8]
+ (keep (validator even?)
+ (range 10)))))
+
(deftest test-decorate
(is (= [[1 2] [2 3] [3 4]] (map (decorate inc) [1 2 3]))))

0 comments on commit 561efc7

Please sign in to comment.