diff options
author | Ben Sima <ben@bsima.me> | 2021-02-10 18:04:12 -0500 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2021-02-17 20:27:39 -0500 |
commit | ad3102e108562aa7c12e89991eb387cd3aa359ae (patch) | |
tree | 542407f91c0a2d78f529eccc1d0ee58df10156aa | |
parent | 81dc733f5138aa0720eb2f6382bb91b33e1661cc (diff) |
Add boolean and logging pipeline operators
-rw-r--r-- | Alpha.hs | 35 | ||||
-rw-r--r-- | Biz/Log.hs | 15 |
2 files changed, 42 insertions, 8 deletions
@@ -42,12 +42,17 @@ module Alpha (<%), (%>), - -- * inding + -- * Binding bind, (+>), -- * Bool don't, + (?>), + (?<), + (?:), + (?.), + (?+), -- * Text tshow, @@ -66,9 +71,6 @@ module Alpha -- * Data Validation require, - - -- * Debugging tools - say, ) where @@ -85,10 +87,6 @@ import qualified Prelude list :: a -> [a] list a = [a] --- | Debugging printf -say :: Text -> IO () -say = putText - -- | Composition compose :: (a -> b) -> (b -> c) -> (a -> c) compose f g x = g (f x) @@ -163,6 +161,27 @@ a +> b = a Prelude.>>= b infixl 1 +> +-- | If-then-else. wutcol +(?:) :: Bool -> (Bool -> p, Bool -> p) -> p +a ?: (f, g) = if a then f a else g a + +-- | Inverted if-then-else. wutdot +(?.) :: Bool -> (Bool -> p, Bool -> p) -> p +a ?. (g, f) = if a then f a else g a + +-- | Positive assertion. wutgar +(?>) :: Bool -> (Bool -> Text -> a) -> Text -> a +a ?> f = if a then f a else panic "wutgar failed" + +-- | Lisp-style cond. wutlus +(?+) :: t -> [(t -> Bool, p)] -> p +a ?+ ((p, v) : ls) = if p a then v else a ?+ ls +_ ?+ [] = panic "wutlus: empty cond list" + +-- | Negative assertion. wutgal +(?<) :: Bool -> (Bool -> Text -> a) -> Text -> a +a ?< f = if not a then f a else panic "wutgal failed" + -- | Removes newlines from text. chomp :: Text -> Text chomp = Text.filter (/= '\n') @@ -11,6 +11,9 @@ module Biz.Log fail, -- Debugging mark, + -- Operators + (~&), + (~?), -- | Low-level msg, br, @@ -56,3 +59,15 @@ mark label val = unsafePerformIO <| do msg Mark [label, tshow val] pure val + +-- | Pipelined version of 'mark'. +-- +-- @ +-- mark label val = val ~| label +-- @ +(~&) :: Show a => a -> Text -> a +(~&) val label = mark label val + +-- | Conditional mark. +(~?) :: Show a => a -> (a -> Bool) -> Text -> a +(~?) val test label = if test val then mark label val else val |