2015-01-17 23:46:23 +01:00
|
|
|
==========
|
|
|
|
Flow
|
|
|
|
==========
|
|
|
|
|
2015-01-18 13:34:07 +01:00
|
|
|
.. versionadded:: 0.10.1
|
2015-01-17 23:46:23 +01:00
|
|
|
|
2015-07-24 09:52:33 +02:00
|
|
|
The ``flow`` macros allow directing the flow of a program with greater ease.
|
|
|
|
|
|
|
|
|
2015-01-17 23:46:23 +01:00
|
|
|
Macros
|
|
|
|
======
|
|
|
|
|
2015-01-18 13:34:07 +01:00
|
|
|
.. _case:
|
2015-01-17 23:46:23 +01:00
|
|
|
.. _switch:
|
|
|
|
|
2015-01-18 13:34:07 +01:00
|
|
|
case
|
2015-01-17 23:46:23 +01:00
|
|
|
-----
|
|
|
|
|
2015-01-18 13:34:07 +01:00
|
|
|
``case`` allows you to decide based on the value of a variable.
|
2015-01-17 23:46:23 +01:00
|
|
|
|
|
|
|
|
2015-01-18 13:34:07 +01:00
|
|
|
Usage: `(case variable val1 (body1) val2 (body2) ...)`
|
2015-01-17 23:46:23 +01:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
(require hy.contrib.flow)
|
2015-07-24 09:52:33 +02:00
|
|
|
|
|
|
|
(defn temp-commenter [temp]
|
|
|
|
(case temp
|
|
|
|
-10 (print "It's freezing. Turn up the thermostat!")
|
|
|
|
15 (print "Sounds about average.")
|
|
|
|
45 (print "Holy smokes. It's hot in here!")
|
2015-01-18 13:34:07 +01:00
|
|
|
(print "I don't even know.")))
|
2015-01-17 23:46:23 +01:00
|
|
|
|
2015-07-24 09:52:33 +02:00
|
|
|
|
2015-01-17 23:46:23 +01:00
|
|
|
switch
|
|
|
|
-----
|
|
|
|
|
|
|
|
``switch`` allows you to run code based on the value of a variable.
|
|
|
|
A final extra body allows for a default case.
|
|
|
|
|
|
|
|
|
|
|
|
Usage: `(switch var (cond1) (body1) (cond2) (body2) ... )`
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: hy
|
|
|
|
|
|
|
|
(require hy.contrib.flow)
|
2015-07-24 09:52:33 +02:00
|
|
|
|
|
|
|
(defn temp-commenter [temp]
|
|
|
|
(switch temp
|
|
|
|
(<= 10.0) (print "Better wear a jacket!")
|
|
|
|
(<= 25.0) (print "Brace yourselves. Summer is coming!")
|
|
|
|
(<= 30.0) (print "Time to get some ice cream.")
|
|
|
|
(print "Sounds like a heat wave")))
|