File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 139139
140140(defmacro infix
141141 " Transform infix expressions into Clojure forms.
142-
142+
143143 Example:
144144 (infix a + b * c) => (+ a (* b c))
145145 (infix x => x * x) => (fn [x] (* x x))"
154154 parser/parse-infix
155155 compiler/compile-postfix)))
156156
157+ (defmacro _+_
158+ " Terse alias for `infix` — operand-operator-operand notation.
159+
160+ Example:
161+ (_+_ a + b * c) => (+ a (* b c))
162+ (_+_ x => x * x) => (fn [x] (* x x))"
163+ [& expr]
164+ `(infix ~@expr))
165+
157166(defn- process-binding-pairs
158167 " Process infix-let binding pairs, transforming RHS to infix."
159168 [bindings]
Original file line number Diff line number Diff line change 1+ (ns infix.alias-test
2+ (:require [clojure.test :refer [deftest is testing]]
3+ [infix.core :refer [_+_]]))
4+
5+ (deftest underscore-plus-alias
6+ (testing " _+_ behaves identically to infix for arithmetic"
7+ (is (= 7 (_+_ 3 + 4 )))
8+ (is (= 14 (_+_ 2 * 3 + 4 * 2 )))
9+ (is (= 25 (_+_ 5 * 5 ))))
10+
11+ (testing " _+_ supports comparison and boolean operators"
12+ (is (= true (_+_ 5 > 3 )))
13+ (is (= true (_+_ 1 < 2 and 2 < 3 ))))
14+
15+ (testing " _+_ supports arrow-lambda syntax"
16+ (is (= 25 ((_+_ x => x * x) 5 )))
17+ (is (= 7 ((_+_ (x y) => x + y) 3 4 ))))
18+
19+ (testing " _+_ supports nested fn(args) calls"
20+ (is (= 3 (_+_ count([1 2 3 ]))))
21+ (is (= 4 (_+_ count([1 2 3 ]) + 1 ))))
22+
23+ (testing " _+_ supports redundant grouping"
24+ (is (= 7 (_+_ ((((3 + 4 ))))))))
25+
26+ (testing " macroexpansion produces equivalent form to infix"
27+ (is (= (macroexpand '(infix.core/infix 3 + 4 ))
28+ (macroexpand '(infix.core/_+_ 3 + 4 ))))))
You can’t perform that action at this time.
0 commit comments