diff options
-rw-r--r-- | src/clojure/contrib/str_utils2.clj | 15 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/str_utils2.clj | 5 |
2 files changed, 19 insertions, 1 deletions
diff --git a/src/clojure/contrib/str_utils2.clj b/src/clojure/contrib/str_utils2.clj index 1fbd030d..9a1ea9f7 100644 --- a/src/clojure/contrib/str_utils2.clj +++ b/src/clojure/contrib/str_utils2.clj @@ -30,7 +30,7 @@ http://github.com/francoisdevlin/devlinsf-clojure-utils/"} clojure.contrib.str-utils2 (:refer-clojure :exclude (take replace drop butlast partition - contains? get repeat reverse)) + contains? get repeat reverse partial)) (:import (java.util.regex Pattern))) @@ -317,6 +317,19 @@ [re coll] (filter (fn [x] (re-find re (str x))) coll)) +(defn partial + "Like clojure.core/partial for functions that take their primary + argument first. + + Takes a function f and its arguments, NOT INCLUDING the first + argument. Returns a new function whose first argument will be the + first argument to f. + + Example: (str-utils2/partial str-utils2/take 2) + ;;=> (fn [s] (str-utils2/take s 2))" + [f & args] + (fn [s & more] (apply f s (concat args more)))) + ;;; WRAPPERS diff --git a/src/clojure/contrib/test_contrib/str_utils2.clj b/src/clojure/contrib/test_contrib/str_utils2.clj index 6d1f94d2..ee6aa68e 100644 --- a/src/clojure/contrib/test_contrib/str_utils2.clj +++ b/src/clojure/contrib/test_contrib/str_utils2.clj @@ -112,3 +112,8 @@ (deftest t-get (is (= \o (s/get "foo" 1)))) + +(deftest t-partial + (is (= "bar" ((s/partial s/drop 3) "foobar"))) + (is (= "ooba" ((comp (s/partial s/take 4) + (s/partial s/drop 1)) "foobar")))) |