2014-12-06 21:28:28 +01:00
|
|
|
=======
|
2013-07-08 04:36:26 +02:00
|
|
|
Hy Core
|
2014-12-06 21:28:28 +01:00
|
|
|
=======
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
Core Functions
|
2014-12-06 21:28:28 +01:00
|
|
|
==============
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-08-22 11:51:12 +02:00
|
|
|
.. _butlast-fn:
|
|
|
|
|
|
|
|
butlast
|
|
|
|
-------
|
|
|
|
|
|
|
|
Usage: ``(butlast coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterator of all but the last item in *coll*.
|
2014-08-22 11:51:12 +02:00
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
=> (list (butlast (range 10)))
|
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8]
|
|
|
|
|
|
|
|
=> (list (butlast [1]))
|
|
|
|
[]
|
|
|
|
|
|
|
|
=> (list (butlast []))
|
|
|
|
[]
|
|
|
|
|
2017-01-05 00:11:54 +01:00
|
|
|
=> (list (take 5 (butlast (count 10))))
|
2014-08-22 11:51:12 +02:00
|
|
|
[10, 11, 12, 13, 14]
|
|
|
|
|
|
|
|
|
2014-01-12 11:22:14 +01:00
|
|
|
.. _is-coll-fn:
|
|
|
|
|
|
|
|
coll?
|
2014-01-16 13:08:18 +01:00
|
|
|
-----
|
2014-01-12 11:22:14 +01:00
|
|
|
|
2014-04-10 20:21:32 +02:00
|
|
|
.. versionadded:: 0.10.0
|
2014-01-12 11:22:14 +01:00
|
|
|
|
|
|
|
Usage: ``(coll? x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if *x* is iterable and not a string.
|
2014-01-12 11:22:14 +01:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2014-01-12 11:22:14 +01:00
|
|
|
|
|
|
|
=> (coll? [1 2 3 4])
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (coll? {"a" 1 "b" 2})
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (coll? "abc")
|
|
|
|
False
|
|
|
|
|
|
|
|
|
2016-12-25 21:11:25 +01:00
|
|
|
.. _comp:
|
|
|
|
|
|
|
|
comp
|
|
|
|
----
|
|
|
|
|
|
|
|
Usage: ``(comp f g)``
|
|
|
|
|
|
|
|
Compose zero or more functions into a new function. The new function will
|
|
|
|
chain the given functions together, so ``((comp g f) x)`` is equivalent to
|
|
|
|
``(g (f x))``. Called without arguments, ``comp`` returns ``identity``.
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
2018-01-05 21:51:00 +01:00
|
|
|
=> (setv example (comp str +))
|
2016-12-25 21:11:25 +01:00
|
|
|
=> (example 1 2 3)
|
|
|
|
"6"
|
|
|
|
|
2018-01-05 21:51:00 +01:00
|
|
|
=> (setv simple (comp))
|
2016-12-25 21:11:25 +01:00
|
|
|
=> (simple "hello")
|
|
|
|
"hello"
|
|
|
|
|
|
|
|
|
|
|
|
.. _complement:
|
|
|
|
|
|
|
|
complement
|
|
|
|
----------
|
|
|
|
|
|
|
|
.. versionadded:: 0.12.0
|
|
|
|
|
|
|
|
Usage: ``(complement f)``
|
|
|
|
|
|
|
|
Returns a new function that returns the same thing as ``f``, but logically
|
|
|
|
inverted. So, ``((complement f) x)`` is equivalent to ``(not (f x))``.
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
2018-01-05 21:51:00 +01:00
|
|
|
=> (setv inverse (complement identity))
|
2016-12-25 21:11:25 +01:00
|
|
|
=> (inverse True)
|
|
|
|
False
|
|
|
|
=> (inverse 1)
|
|
|
|
False
|
|
|
|
=> (inverse False)
|
|
|
|
True
|
|
|
|
|
|
|
|
|
2013-05-16 18:59:20 +02:00
|
|
|
cons
|
|
|
|
----
|
|
|
|
|
2014-04-10 20:21:32 +02:00
|
|
|
.. versionadded:: 0.10.0
|
2013-05-16 18:59:20 +02:00
|
|
|
|
|
|
|
Usage: ``(cons a b)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns a fresh :ref:`cons cell <hycons>` with car *a* and cdr *b*.
|
2013-05-16 18:59:20 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-05-16 18:59:20 +02:00
|
|
|
|
|
|
|
=> (setv a (cons 'hd 'tl))
|
|
|
|
|
2017-03-06 17:34:40 +01:00
|
|
|
=> (= 'hd (get a 0))
|
2013-05-16 18:59:20 +02:00
|
|
|
True
|
|
|
|
|
2017-03-06 17:34:40 +01:00
|
|
|
=> (= 'tl (cut a 1))
|
2013-05-16 18:59:20 +02:00
|
|
|
True
|
|
|
|
|
|
|
|
|
|
|
|
cons?
|
|
|
|
-----
|
|
|
|
|
2014-04-10 20:21:32 +02:00
|
|
|
.. versionadded:: 0.10.0
|
2013-05-16 18:59:20 +02:00
|
|
|
|
|
|
|
Usage: ``(cons? foo)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Checks whether *foo* is a :ref:`cons cell <hycons>`.
|
2013-05-16 18:59:20 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-05-16 18:59:20 +02:00
|
|
|
|
|
|
|
=> (setv a (cons 'hd 'tl))
|
|
|
|
|
|
|
|
=> (cons? a)
|
|
|
|
True
|
|
|
|
|
2016-11-24 03:35:17 +01:00
|
|
|
=> (cons? None)
|
2013-05-16 18:59:20 +02:00
|
|
|
False
|
|
|
|
|
|
|
|
=> (cons? [1 2 3])
|
|
|
|
False
|
|
|
|
|
2016-12-25 21:11:25 +01:00
|
|
|
|
|
|
|
.. _constantly:
|
|
|
|
|
|
|
|
constantly
|
|
|
|
----------
|
|
|
|
|
|
|
|
.. versionadded:: 0.12.0
|
|
|
|
|
|
|
|
Usage ``(constantly 42)``
|
|
|
|
|
|
|
|
Create a new function that always returns the given value, regardless of
|
|
|
|
the arguments given to it.
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
2018-01-05 21:51:00 +01:00
|
|
|
=> (setv answer (constantly 42))
|
2016-12-25 21:11:25 +01:00
|
|
|
=> (answer)
|
|
|
|
42
|
|
|
|
=> (answer 1 2 3)
|
|
|
|
42
|
|
|
|
=> (answer 1 :foo 2)
|
|
|
|
42
|
|
|
|
|
|
|
|
|
2013-07-08 04:36:26 +02:00
|
|
|
.. _dec-fn:
|
|
|
|
|
|
|
|
dec
|
|
|
|
---
|
|
|
|
|
|
|
|
Usage: ``(dec x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns one less than *x*. Equivalent to ``(- x 1)``. Raises ``TypeError``
|
|
|
|
if ``(not (numeric? x))``.
|
2013-07-29 18:40:48 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (dec 3)
|
|
|
|
2
|
|
|
|
|
|
|
|
=> (dec 0)
|
|
|
|
-1
|
|
|
|
|
|
|
|
=> (dec 12.3)
|
|
|
|
11.3
|
|
|
|
|
|
|
|
|
2014-01-05 14:42:19 +01:00
|
|
|
.. _disassemble-fn:
|
|
|
|
|
|
|
|
disassemble
|
|
|
|
-----------
|
|
|
|
|
2014-04-10 20:21:32 +02:00
|
|
|
.. versionadded:: 0.10.0
|
2014-01-05 15:25:41 +01:00
|
|
|
|
2014-01-05 14:42:19 +01:00
|
|
|
Usage: ``(disassemble tree &optional [codegen false])``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Dump the Python AST for given Hy *tree* to standard output. If *codegen*
|
|
|
|
is ``True``, the function prints Python code instead.
|
2014-01-05 14:42:19 +01:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2014-01-05 14:42:19 +01:00
|
|
|
|
|
|
|
=> (disassemble '(print "Hello World!"))
|
|
|
|
Module(
|
|
|
|
body=[
|
|
|
|
Expr(value=Call(func=Name(id='print'), args=[Str(s='Hello World!')], keywords=[], starargs=None, kwargs=None))])
|
|
|
|
|
2016-11-24 03:35:17 +01:00
|
|
|
=> (disassemble '(print "Hello World!") True)
|
2014-01-05 14:42:19 +01:00
|
|
|
print('Hello World!')
|
|
|
|
|
|
|
|
|
2016-06-11 01:04:07 +02:00
|
|
|
.. _empty?-fn:
|
2013-07-29 18:40:48 +02:00
|
|
|
|
|
|
|
empty?
|
|
|
|
------
|
|
|
|
|
|
|
|
Usage: ``(empty? coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if *coll* is empty. Equivalent to ``(= 0 (len coll))``.
|
2013-07-29 18:40:48 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-29 18:40:48 +02:00
|
|
|
|
|
|
|
=> (empty? [])
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (empty? "")
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (empty? (, 1 2))
|
|
|
|
False
|
|
|
|
|
|
|
|
|
2017-06-22 20:23:16 +02:00
|
|
|
.. _eval-fn:
|
|
|
|
|
|
|
|
eval
|
|
|
|
----
|
|
|
|
|
|
|
|
``eval`` evaluates a quoted expression and returns the value. The optional
|
|
|
|
second and third arguments specify the dictionary of globals to use and the
|
|
|
|
module name. The globals dictionary defaults to ``(local)`` and the module name
|
|
|
|
defaults to the name of the current module.
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
=> (eval '(print "Hello World"))
|
|
|
|
"Hello World"
|
|
|
|
|
|
|
|
If you want to evaluate a string, use ``read-str`` to convert it to a
|
|
|
|
form first:
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
=> (eval (read-str "(+ 1 1)"))
|
|
|
|
2
|
|
|
|
|
|
|
|
|
2014-02-11 09:42:56 +01:00
|
|
|
.. _every?-fn:
|
|
|
|
|
|
|
|
every?
|
|
|
|
------
|
|
|
|
|
2014-04-10 20:21:32 +02:00
|
|
|
.. versionadded:: 0.10.0
|
2014-02-11 09:42:56 +01:00
|
|
|
|
|
|
|
Usage: ``(every? pred coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if ``(pred x)`` is logical true for every *x* in *coll*,
|
|
|
|
otherwise ``False``. Return ``True`` if *coll* is empty.
|
2014-02-11 09:42:56 +01:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2014-02-11 09:42:56 +01:00
|
|
|
|
|
|
|
=> (every? even? [2 4 6])
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (every? even? [1 3 5])
|
|
|
|
False
|
|
|
|
|
|
|
|
=> (every? even? [2 4 5])
|
|
|
|
False
|
|
|
|
|
|
|
|
=> (every? even? [])
|
|
|
|
True
|
|
|
|
|
|
|
|
|
2017-07-25 19:28:42 +02:00
|
|
|
.. _exec-fn:
|
|
|
|
|
|
|
|
exec
|
|
|
|
----
|
|
|
|
|
|
|
|
Equivalent to Python 3's built-in function :py:func:`exec`.
|
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
=> (exec "print(a + b)" {"a" 1} {"b" 2})
|
|
|
|
3
|
|
|
|
|
|
|
|
|
2013-07-29 18:40:48 +02:00
|
|
|
.. _float?-fn:
|
|
|
|
|
|
|
|
float?
|
|
|
|
-------
|
|
|
|
|
|
|
|
Usage: ``(float? x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if *x* is a float.
|
2013-07-29 18:40:48 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-29 18:40:48 +02:00
|
|
|
|
|
|
|
=> (float? 3.2)
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (float? -2)
|
|
|
|
False
|
|
|
|
|
|
|
|
|
2015-06-26 23:10:22 +02:00
|
|
|
.. _fraction-fn:
|
|
|
|
|
|
|
|
fraction
|
|
|
|
--------
|
|
|
|
|
|
|
|
Returns a Python object of type ``fractions.Fraction``.
|
|
|
|
|
|
|
|
.. code-block:: hy
|
2016-12-25 22:59:41 +01:00
|
|
|
|
2015-06-26 23:10:22 +02:00
|
|
|
=> (fraction 1 2)
|
|
|
|
Fraction(1, 2)
|
|
|
|
|
|
|
|
Note that Hy has a built-in fraction literal that does the same thing:
|
|
|
|
|
|
|
|
.. code-block:: hy
|
2016-12-25 22:59:41 +01:00
|
|
|
|
2015-06-26 23:10:22 +02:00
|
|
|
=> 1/2
|
|
|
|
Fraction(1, 2)
|
|
|
|
|
|
|
|
|
2013-07-08 04:36:26 +02:00
|
|
|
.. _even?-fn:
|
|
|
|
|
|
|
|
even?
|
|
|
|
-----
|
|
|
|
|
|
|
|
Usage: ``(even? x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if *x* is even. Raises ``TypeError`` if
|
|
|
|
``(not (numeric? x))``.
|
2013-07-29 18:40:48 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (even? 2)
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (even? 13)
|
|
|
|
False
|
|
|
|
|
|
|
|
=> (even? 0)
|
|
|
|
True
|
|
|
|
|
|
|
|
|
2014-01-21 20:19:49 +01:00
|
|
|
.. _identity-fn:
|
|
|
|
|
|
|
|
identity
|
|
|
|
--------
|
|
|
|
|
|
|
|
Usage: ``(identity x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns the argument supplied to the function.
|
2014-01-21 20:19:49 +01:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2014-01-21 20:19:49 +01:00
|
|
|
|
|
|
|
=> (identity 4)
|
|
|
|
4
|
|
|
|
|
|
|
|
=> (list (map identity [1 2 3 4]))
|
|
|
|
[1 2 3 4]
|
|
|
|
|
|
|
|
|
2013-07-08 04:36:26 +02:00
|
|
|
.. _inc-fn:
|
|
|
|
|
|
|
|
inc
|
|
|
|
---
|
|
|
|
|
|
|
|
Usage: ``(inc x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns one more than *x*. Equivalent to ``(+ x 1)``. Raises ``TypeError``
|
|
|
|
if ``(not (numeric? x))``.
|
2013-07-29 18:40:48 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (inc 3)
|
|
|
|
4
|
|
|
|
|
|
|
|
=> (inc 0)
|
|
|
|
1
|
|
|
|
|
|
|
|
=> (inc 12.3)
|
|
|
|
13.3
|
|
|
|
|
|
|
|
|
|
|
|
.. _instance?-fn:
|
|
|
|
|
|
|
|
instance?
|
|
|
|
---------
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Usage: ``(instance? class x)``
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if *x* is an instance of *class*.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (instance? float 1.0)
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (instance? int 7)
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (instance? str (str "foo"))
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (defclass TestClass [object])
|
|
|
|
=> (setv inst (TestClass))
|
|
|
|
=> (instance? TestClass inst)
|
|
|
|
True
|
|
|
|
|
2013-07-29 18:40:48 +02:00
|
|
|
.. _integer?-fn:
|
|
|
|
|
|
|
|
integer?
|
|
|
|
--------
|
|
|
|
|
|
|
|
Usage: ``(integer? x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns `True` if *x* is an integer. For Python 2, this is
|
2013-07-29 18:40:48 +02:00
|
|
|
either ``int`` or ``long``. For Python 3, this is ``int``.
|
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-29 18:40:48 +02:00
|
|
|
|
|
|
|
=> (integer? 3)
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (integer? -2.4)
|
|
|
|
False
|
|
|
|
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-08-17 04:53:57 +02:00
|
|
|
.. _interleave-fn:
|
|
|
|
|
|
|
|
interleave
|
|
|
|
----------
|
|
|
|
|
|
|
|
.. versionadded:: 0.10.1
|
|
|
|
|
|
|
|
Usage: ``(interleave seq1 seq2 ...)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterable of the first item in each of the sequences,
|
2014-12-06 21:28:28 +01:00
|
|
|
then the second, etc.
|
2014-08-17 04:53:57 +02:00
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
=> (list (interleave (range 5) (range 100 105)))
|
|
|
|
[0, 100, 1, 101, 2, 102, 3, 103, 4, 104]
|
|
|
|
|
|
|
|
=> (list (interleave (range 1000000) "abc"))
|
|
|
|
[0, 'a', 1, 'b', 2, 'c']
|
|
|
|
|
|
|
|
|
|
|
|
.. _interpose-fn:
|
|
|
|
|
|
|
|
interpose
|
|
|
|
---------
|
|
|
|
|
|
|
|
.. versionadded:: 0.10.1
|
|
|
|
|
|
|
|
Usage: ``(interpose item seq)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterable of the elements of the sequence separated by the item.
|
2014-08-17 04:53:57 +02:00
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
=> (list (interpose "!" "abcd"))
|
|
|
|
['a', '!', 'b', '!', 'c', '!', 'd']
|
|
|
|
|
|
|
|
=> (list (interpose -1 (range 5)))
|
|
|
|
[0, -1, 1, -1, 2, -1, 3, -1, 4]
|
|
|
|
|
|
|
|
|
2013-07-08 04:36:26 +02:00
|
|
|
.. _iterable?-fn:
|
|
|
|
|
|
|
|
iterable?
|
|
|
|
---------
|
|
|
|
|
|
|
|
Usage: ``(iterable? x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if *x* is iterable. Iterable objects return a new iterator
|
2013-07-08 04:36:26 +02:00
|
|
|
when ``(iter x)`` is called. Contrast with :ref:`iterator?-fn`.
|
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> ;; works for strings
|
|
|
|
=> (iterable? (str "abcde"))
|
|
|
|
True
|
|
|
|
|
|
|
|
=> ;; works for lists
|
|
|
|
=> (iterable? [1 2 3 4 5])
|
|
|
|
True
|
|
|
|
|
|
|
|
=> ;; works for tuples
|
|
|
|
=> (iterable? (, 1 2 3))
|
|
|
|
True
|
|
|
|
|
|
|
|
=> ;; works for dicts
|
|
|
|
=> (iterable? {:a 1 :b 2 :c 3})
|
|
|
|
True
|
|
|
|
|
|
|
|
=> ;; works for iterators/generators
|
|
|
|
=> (iterable? (repeat 3))
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
|
|
.. _iterator?-fn:
|
|
|
|
|
|
|
|
iterator?
|
|
|
|
---------
|
|
|
|
|
|
|
|
Usage: ``(iterator? x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if *x* is an iterator. Iterators are objects that return
|
|
|
|
themselves as an iterator when ``(iter x)`` is called. Contrast with
|
|
|
|
:ref:`iterable?-fn`.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> ;; doesn't work for a list
|
|
|
|
=> (iterator? [1 2 3 4 5])
|
|
|
|
False
|
|
|
|
|
|
|
|
=> ;; but we can get an iter from the list
|
|
|
|
=> (iterator? (iter [1 2 3 4 5]))
|
|
|
|
True
|
|
|
|
|
|
|
|
=> ;; doesn't work for dict
|
|
|
|
=> (iterator? {:a 1 :b 2 :c 3})
|
|
|
|
False
|
|
|
|
|
|
|
|
=> ;; create an iterator from the dict
|
|
|
|
=> (iterator? (iter {:a 1 :b 2 :c 3}))
|
|
|
|
True
|
|
|
|
|
2016-12-25 23:02:51 +01:00
|
|
|
|
|
|
|
.. _juxt-fn:
|
|
|
|
|
|
|
|
juxt
|
|
|
|
----
|
|
|
|
|
|
|
|
.. versionadded:: 0.12.0
|
|
|
|
|
|
|
|
Usage: ``(juxt f &rest fs)``
|
|
|
|
|
2016-12-26 22:26:44 +01:00
|
|
|
Return a function that applies each of the supplied functions to a
|
|
|
|
single set of arguments and collects the results into a list.
|
2016-12-25 23:02:51 +01:00
|
|
|
|
|
|
|
.. 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}
|
|
|
|
|
2016-12-26 22:26:44 +01:00
|
|
|
=> ((juxt + - * /) 24 3)
|
2016-12-25 23:02:51 +01:00
|
|
|
[27, 21, 72, 8.0]
|
|
|
|
|
|
|
|
|
2015-10-17 13:56:35 +02:00
|
|
|
.. _keyword-fn:
|
|
|
|
|
|
|
|
keyword
|
|
|
|
-------
|
|
|
|
|
|
|
|
.. versionadded:: 0.10.1
|
|
|
|
|
|
|
|
Usage: ``(keyword "foo")``
|
|
|
|
|
|
|
|
Create a keyword from the given value. Strings, numbers, and even
|
|
|
|
objects with the `__name__` magic will work.
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
=> (keyword "foo")
|
|
|
|
u'\ufdd0:foo'
|
|
|
|
|
|
|
|
=> (keyword 1)
|
|
|
|
u'\ufdd0:1'
|
|
|
|
|
2015-08-31 21:35:37 +02:00
|
|
|
.. _keyword?-fn:
|
|
|
|
|
|
|
|
keyword?
|
|
|
|
--------
|
|
|
|
|
|
|
|
.. versionadded:: 0.10.1
|
|
|
|
|
|
|
|
Usage: ``(keyword? foo)``
|
|
|
|
|
|
|
|
Check whether *foo* is a :ref:`keyword<HyKeyword>`.
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
=> (keyword? :foo)
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (setv foo 1)
|
|
|
|
=> (keyword? foo)
|
|
|
|
False
|
2014-12-06 21:32:11 +01:00
|
|
|
|
|
|
|
.. _list*-fn:
|
|
|
|
|
2013-05-16 18:59:20 +02:00
|
|
|
list*
|
|
|
|
-----
|
|
|
|
|
|
|
|
Usage: ``(list* head &rest tail)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Generates a chain of nested cons cells (a dotted list) containing the
|
2013-05-16 18:59:20 +02:00
|
|
|
arguments. If the argument list only has one element, return it.
|
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-05-16 18:59:20 +02:00
|
|
|
|
2017-08-26 06:50:13 +02:00
|
|
|
=> (list* 1 2 3 4)
|
|
|
|
<HyCons (
|
|
|
|
HyInteger(1)
|
|
|
|
HyInteger(2)
|
|
|
|
HyInteger(3)
|
|
|
|
. HyInteger(4))>
|
|
|
|
=> (list* 1 2 3 [4])
|
|
|
|
[HyInteger(1), HyInteger(2), HyInteger(3), 4]
|
|
|
|
=> (list* 1)
|
|
|
|
1
|
|
|
|
=> (cons? (list* 1 2 3 4))
|
|
|
|
True
|
|
|
|
=> (list* 1 10 2 20 '{})
|
|
|
|
HyDict([
|
|
|
|
HyInteger(1), HyInteger(10),
|
2017-09-18 08:58:14 +02:00
|
|
|
HyInteger(2), HyInteger(20)])
|
2017-08-26 06:50:13 +02:00
|
|
|
=> (list* 1 10 2 20 {})
|
|
|
|
<HyCons (
|
|
|
|
HyInteger(1)
|
|
|
|
HyInteger(10)
|
|
|
|
HyInteger(2)
|
|
|
|
HyInteger(20)
|
|
|
|
. HyDict())>
|
2013-05-16 18:59:20 +02:00
|
|
|
|
2014-01-02 11:49:40 +01:00
|
|
|
.. _macroexpand-fn:
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2013-10-11 13:06:22 +02:00
|
|
|
macroexpand
|
|
|
|
-----------
|
|
|
|
|
2014-04-10 20:21:32 +02:00
|
|
|
.. versionadded:: 0.10.0
|
2014-01-02 11:49:40 +01:00
|
|
|
|
2013-10-11 13:06:22 +02:00
|
|
|
Usage: ``(macroexpand form)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns the full macro expansion of *form*.
|
2013-10-11 13:06:22 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-10-11 13:06:22 +02:00
|
|
|
|
2017-08-26 06:50:13 +02:00
|
|
|
=> (macroexpand '(-> (a b) (x y)))
|
|
|
|
HyExpression([
|
|
|
|
HySymbol('x'),
|
|
|
|
HyExpression([
|
|
|
|
HySymbol('a'),
|
|
|
|
HySymbol('b')]),
|
|
|
|
HySymbol('y')])
|
|
|
|
=> (macroexpand '(-> (a b) (-> (c d) (e f))))
|
|
|
|
HyExpression([
|
|
|
|
HySymbol('e'),
|
|
|
|
HyExpression([
|
|
|
|
HySymbol('c'),
|
|
|
|
HyExpression([
|
|
|
|
HySymbol('a'),
|
|
|
|
HySymbol('b')]),
|
|
|
|
HySymbol('d')]),
|
|
|
|
HySymbol('f')])
|
2013-10-11 14:50:10 +02:00
|
|
|
|
2014-01-02 11:49:40 +01:00
|
|
|
.. _macroexpand-1-fn:
|
|
|
|
|
2013-10-11 14:50:10 +02:00
|
|
|
macroexpand-1
|
|
|
|
-------------
|
|
|
|
|
2014-04-10 20:21:32 +02:00
|
|
|
.. versionadded:: 0.10.0
|
2014-01-02 11:49:40 +01:00
|
|
|
|
2013-10-11 14:50:10 +02:00
|
|
|
Usage: ``(macroexpand-1 form)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns the single step macro expansion of *form*.
|
2013-10-11 14:50:10 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-10-11 14:50:10 +02:00
|
|
|
|
2017-08-26 06:50:13 +02:00
|
|
|
=> (macroexpand-1 '(-> (a b) (-> (c d) (e f))))
|
|
|
|
HyExpression([
|
|
|
|
HySymbol('_>'),
|
|
|
|
HyExpression([
|
|
|
|
HySymbol('a'),
|
|
|
|
HySymbol('b')]),
|
|
|
|
HyExpression([
|
|
|
|
HySymbol('c'),
|
|
|
|
HySymbol('d')]),
|
|
|
|
HyExpression([
|
|
|
|
HySymbol('e'),
|
|
|
|
HySymbol('f')])])
|
2013-10-11 14:50:10 +02:00
|
|
|
|
2018-03-05 01:39:54 +01:00
|
|
|
.. _mangle-fn:
|
|
|
|
|
|
|
|
mangle
|
|
|
|
------
|
|
|
|
|
|
|
|
Usage: ``(mangle x)``
|
|
|
|
|
|
|
|
Stringify the input and translate it according to :ref:`Hy's mangling rules
|
|
|
|
<mangling>`.
|
|
|
|
|
|
|
|
.. code-block:: hylang
|
|
|
|
|
|
|
|
=> (mangle "foo-bar")
|
|
|
|
'foo_bar'
|
2014-01-14 08:30:36 +01:00
|
|
|
|
|
|
|
.. _merge-with-fn:
|
|
|
|
|
|
|
|
merge-with
|
|
|
|
----------
|
|
|
|
|
2014-10-09 01:58:42 +02:00
|
|
|
.. versionadded:: 0.10.1
|
2014-01-14 08:30:36 +01:00
|
|
|
|
2014-12-06 21:32:11 +01:00
|
|
|
Usage: ``(merge-with f &rest maps)``
|
2014-01-14 08:30:36 +01:00
|
|
|
|
2014-10-09 01:58:42 +02:00
|
|
|
Returns a map that consist of the rest of the maps joined onto first.
|
|
|
|
If a key occurs in more than one map, the mapping(s) from the latter
|
|
|
|
(left-to-right) will be combined with the mapping in the result by
|
|
|
|
calling ``(f val-in-result val-in-latter)``.
|
2014-01-14 08:30:36 +01:00
|
|
|
|
2014-12-07 07:35:32 +01:00
|
|
|
.. code-block:: hy
|
2014-01-14 08:30:36 +01:00
|
|
|
|
|
|
|
=> (merge-with (fn [x y] (+ x y)) {"a" 10 "b" 20} {"a" 1 "c" 30})
|
|
|
|
{u'a': 11L, u'c': 30L, u'b': 20L}
|
|
|
|
|
|
|
|
|
2015-10-17 13:56:35 +02:00
|
|
|
.. _name-fn:
|
|
|
|
|
|
|
|
name
|
|
|
|
----
|
|
|
|
|
|
|
|
.. versionadded:: 0.10.1
|
|
|
|
|
|
|
|
Usage: ``(name :keyword)``
|
|
|
|
|
|
|
|
Convert the given value to a string. Keyword special character will be
|
|
|
|
stripped. Strings will be used as is. Even objects with the `__name__`
|
|
|
|
magic will work.
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
=> (name :foo)
|
|
|
|
u'foo'
|
|
|
|
|
2014-01-02 11:49:40 +01:00
|
|
|
.. _neg?-fn:
|
|
|
|
|
2013-07-08 04:36:26 +02:00
|
|
|
neg?
|
|
|
|
----
|
|
|
|
|
|
|
|
Usage: ``(neg? x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if *x* is less than zero. Raises ``TypeError`` if
|
|
|
|
``(not (numeric? x))``.
|
2013-07-29 18:40:48 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (neg? -2)
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (neg? 3)
|
|
|
|
False
|
|
|
|
|
|
|
|
=> (neg? 0)
|
|
|
|
False
|
|
|
|
|
|
|
|
.. _none?-fn:
|
|
|
|
|
|
|
|
none?
|
|
|
|
-----
|
|
|
|
|
|
|
|
Usage: ``(none? x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if *x* is ``None``.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (none? None)
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (none? 0)
|
|
|
|
False
|
|
|
|
|
2017-05-19 22:36:06 +02:00
|
|
|
=> (setv x None)
|
2013-07-08 04:36:26 +02:00
|
|
|
=> (none? x)
|
|
|
|
True
|
|
|
|
|
|
|
|
=> ;; list.append always returns None
|
|
|
|
=> (none? (.append [1 2 3] 4))
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
|
|
.. _nth-fn:
|
|
|
|
|
|
|
|
nth
|
|
|
|
---
|
|
|
|
|
2016-11-24 03:35:17 +01:00
|
|
|
Usage: ``(nth coll n &optional [default None])``
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns the *n*-th item in a collection, counting from 0. Return the
|
2016-11-24 03:35:17 +01:00
|
|
|
default value, ``None``, if out of bounds (unless specified otherwise).
|
2014-12-07 07:05:52 +01:00
|
|
|
Raises ``ValueError`` if *n* is negative.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (nth [1 2 4 7] 1)
|
|
|
|
2
|
|
|
|
|
|
|
|
=> (nth [1 2 4 7] 3)
|
|
|
|
7
|
|
|
|
|
2016-11-24 03:35:17 +01:00
|
|
|
=> (none? (nth [1 2 4 7] 5))
|
2014-08-23 13:35:31 +02:00
|
|
|
True
|
|
|
|
|
|
|
|
=> (nth [1 2 4 7] 5 "default")
|
|
|
|
'default'
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (nth (take 3 (drop 2 [1 2 3 4 5 6])) 2))
|
|
|
|
5
|
2014-01-02 11:49:40 +01:00
|
|
|
|
2014-08-23 13:35:31 +02:00
|
|
|
=> (nth [1 2 4 7] -1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Indices for islice() must be None or an integer: 0 <= x <= sys.maxsize.
|
|
|
|
|
|
|
|
|
2013-07-29 18:40:48 +02:00
|
|
|
.. _numeric?-fn:
|
|
|
|
|
|
|
|
numeric?
|
2014-12-06 21:32:11 +01:00
|
|
|
--------
|
2013-07-29 18:40:48 +02:00
|
|
|
|
|
|
|
Usage: ``(numeric? x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if *x* is a numeric, as defined in Python's
|
|
|
|
``numbers.Number`` class.
|
2013-07-29 18:40:48 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-29 18:40:48 +02:00
|
|
|
|
|
|
|
=> (numeric? -2)
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (numeric? 3.2)
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (numeric? "foo")
|
|
|
|
False
|
|
|
|
|
|
|
|
|
2013-07-08 04:36:26 +02:00
|
|
|
.. _odd?-fn:
|
|
|
|
|
|
|
|
odd?
|
|
|
|
----
|
|
|
|
|
|
|
|
Usage: ``(odd? x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if *x* is odd. Raises ``TypeError`` if
|
|
|
|
``(not (numeric? x))``.
|
2013-07-29 18:40:48 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (odd? 13)
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (odd? 2)
|
|
|
|
False
|
|
|
|
|
|
|
|
=> (odd? 0)
|
|
|
|
False
|
|
|
|
|
2015-08-06 06:10:39 +02:00
|
|
|
.. _partition-fn:
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2015-08-05 22:59:05 +02:00
|
|
|
partition
|
|
|
|
---------
|
|
|
|
|
2015-09-03 06:54:24 +02:00
|
|
|
Usage: ``(partition coll [n] [step] [fillvalue])``
|
|
|
|
|
|
|
|
Chunks *coll* into *n*-tuples (pairs by default).
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
2017-08-26 06:50:13 +02:00
|
|
|
=> (list (partition (range 10))) ; n=2
|
|
|
|
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]
|
2015-08-05 22:59:05 +02:00
|
|
|
|
2015-09-03 06:54:24 +02:00
|
|
|
The *step* defaults to *n*, but can be more to skip elements, or less for a sliding window with overlap.
|
2015-08-05 22:59:05 +02:00
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
2017-08-26 06:50:13 +02:00
|
|
|
=> (list (partition (range 10) 2 3))
|
|
|
|
[(0, 1), (3, 4), (6, 7)]
|
|
|
|
=> (list (partition (range 5) 2 1))
|
|
|
|
[(0, 1), (1, 2), (2, 3), (3, 4)]
|
2015-09-03 06:54:24 +02:00
|
|
|
|
|
|
|
The remainder, if any, is not included unless a *fillvalue* is specified.
|
|
|
|
|
|
|
|
.. code-block:: hy
|
2015-08-05 22:59:05 +02:00
|
|
|
|
2017-08-26 06:50:13 +02:00
|
|
|
=> (list (partition (range 10) 3))
|
|
|
|
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
|
|
|
|
=> (list (partition (range 10) 3 :fillvalue "x"))
|
|
|
|
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 'x', 'x')]
|
2015-08-06 06:10:39 +02:00
|
|
|
|
|
|
|
.. _pos?-fn:
|
2015-08-05 22:59:05 +02:00
|
|
|
|
2013-07-08 04:36:26 +02:00
|
|
|
pos?
|
|
|
|
----
|
|
|
|
|
|
|
|
Usage: ``(pos? x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if *x* is greater than zero. Raises ``TypeError``
|
|
|
|
if ``(not (numeric? x))``.
|
2013-07-29 18:40:48 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (pos? 3)
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (pos? -2)
|
|
|
|
False
|
|
|
|
|
|
|
|
=> (pos? 0)
|
|
|
|
False
|
|
|
|
|
|
|
|
|
2013-07-29 18:40:48 +02:00
|
|
|
.. _second-fn:
|
|
|
|
|
|
|
|
second
|
2014-12-06 21:32:11 +01:00
|
|
|
------
|
2013-07-29 18:40:48 +02:00
|
|
|
|
|
|
|
Usage: ``(second coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns the second member of *coll*. Equivalent to ``(get coll 1)``.
|
2013-07-29 18:40:48 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-29 18:40:48 +02:00
|
|
|
|
|
|
|
=> (second [0 1 2])
|
|
|
|
1
|
|
|
|
|
|
|
|
|
2014-02-11 09:42:56 +01:00
|
|
|
.. _some-fn:
|
|
|
|
|
|
|
|
some
|
|
|
|
----
|
|
|
|
|
2014-04-10 20:21:32 +02:00
|
|
|
.. versionadded:: 0.10.0
|
2014-02-11 09:42:56 +01:00
|
|
|
|
|
|
|
Usage: ``(some pred coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns the first logically-true value of ``(pred x)`` for any ``x`` in
|
2016-11-24 03:35:17 +01:00
|
|
|
*coll*, otherwise ``None``. Return ``None`` if *coll* is empty.
|
2014-02-11 09:42:56 +01:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2014-02-11 09:42:56 +01:00
|
|
|
|
|
|
|
=> (some even? [2 4 6])
|
|
|
|
True
|
|
|
|
|
2016-11-24 03:35:17 +01:00
|
|
|
=> (none? (some even? [1 3 5]))
|
2014-09-04 11:29:38 +02:00
|
|
|
True
|
2014-02-11 09:42:56 +01:00
|
|
|
|
2016-11-24 03:35:17 +01:00
|
|
|
=> (none? (some identity [0 "" []]))
|
2014-02-11 09:42:56 +01:00
|
|
|
True
|
|
|
|
|
2014-09-04 11:29:38 +02:00
|
|
|
=> (some identity [0 "non-empty-string" []])
|
|
|
|
'non-empty-string'
|
|
|
|
|
2016-11-24 03:35:17 +01:00
|
|
|
=> (none? (some even? []))
|
2014-09-04 11:29:38 +02:00
|
|
|
True
|
2014-02-11 09:42:56 +01:00
|
|
|
|
|
|
|
|
2013-07-29 18:40:48 +02:00
|
|
|
.. _string?-fn:
|
|
|
|
|
|
|
|
string?
|
|
|
|
-------
|
|
|
|
|
|
|
|
Usage: ``(string? x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if *x* is a string.
|
2013-07-29 18:40:48 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-29 18:40:48 +02:00
|
|
|
|
|
|
|
=> (string? "foo")
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (string? -2)
|
|
|
|
False
|
|
|
|
|
2015-01-29 18:21:26 +01:00
|
|
|
.. _symbol?-fn:
|
|
|
|
|
|
|
|
symbol?
|
|
|
|
-------
|
|
|
|
|
|
|
|
Usage: ``(symbol? x)``
|
|
|
|
|
|
|
|
Returns ``True`` if *x* is a symbol.
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
=> (symbol? 'foo)
|
|
|
|
True
|
|
|
|
|
|
|
|
=> (symbol? '[a b c])
|
|
|
|
False
|
|
|
|
|
2013-09-04 02:41:11 +02:00
|
|
|
.. _zero?-fn:
|
|
|
|
|
|
|
|
zero?
|
2014-01-01 00:14:05 +01:00
|
|
|
-----
|
2013-09-04 02:41:11 +02:00
|
|
|
|
|
|
|
Usage: ``(zero? x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns ``True`` if *x* is zero.
|
2013-09-04 02:41:11 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-09-04 02:41:11 +02:00
|
|
|
|
|
|
|
=> (zero? 3)
|
|
|
|
False
|
|
|
|
|
|
|
|
=> (zero? -2)
|
|
|
|
False
|
|
|
|
|
|
|
|
=> (zero? 0)
|
|
|
|
True
|
|
|
|
|
2013-09-02 11:28:21 +02:00
|
|
|
|
2013-07-08 04:36:26 +02:00
|
|
|
Sequence Functions
|
2014-12-06 21:32:11 +01:00
|
|
|
==================
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
Sequence functions can either create or operate on a potentially
|
|
|
|
infinite sequence without requiring the sequence be fully realized in
|
|
|
|
a list or similar container. They do this by returning a Python
|
|
|
|
iterator.
|
|
|
|
|
|
|
|
We can use the canonical infinite Fibonacci number generator
|
|
|
|
as an example of how to use some of these functions.
|
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
(defn fib []
|
2013-11-26 19:31:10 +01:00
|
|
|
(setv a 0)
|
|
|
|
(setv b 1)
|
2016-11-24 03:35:17 +01:00
|
|
|
(while True
|
2013-07-08 04:36:26 +02:00
|
|
|
(yield a)
|
2013-11-26 19:31:10 +01:00
|
|
|
(setv (, a b) (, b (+ a b)))))
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
|
2016-11-24 03:35:17 +01:00
|
|
|
Note the ``(while True ...)`` loop. If we run this in the REPL,
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (fib)
|
|
|
|
<generator object fib at 0x101e642d0>
|
|
|
|
|
|
|
|
|
|
|
|
Calling the function only returns an iterator, but does no
|
|
|
|
work until we consume it. Trying something like this is not recommend as
|
|
|
|
the infinite loop will run until it consumes all available RAM, or
|
|
|
|
in this case until I killed it.
|
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (list (fib))
|
|
|
|
[1] 91474 killed hy
|
|
|
|
|
|
|
|
|
|
|
|
To get the first 10 Fibonacci numbers, use :ref:`take-fn`. Note that
|
|
|
|
:ref:`take-fn` also returns a generator, so I create a list from it.
|
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (list (take 10 (fib)))
|
|
|
|
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
|
|
|
|
|
|
|
|
|
|
|
|
To get the Fibonacci number at index 9, (starting from 0):
|
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (nth (fib) 9)
|
2014-01-02 11:49:40 +01:00
|
|
|
34
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
.. _cycle-fn:
|
|
|
|
|
|
|
|
cycle
|
2014-12-06 21:32:11 +01:00
|
|
|
-----
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
Usage: ``(cycle coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an infinite iterator of the members of coll.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
.. code-block:: clj
|
|
|
|
|
|
|
|
=> (list (take 7 (cycle [1 2 3])))
|
|
|
|
[1, 2, 3, 1, 2, 3, 1]
|
|
|
|
|
|
|
|
=> (list (take 2 (cycle [1 2 3])))
|
2014-01-02 11:49:40 +01:00
|
|
|
[1, 2]
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
.. _distinct-fn:
|
|
|
|
|
|
|
|
distinct
|
|
|
|
--------
|
|
|
|
|
|
|
|
Usage: ``(distinct coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterator containing only the unique members in *coll*.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (list (distinct [ 1 2 3 4 3 5 2 ]))
|
|
|
|
[1, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
=> (list (distinct []))
|
|
|
|
[]
|
|
|
|
|
|
|
|
=> (list (distinct (iter [ 1 2 3 4 3 5 2 ])))
|
|
|
|
[1, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
|
|
|
|
.. _drop-fn:
|
|
|
|
|
|
|
|
drop
|
|
|
|
----
|
|
|
|
|
|
|
|
Usage: ``(drop n coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterator, skipping the first *n* members of *coll*.
|
|
|
|
Raises ``ValueError`` if *n* is negative.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (list (drop 2 [1 2 3 4 5]))
|
|
|
|
[3, 4, 5]
|
|
|
|
|
|
|
|
=> (list (drop 4 [1 2 3 4 5]))
|
|
|
|
[5]
|
|
|
|
|
|
|
|
=> (list (drop 0 [1 2 3 4 5]))
|
|
|
|
[1, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
=> (list (drop 6 [1 2 3 4 5]))
|
|
|
|
[]
|
|
|
|
|
2014-08-22 11:09:59 +02:00
|
|
|
|
|
|
|
.. _drop-last-fn:
|
|
|
|
|
|
|
|
drop-last
|
|
|
|
---------
|
|
|
|
|
|
|
|
Usage: ``(drop-last n coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterator of all but the last *n* items in *coll*. Raises
|
|
|
|
``ValueError`` if *n* is negative.
|
2014-08-22 11:09:59 +02:00
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
=> (list (drop-last 5 (range 10 20)))
|
|
|
|
[10, 11, 12, 13, 14]
|
|
|
|
|
|
|
|
=> (list (drop-last 0 (range 5)))
|
|
|
|
[0, 1, 2, 3, 4]
|
|
|
|
|
|
|
|
=> (list (drop-last 100 (range 100)))
|
|
|
|
[]
|
|
|
|
|
2017-01-05 00:11:54 +01:00
|
|
|
=> (list (take 5 (drop-last 100 (count 10))))
|
2014-08-22 11:09:59 +02:00
|
|
|
[10, 11, 12, 13, 14]
|
|
|
|
|
|
|
|
|
2013-07-29 18:40:48 +02:00
|
|
|
.. _drop-while-fn:
|
|
|
|
|
|
|
|
drop-while
|
|
|
|
-----------
|
|
|
|
|
|
|
|
Usage: ``(drop-while pred coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterator, skipping members of *coll* until *pred* is ``False``.
|
2013-07-29 18:40:48 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-29 18:40:48 +02:00
|
|
|
|
|
|
|
=> (list (drop-while even? [2 4 7 8 9]))
|
|
|
|
[7, 8, 9]
|
|
|
|
|
|
|
|
=> (list (drop-while numeric? [1 2 3 None "a"])))
|
|
|
|
[None, u'a']
|
|
|
|
|
|
|
|
=> (list (drop-while pos? [2 4 7 8 9]))
|
|
|
|
[]
|
|
|
|
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
.. _filter-fn:
|
|
|
|
|
|
|
|
filter
|
|
|
|
------
|
|
|
|
|
|
|
|
Usage: ``(filter pred coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterator for all items in *coll* that pass the predicate *pred*.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
See also :ref:`remove-fn`.
|
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (list (filter pos? [1 2 3 -4 5 -7]))
|
|
|
|
[1, 2, 3, 5]
|
|
|
|
|
|
|
|
=> (list (filter even? [1 2 3 -4 5 -7]))
|
|
|
|
[2, -4]
|
|
|
|
|
2013-12-30 23:09:17 +01:00
|
|
|
.. _flatten-fn:
|
|
|
|
|
|
|
|
flatten
|
|
|
|
-------
|
|
|
|
|
|
|
|
.. versionadded:: 0.9.12
|
|
|
|
|
|
|
|
Usage: ``(flatten coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns a single list of all the items in *coll*, by flattening all
|
2013-12-30 23:09:17 +01:00
|
|
|
contained lists and/or tuples.
|
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-12-30 23:09:17 +01:00
|
|
|
|
|
|
|
=> (flatten [1 2 [3 4] 5])
|
|
|
|
[1, 2, 3, 4, 5]
|
|
|
|
|
|
|
|
=> (flatten ["foo" (, 1 2) [1 [2 3] 4] "bar"])
|
|
|
|
['foo', 1, 2, 1, 2, 3, 4, 'bar']
|
|
|
|
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
.. _iterate-fn:
|
|
|
|
|
|
|
|
iterate
|
|
|
|
-------
|
|
|
|
|
|
|
|
Usage: ``(iterate fn x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterator of *x*, *fn(x)*, *fn(fn(x))*, etc.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (list (take 5 (iterate inc 5)))
|
|
|
|
[5, 6, 7, 8, 9]
|
|
|
|
|
|
|
|
=> (list (take 5 (iterate (fn [x] (* x x)) 5)))
|
|
|
|
[5, 25, 625, 390625, 152587890625]
|
|
|
|
|
|
|
|
|
2014-07-24 20:44:55 +02:00
|
|
|
.. _read-fn:
|
|
|
|
|
|
|
|
read
|
|
|
|
----
|
|
|
|
|
2014-09-02 22:26:47 +02:00
|
|
|
Usage: ``(read &optional [from-file eof])``
|
2014-07-24 20:44:55 +02:00
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Reads the next Hy expression from *from-file* (defaulting to ``sys.stdin``), and
|
|
|
|
can take a single byte as EOF (defaults to an empty string). Raises ``EOFError``
|
|
|
|
if *from-file* ends before a complete expression can be parsed.
|
2014-07-24 20:44:55 +02:00
|
|
|
|
|
|
|
.. code-block:: hy
|
2014-08-19 10:55:47 +02:00
|
|
|
|
2017-08-26 06:50:13 +02:00
|
|
|
=> (read)
|
|
|
|
(+ 2 2)
|
|
|
|
HyExpression([
|
|
|
|
HySymbol('+'),
|
|
|
|
HyInteger(2),
|
|
|
|
HyInteger(2)])
|
|
|
|
=> (eval (read))
|
|
|
|
(+ 2 2)
|
|
|
|
4
|
|
|
|
=> (import io)
|
|
|
|
=> (setv buffer (io.StringIO "(+ 2 2)\n(- 2 1)"))
|
|
|
|
=> (eval (read :from_file buffer))
|
|
|
|
4
|
|
|
|
=> (eval (read :from_file buffer))
|
|
|
|
1
|
|
|
|
|
|
|
|
=> (with [f (open "example.hy" "w")]
|
|
|
|
... (.write f "(print 'hello)\n(print \"hyfriends!\")"))
|
|
|
|
35
|
|
|
|
=> (with [f (open "example.hy")]
|
|
|
|
... (try (while True
|
|
|
|
... (setv exp (read f))
|
|
|
|
... (print "OHY" exp)
|
|
|
|
... (eval exp))
|
|
|
|
... (except [e EOFError]
|
|
|
|
... (print "EOF!"))))
|
|
|
|
OHY HyExpression([
|
|
|
|
HySymbol('print'),
|
|
|
|
HyExpression([
|
|
|
|
HySymbol('quote'),
|
|
|
|
HySymbol('hello')])])
|
|
|
|
hello
|
|
|
|
OHY HyExpression([
|
|
|
|
HySymbol('print'),
|
|
|
|
HyString('hyfriends!')])
|
|
|
|
hyfriends!
|
|
|
|
EOF!
|
2014-08-19 11:02:07 +02:00
|
|
|
|
2015-07-28 16:55:16 +02:00
|
|
|
read-str
|
|
|
|
--------
|
|
|
|
|
2015-07-28 17:37:50 +02:00
|
|
|
Usage: ``(read-str "string")``
|
2015-07-28 16:55:16 +02:00
|
|
|
|
|
|
|
This is essentially a wrapper around `read` which reads expressions from a
|
|
|
|
string:
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
2017-08-26 06:50:13 +02:00
|
|
|
=> (read-str "(print 1)")
|
|
|
|
HyExpression([
|
|
|
|
HySymbol('print'),
|
|
|
|
HyInteger(1)])
|
|
|
|
=> (eval (read-str "(print 1)"))
|
|
|
|
1
|
2014-07-24 20:44:55 +02:00
|
|
|
|
2013-07-08 04:36:26 +02:00
|
|
|
.. _remove-fn:
|
|
|
|
|
|
|
|
remove
|
|
|
|
------
|
|
|
|
|
|
|
|
Usage: ``(remove pred coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterator from *coll* with elements that pass the
|
|
|
|
predicate, *pred*, removed.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
See also :ref:`filter-fn`.
|
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (list (remove odd? [1 2 3 4 5 6 7]))
|
|
|
|
[2, 4, 6]
|
|
|
|
|
|
|
|
=> (list (remove pos? [1 2 3 4 5 6 7]))
|
|
|
|
[]
|
|
|
|
|
|
|
|
=> (list (remove neg? [1 2 3 4 5 6 7]))
|
|
|
|
[1, 2, 3, 4, 5, 6, 7]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. _repeat-fn:
|
|
|
|
|
|
|
|
repeat
|
|
|
|
------
|
|
|
|
|
|
|
|
Usage: ``(repeat x)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterator (infinite) of ``x``.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (list (take 6 (repeat "s")))
|
|
|
|
[u's', u's', u's', u's', u's', u's']
|
|
|
|
|
|
|
|
|
|
|
|
.. _repeatedly-fn:
|
|
|
|
|
|
|
|
repeatedly
|
|
|
|
----------
|
|
|
|
|
|
|
|
Usage: ``(repeatedly fn)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterator by calling *fn* repeatedly.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (import [random [randint]])
|
|
|
|
|
|
|
|
=> (list (take 5 (repeatedly (fn [] (randint 0 10)))))
|
|
|
|
[6, 2, 0, 6, 7]
|
|
|
|
|
|
|
|
|
|
|
|
.. _take-fn:
|
|
|
|
|
|
|
|
take
|
|
|
|
----
|
|
|
|
|
|
|
|
Usage: ``(take n coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterator containing the first *n* members of *coll*.
|
|
|
|
Raises ``ValueError`` if *n* is negative.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (list (take 3 [1 2 3 4 5]))
|
|
|
|
[1, 2, 3]
|
|
|
|
|
|
|
|
=> (list (take 4 (repeat "s")))
|
|
|
|
[u's', u's', u's', u's']
|
2014-01-02 11:49:40 +01:00
|
|
|
|
2013-07-08 04:36:26 +02:00
|
|
|
=> (list (take 0 (repeat "s")))
|
|
|
|
[]
|
|
|
|
|
|
|
|
.. _take-nth-fn:
|
|
|
|
|
|
|
|
take-nth
|
|
|
|
--------
|
|
|
|
|
|
|
|
Usage: ``(take-nth n coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterator containing every *n*-th member of *coll*.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (list (take-nth 2 [1 2 3 4 5 6 7]))
|
|
|
|
[1, 3, 5, 7]
|
|
|
|
|
|
|
|
=> (list (take-nth 3 [1 2 3 4 5 6 7]))
|
|
|
|
[1, 4, 7]
|
|
|
|
|
|
|
|
=> (list (take-nth 4 [1 2 3 4 5 6 7]))
|
|
|
|
[1, 5]
|
|
|
|
|
|
|
|
=> (list (take-nth 10 [1 2 3 4 5 6 7]))
|
|
|
|
[1]
|
2014-01-02 11:49:40 +01:00
|
|
|
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
.. _take-while-fn:
|
|
|
|
|
|
|
|
take-while
|
|
|
|
----------
|
|
|
|
|
|
|
|
Usage: ``(take-while pred coll)``
|
|
|
|
|
2014-12-07 07:05:52 +01:00
|
|
|
Returns an iterator from *coll* as long as *pred* returns ``True``.
|
2013-07-08 04:36:26 +02:00
|
|
|
|
2014-03-14 14:01:47 +01:00
|
|
|
.. code-block:: hy
|
2013-07-08 04:36:26 +02:00
|
|
|
|
|
|
|
=> (list (take-while pos? [ 1 2 3 -4 5]))
|
|
|
|
[1, 2, 3]
|
|
|
|
|
|
|
|
=> (list (take-while neg? [ -4 -3 1 2 5]))
|
|
|
|
[-4, -3]
|
|
|
|
|
|
|
|
=> (list (take-while neg? [ 1 2 3 -4 5]))
|
|
|
|
[]
|
2014-04-29 16:04:21 +02:00
|
|
|
|
2018-03-05 01:39:54 +01:00
|
|
|
.. _unmangle-fn:
|
|
|
|
|
|
|
|
unmangle
|
|
|
|
--------
|
|
|
|
|
|
|
|
Usage: ``(unmangle x)``
|
|
|
|
|
|
|
|
Stringify the input and return a string that would :ref:`mangle <mangling>` to
|
|
|
|
it. Note that this isn't a one-to-one operation, and nor is ``mangle``, so
|
|
|
|
``mangle`` and ``unmangle`` don't always round-trip.
|
|
|
|
|
|
|
|
.. code-block:: hylang
|
|
|
|
|
|
|
|
=> (unmangle "foo_bar")
|
|
|
|
'foo-bar'
|
|
|
|
|
2016-12-23 01:20:30 +01:00
|
|
|
Included itertools
|
|
|
|
==================
|
|
|
|
|
2017-01-04 21:04:41 +01:00
|
|
|
count cycle repeat accumulate chain compress drop-while remove group-by islice *map take-while tee zip-longest product permutations combinations multicombinations
|
|
|
|
---------
|
|
|
|
|
2017-01-05 00:11:54 +01:00
|
|
|
All of Python's `itertools <https://docs.python.org/3/library/itertools.html>`_
|
|
|
|
are available. Some of their names have been changed:
|
2016-12-23 01:20:30 +01:00
|
|
|
|
2017-01-05 00:11:54 +01:00
|
|
|
- ``starmap`` has been changed to ``*map``
|
2016-12-23 01:20:30 +01:00
|
|
|
|
2017-01-05 00:11:54 +01:00
|
|
|
- ``combinations_with_replacement`` has been changed to ``multicombinations``
|
2016-12-23 01:20:30 +01:00
|
|
|
|
2017-01-05 00:11:54 +01:00
|
|
|
- ``groupby`` has been changed to ``group-by``
|
2016-12-23 01:20:30 +01:00
|
|
|
|
2017-01-05 00:11:54 +01:00
|
|
|
- ``takewhile`` has been changed to ``take-while``
|
2017-01-04 21:04:41 +01:00
|
|
|
|
2017-01-05 00:11:54 +01:00
|
|
|
- ``dropwhile`` has been changed to ``drop-while``
|
2017-01-04 21:04:41 +01:00
|
|
|
|
2017-01-05 00:11:54 +01:00
|
|
|
- ``filterfalse`` has been changed to ``remove``
|
2017-08-26 06:50:13 +02:00
|
|
|
|