From 2e8728498784e8b91f6d3dbcf678f31dc375503a Mon Sep 17 00:00:00 2001 From: Philip Xu Date: Sun, 25 Dec 2016 16:54:53 -0500 Subject: [PATCH 1/6] Add juxt --- hy/core/language.hy | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/hy/core/language.hy b/hy/core/language.hy index 1007aba..df9dbff 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -298,6 +298,13 @@ "Return true if x is an iterator" (isinstance x collections.Iterator)) +(defn juxt [f &rest fs] + "Return a function that apply a set of functions to same arguments and + collect the result into a list." + (setv fs (cons f fs)) + (fn [&rest args &kwargs kwargs] + (list-comp (apply f args kwargs) [f fs]))) + (defn last [coll] "Return last item from `coll`" (get (tuple coll) -1)) @@ -482,9 +489,9 @@ comp complement compress cons cons? constantly count cycle dec distinct disassemble drop drop-last drop-while empty? even? every? first filter flatten float? fraction gensym group-by identity inc input instance? - integer integer? integer-char? interleave interpose islice iterable? - iterate iterator? keyword keyword? last list* macroexpand macroexpand-1 - map merge-with multicombinations name neg? none? nth numeric? odd? - partition permutations pos? product range read read-str remove repeat - repeatedly rest reduce second some string string? symbol? take take-nth - take-while xor tee zero? zip zip-longest]) + integer integer? integer-char? interleave interpose islice iterable? + iterate iterator? juxt keyword keyword? last list* macroexpand + macroexpand-1 map merge-with multicombinations name neg? none? nth + numeric? odd? partition permutations pos? product range read read-str + remove repeat repeatedly rest reduce second some string string? symbol? + take take-nth take-while xor tee zero? zip zip-longest]) From f8a5c151f8e14050ec07969f38f6bb075bd1ebac Mon Sep 17 00:00:00 2001 From: Philip Xu Date: Sun, 25 Dec 2016 16:56:24 -0500 Subject: [PATCH 2/6] Remove trailing space --- tests/native_tests/core.hy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index b279abd..0d33f1e 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -630,7 +630,7 @@ (defn test-complement [] "NATIVE: test complement" (def helper (complement identity)) - + (assert-true (helper False)) (assert-false (helper True))) From eb8fe0b085162bbcdabf92ca911bceb08bbdbaf5 Mon Sep 17 00:00:00 2001 From: Philip Xu Date: Sun, 25 Dec 2016 16:57:50 -0500 Subject: [PATCH 3/6] Add test for juxt --- tests/native_tests/core.hy | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index 0d33f1e..2263f53 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -649,3 +649,10 @@ (assert-true ((comp even? inc +) 1 2 3 4 5)) (assert-true (= 5 ((comp) 5))) (assert (is (comp) identity))) + +(defn test-juxt [] + "NATIVE: test juxt" + (assert-equal ((juxt min max sum) [1 2 3 4 5 6]) + [1 6 21]) + (assert-equal ((juxt identity) 42) + [42])) From 24359336f78e42f515c9e8dfd5e3a5cc2e70efcb Mon Sep 17 00:00:00 2001 From: Philip Xu Date: Sun, 25 Dec 2016 16:59:41 -0500 Subject: [PATCH 4/6] Remove trailing whitespace --- docs/language/core.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/language/core.rst b/docs/language/core.rst index 76585e9..6de4af2 100644 --- a/docs/language/core.rst +++ b/docs/language/core.rst @@ -284,14 +284,14 @@ fraction Returns a Python object of type ``fractions.Fraction``. .. code-block:: hy - + => (fraction 1 2) Fraction(1, 2) Note that Hy has a built-in fraction literal that does the same thing: .. code-block:: hy - + => 1/2 Fraction(1, 2) From 7dfb9602f886dd3b81c913e3ccdb980b68257a94 Mon Sep 17 00:00:00 2001 From: Philip Xu Date: Sun, 25 Dec 2016 17:02:51 -0500 Subject: [PATCH 5/6] Add juxt documentation --- docs/language/core.rst | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/language/core.rst b/docs/language/core.rst index 6de4af2..3fc1c8e 100644 --- a/docs/language/core.rst +++ b/docs/language/core.rst @@ -505,6 +505,31 @@ themselves as an iterator when ``(iter x)`` is called. Contrast with => (iterator? (iter {:a 1 :b 2 :c 3})) True + +.. _juxt-fn: + +juxt +---- + +.. versionadded:: 0.12.0 + +Usage: ``(juxt f &rest fs)`` + +Return a function that apply a set of functions to same arguments +and collect the result into a list. + +.. code-block:: hy + + => ((juxt min max sum) (range 1 101)) + [1, 100, 5050] + + => (dict (map (juxt identity ord) "abcdef")) + {'f': 102, 'd': 100, 'b': 98, 'e': 101, 'c': 99, 'a': 97} + + => (setv [total difference product quotient] ((juxt + - * /) 24 3)) + [27, 21, 72, 8.0] + + .. _keyword-fn: keyword From 0c4d416a37bc7d4ee03dddd5720e4e6b62b35206 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Mon, 26 Dec 2016 13:26:44 -0800 Subject: [PATCH 6/6] juxt documentation edits --- docs/language/core.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/language/core.rst b/docs/language/core.rst index 3fc1c8e..3e9db35 100644 --- a/docs/language/core.rst +++ b/docs/language/core.rst @@ -515,8 +515,8 @@ juxt Usage: ``(juxt f &rest fs)`` -Return a function that apply a set of functions to same arguments -and collect the result into a list. +Return a function that applies each of the supplied functions to a +single set of arguments and collects the results into a list. .. code-block:: hy @@ -526,7 +526,7 @@ and collect the result into a list. => (dict (map (juxt identity ord) "abcdef")) {'f': 102, 'd': 100, 'b': 98, 'e': 101, 'c': 99, 'a': 97} - => (setv [total difference product quotient] ((juxt + - * /) 24 3)) + => ((juxt + - * /) 24 3) [27, 21, 72, 8.0]