Add tail threading (->>)

This commit is contained in:
Paul R. Tagliamonte 2013-04-07 15:05:30 -04:00
parent 546e7ff50b
commit bfc71e4a60
2 changed files with 20 additions and 0 deletions

View File

@ -94,6 +94,20 @@ def threading_macro(tree):
return ret
@macro("_>>")
def threading_tail_macro(tree):
tree.pop(0)
ret = tree.pop(0)
for node in tree:
if not isinstance(node, HyExpression):
nnode = HyExpression([node])
nnode.replace(node)
node = nnode
node.append(ret)
ret = node
return ret
@macro("car")
@macro("first")
def first_macro(tree):

View File

@ -235,6 +235,12 @@
["X" "B" "C" "D"])))
(defn test-tail-threading []
"NATIVE: test tail threading macro"
(assert (= (.join ", " (* 10 ["foo"]))
(->> ["foo"] (* 10) (.join ", ")))))
(defn test-threading-two []
"NATIVE: test threading macro"
(assert (= (-> "a b c d" .upper (.replace "A" "X") .split)