Merge branch 'juxt'

This commit is contained in:
Kodi Arfer 2016-12-26 13:28:56 -08:00
commit 7755778123
3 changed files with 48 additions and 9 deletions

View File

@ -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)
@ -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 applies each of the supplied functions to a
single set of arguments and collects the results 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}
=> ((juxt + - * /) 24 3)
[27, 21, 72, 8.0]
.. _keyword-fn:
keyword

View File

@ -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])

View File

@ -630,7 +630,7 @@
(defn test-complement []
"NATIVE: test complement"
(def helper (complement identity))
(assert-true (helper False))
(assert-false (helper True)))
@ -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]))