Skip to content

Commit 463c776

Browse files
committed
[Fix #233] Rework the guidelines about complement, comp and partial
This reflects a recent discussion between the editor team and input from https://ask.clojure.org/index.php/8373/when-should-prefer-comp-and-partial-to-anonymous-functions
1 parent 7f31372 commit 463c776

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

README.adoc

+23-15
Original file line numberDiff line numberDiff line change
@@ -1511,25 +1511,23 @@ more than one form.
15111511
(* % 2))
15121512
----
15131513

1514-
=== `complement` [[complement]]
1514+
=== Anonymous Functions vs `complement`, `comp` and `partial`
15151515

1516-
Favor the use of `complement` versus the use of an anonymous function.
1516+
Prefer anonymous functions over `complement`, `comp` and `partial`, as this results
1517+
in simpler code most of the time.footnote:[You can read more on the subject https://ask.clojure.org/index.php/8373/when-should-prefer-comp-and-partial-to-anonymous-functions[here].]
1518+
1519+
==== `complement` [[complement]]
15171520

15181521
[source,clojure]
15191522
----
15201523
;; good
1521-
(filter (complement some-pred?) coll)
1522-
1523-
;; bad
15241524
(filter #(not (some-pred? %)) coll)
1525-
----
1526-
1527-
This rule should obviously be ignored if the complementing predicate
1528-
exists in the form of a separate function (e.g. `even?` and `odd?`).
15291525
1530-
=== `comp` [[comp]]
1526+
;; okish
1527+
(filter (complement some-pred?) coll)
1528+
----
15311529

1532-
Favor `comp` over anonymous functions for function composition.
1530+
==== `comp` [[comp]]
15331531

15341532
[source,clojure]
15351533
----
@@ -1538,20 +1536,30 @@ Favor `comp` over anonymous functions for function composition.
15381536
;; good
15391537
(map #(str/capitalize (str/trim %)) ["top " " test "])
15401538
1541-
;; better
1539+
;; okish
15421540
(map (comp str/capitalize str/trim) ["top " " test "])
15431541
----
15441542

1545-
=== `partial` [[partial]]
1543+
`comp` is quite useful when composing transducer chains, though.
1544+
1545+
[source,clojure]
1546+
----
1547+
;; good
1548+
(def xf
1549+
(comp
1550+
(filter odd?)
1551+
(map inc)
1552+
(take 5)))
1553+
----
15461554

1547-
Favor `partial` over anonymous functions.
1555+
==== `partial` [[partial]]
15481556

15491557
[source,clojure]
15501558
----
15511559
;; good
15521560
(map #(+ 5 %) (range 1 10))
15531561
1554-
;; (arguably) better
1562+
;; okish
15551563
(map (partial + 5) (range 1 10))
15561564
----
15571565

0 commit comments

Comments
 (0)