From bfc71e4a60d0e8801039a0d20e7e7a06a70fc476 Mon Sep 17 00:00:00 2001 From: "Paul R. Tagliamonte" Date: Sun, 7 Apr 2013 15:05:30 -0400 Subject: [PATCH] Add tail threading (->>) --- hy/core/bootstrap.py | 14 ++++++++++++++ tests/native_tests/language.hy | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/hy/core/bootstrap.py b/hy/core/bootstrap.py index e77fb02..50efc66 100644 --- a/hy/core/bootstrap.py +++ b/hy/core/bootstrap.py @@ -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): diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index d41d20b..1044c41 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -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)