aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStuart Sierra <mail@stuartsierra.com>2009-08-20 11:20:26 -0400
committerStuart Sierra <mail@stuartsierra.com>2009-08-20 11:20:26 -0400
commitef69dc58760232c75b55e91d405116aa5ed03d51 (patch)
treef43b7050486909382c052af835cf5def89d184ef
parent54a19a97e34c04dd7f9410e7683b13c240241458 (diff)
str_utils2.clj: added partial, with tests
Alternate definition of partial for fns that take their primary argument first.
-rw-r--r--src/clojure/contrib/str_utils2.clj15
-rw-r--r--src/clojure/contrib/test_contrib/str_utils2.clj5
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"))))