Skip to content

Commit b638cdf

Browse files
swannodetteslipset
andcommitted
Port CLJ-2228: Improve performance of constantly
- Unroll constantly to arity 2 - Add test for constantly with four args Co-authored-by: Erik Assum <erik@assum.net>
1 parent 946d75f commit b638cdf

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

src/main/cljs/cljs/core.cljs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4462,7 +4462,11 @@ reduces them without incurring seq initialization"
44624462

44634463
(defn constantly
44644464
"Returns a function that takes any number of arguments and returns x."
4465-
[x] (fn [& args] x))
4465+
[x] (fn
4466+
([] x)
4467+
([_] x)
4468+
([_ _] x)
4469+
([_ _ & args] x)))
44664470

44674471
(defn comp
44684472
"Takes a set of functions and returns a fn that is the composition

src/test/cljs/cljs/other_functions_test.cljs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,14 @@
9696
(deftest test-constantly
9797
(let [c0 (constantly 10)]
9898
(are [x] (= 10 (c0 x))
99-
nil
100-
42
101-
"foo")))
99+
nil
100+
42
101+
"foo")
102+
(are [x] (= 10 (c0 x :a :b :c))
103+
nil
104+
42
105+
"foo")))
106+
102107
;juxt
103108

104109
(deftest test-juxt

0 commit comments

Comments
 (0)