/compose-ltr

Primary LanguageHaskellMIT LicenseMIT

compose-ltr

More intuitive, left-to-right function composition

Written in Haskell MIT Licensed Hackage

compose-ltr introduces alternatives to $ and . that compose left-to-right.

Example

Imagine you see the following snippet of code. How quickly and easily can you understand what it does?

mapM_ print $ reverse $ filter (\x -> x `mod` 2 == 0) $ [1..10]

While the function calls are indeed flowing left to right, the data's source is actually on the right ([1..10]) and the subsequent modifications flow right to left. One has to keep a list of modifications in one's head to understand the code. Alternatively, one could try to read the code right to left.

In many cases, focusing on the data rather than the execution can lead to far more intuitive code. Here's the same snippet written using $> from compose-ltr:

[1..10] $> filter (\x -> x `mod` 2 == 0) $> reverse $> mapM_ print

How To Use

From the comparison example above, notice that all the steps are written in reverse order (to execution). Any code that uses $ and . can be reversed:

a $ b
c . d

Becomes (adoptiong LTR direction):

b $> a
d .> c

Should you wish to keep using $ and ., compose-ltr also provides directional equivalents for them too:

a $ b
c . d

Becomes (retaining RTL direction):

a <$ b
c <. d

Real World Example

process :: String -> Q Exp
process = id
  .> splitOnCommas
  .> map nameAndValue
  .> joinAsColumns
  .> wrapInParens
  .> parseHsStrToQQExp
  .> return

(source: https://github.com/Wizek/dump/blob/48443d5/src/Debug/Dump.hs#L77)