Skip to content

Commit e86fc4d

Browse files
authored
Merge pull request #11 from Flexiana/feat/underscore-plus-alias
Add _+_ as terse alias for infix
2 parents 3a8ce47 + 7c62f51 commit e86fc4d

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/infix/core.clj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
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))"
@@ -154,6 +154,15 @@
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]

test/infix/alias_test.clj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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))))))

0 commit comments

Comments
 (0)