Skip to content

Instantly share code, notes, and snippets.

@danidiaz
Last active December 29, 2015 04:09
Show Gist options
  • Select an option

  • Save danidiaz/7612820 to your computer and use it in GitHub Desktop.

Select an option

Save danidiaz/7612820 to your computer and use it in GitHub Desktop.
Concatenative helpers for Haskell
module Util (
dot, (.:),
dot3, (..:),
cleave,
cleave3,
spread,
spread3,
apply,
apply3,
ifte
) where
import Data.Function (on)
import Control.Applicative (liftA2,liftA3)
dot :: (b -> c) -> (a -> a1 -> b) -> a -> a1 -> c
dot = ((.).(.))
(.:) :: (b -> c) -> (a -> a1 -> b) -> a -> a1 -> c
(.:) = dot
dot3 :: (b -> c) -> (a -> a1 -> a2 -> b) -> a -> a1 -> a2 -> c
dot3 = ((.).(.).(.))
(..:) :: (b -> c) -> (a -> a1 -> a2 -> b) -> a -> a1 -> a2 -> c
(..:) = dot3
-- http://hackage.haskell.org/package/concatenative-1.0.1/docs/src/Control-Concatenative.html
-- http://stackoverflow.com/questions/20153492/is-there-a-way-to-make-h-f-x-g-x-point-free-in-haskell
-- http://www.infoq.com/presentations/gershwin
-- http://www.infoq.com/presentations/concatenative-clojure
-- http://www.amazon.com/Combinatory-Logic-Discrete-Mathematics-Applications-ebook/dp/B008ID44T4/
cleave :: (a -> b) -> (a -> c) -> (b -> c -> d) -> a -> d
cleave h i f = liftA2 f h i
cleave3 :: (a -> b) -> (a -> c) -> (a -> d) -> (b -> c -> d -> e) -> a -> e
cleave3 h i j f= liftA3 f h i j
spread :: (a -> c) -> (b -> d) -> (c -> d -> e) -> a -> b -> e
spread f g c x y = c (f x) (g y)
spread3 :: (a -> d) -> (b -> e) -> (c -> f) -> (d -> e -> f -> g) -> a -> b -> c -> g
spread3 f g h c x y z = c (f x) (g y) (h z)
apply :: (t -> t1) -> (t1 -> t1 -> t2) -> t -> t -> t2
apply = flip on
apply3 :: (a -> b) -> (b -> b -> b -> c) -> a -> a -> a -> c
apply3 f c x y z = c (f x) (f y) (f z)
ifte :: (a -> Bool) -- ^ A predicate
-> (a -> b) -- ^ Applied if the predicate yields True
-> (a -> b) -- ^ Applied if the predicate yields False
-> a -> b
ifte test ca cb x =
if test x then ca x else cb x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment