From a3670a8d575c70f8ae314950b1db941d881b91e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matth=C3=ADas=20P=C3=A1ll=20Gissurarson?= Date: Sat, 17 Jan 2015 22:46:23 +0000 Subject: [PATCH 1/2] added flow macros --- docs/contrib/flow.rst | 59 ++++++++++++++++++++++++++++++++++++++++++ docs/contrib/index.rst | 1 + hy/contrib/flow.hy | 24 +++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 docs/contrib/flow.rst create mode 100644 hy/contrib/flow.hy diff --git a/docs/contrib/flow.rst b/docs/contrib/flow.rst new file mode 100644 index 0000000..46aae99 --- /dev/null +++ b/docs/contrib/flow.rst @@ -0,0 +1,59 @@ +========== +Flow +========== + +.. versionadded:: 0.10.2 + +The ``flow`` macros allow a programmer to direct the flow of his program with +greater ease. + + +Macros +====== + +.. _guard: +.. _switch: + +guard +----- + +``guard`` allows you to guard against a condition. + + +Usage: `(guard (cond1) (body1) (cond2) (body2) ...)` + +Example: + +.. code-block:: hy + + (require hy.contrib.flow) + + (defn army-greeter [age height] + (guard + (< age 18) (print "You are too young!") + (< height 170) (print "You are too small!") + True (print "Welcome aboard!"))) + + +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) + + (defn bmi-commenter [bmi] + (switch bmi + (<= 18.5) (print "you are underweight!") + (<= 25.0) (print "apparently normal") + (<= 30) (print "a little too heavy, but ok") + (print "You are a whale!"))) + diff --git a/docs/contrib/index.rst b/docs/contrib/index.rst index eda11f2..ebed64f 100644 --- a/docs/contrib/index.rst +++ b/docs/contrib/index.rst @@ -10,3 +10,4 @@ Contents: anaphoric loop multi + flow diff --git a/hy/contrib/flow.hy b/hy/contrib/flow.hy new file mode 100644 index 0000000..6f0aa4e --- /dev/null +++ b/hy/contrib/flow.hy @@ -0,0 +1,24 @@ +;; Additional flow macros + +(defmacro/g! guard [&rest args] + (setv g!cond (car args)) + (setv g!body (car (cdr args))) + (setv g!rest (cdr (cdr args))) + (if g!rest + `(if ~g!cond + ~g!body + (guard ~@g!rest)) + `(if ~g!cond + ~g!body))) + +(defmacro/g! switch [variable &rest args] + (setv g!comp (car args)) + (setv g!body (car (cdr args))) + (setv g!rest (cdr (cdr args))) + (setv g!cond `(~(car g!comp) ~variable ~@(cdr g!comp))) + (if g!rest + (if (cdr g!rest) + `(if ~g!cond ~g!body (switch ~variable ~@g!rest)) + `(if ~g!cond ~g!body ~@g!rest)) + `(if ~g!cond ~g!body))) + From 7be22e361b8e05818d79ddc688ceea7278977142 Mon Sep 17 00:00:00 2001 From: Matthias Pall Gissurarson Date: Sun, 18 Jan 2015 12:34:07 +0000 Subject: [PATCH 2/2] removed guard as it matches cond almost exactly, added case --- docs/contrib/flow.rst | 25 +++++++++++++------------ hy/contrib/flow.hy | 22 +++++++++++----------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/docs/contrib/flow.rst b/docs/contrib/flow.rst index 46aae99..be868db 100644 --- a/docs/contrib/flow.rst +++ b/docs/contrib/flow.rst @@ -2,7 +2,7 @@ Flow ========== -.. versionadded:: 0.10.2 +.. versionadded:: 0.10.1 The ``flow`` macros allow a programmer to direct the flow of his program with greater ease. @@ -11,16 +11,16 @@ greater ease. Macros ====== -.. _guard: +.. _case: .. _switch: -guard +case ----- -``guard`` allows you to guard against a condition. +``case`` allows you to decide based on the value of a variable. -Usage: `(guard (cond1) (body1) (cond2) (body2) ...)` +Usage: `(case variable val1 (body1) val2 (body2) ...)` Example: @@ -28,11 +28,12 @@ Example: (require hy.contrib.flow) - (defn army-greeter [age height] - (guard - (< age 18) (print "You are too young!") - (< height 170) (print "You are too small!") - True (print "Welcome aboard!"))) + (defn bmi-commenter [bmi] + (case bmi + 10 (print "The bmi was 10, wow.") + 20 (print "20? Really?") + 30 (print "Was it 30? Ok...") + (print "I don't even know."))) switch @@ -54,6 +55,6 @@ Example: (switch bmi (<= 18.5) (print "you are underweight!") (<= 25.0) (print "apparently normal") - (<= 30) (print "a little too heavy, but ok") - (print "You are a whale!"))) + (<= 30.0) (print "a little too heavy, but ok") + (print "You are a whale!"))) diff --git a/hy/contrib/flow.hy b/hy/contrib/flow.hy index 6f0aa4e..ae70362 100644 --- a/hy/contrib/flow.hy +++ b/hy/contrib/flow.hy @@ -1,15 +1,5 @@ ;; Additional flow macros -(defmacro/g! guard [&rest args] - (setv g!cond (car args)) - (setv g!body (car (cdr args))) - (setv g!rest (cdr (cdr args))) - (if g!rest - `(if ~g!cond - ~g!body - (guard ~@g!rest)) - `(if ~g!cond - ~g!body))) (defmacro/g! switch [variable &rest args] (setv g!comp (car args)) @@ -21,4 +11,14 @@ `(if ~g!cond ~g!body (switch ~variable ~@g!rest)) `(if ~g!cond ~g!body ~@g!rest)) `(if ~g!cond ~g!body))) - + +(defmacro/g! case [variable &rest args] + (setv g!value (car args)) + (setv g!body (car (cdr args))) + (setv g!rest (cdr (cdr args))) + (setv g!cond `(= ~variable ~g!value)) + (if g!rest + (if (cdr g!rest) + `(if ~g!cond ~g!body (case ~variable ~@g!rest)) + `(if ~g!cond ~g!body ~@g!rest)) + `(if ~g!cond ~g!body)))