arliss_obsidian/fp/Language/Infix Operators/Directionality.md

45 lines
1.5 KiB
Markdown
Raw Normal View History

2024-09-23 23:56:55 +00:00
Most commonly used [[Infix Operators]] have flipped variants, e.g.
- [[Functions#Composition|function composition]] has `g <<< f` or `f >>> g`
- function application has `f $ a` or `a # f`
- [[Functor|map]] has `f <$> a` or `a <#> f`
- [[Bind|bind]] has `f =<< m` or `m >>= f`
In general, right-to-left operators tend to be easier to refactor into & out of because they closely mirror the expressions they replace:
```haskell
map (add 1) maybeNum
-- into
add 1 <$> maybeNum
foo (bar a)
-- into
foo <<< bar
```
Left-to-right, on the other hand, can read better to humans and plays better with pipelines containing both [[Bind|bind `>>=`]] and [[Functor|map `<#>`]].
Consider an expression piping `Maybe String` to `split :: String -> Array String` then `Data.Array.NonEmpty.fromArray :: Array a -> Maybe (NonEmptyArray a)`, then `toLower` each element:
```haskell
String.toLower
<$> (
Array.NonEmpty.fromArray
=<< String.split "."
<$> mStr
)
```
We need to wrap the right hand of the last `map` because the precedence of `=<<` is `1` (the lowest) and the precedence of `<$>` is `4`.
Written RTL, though, gives:
```haskell
mStr
<#> String.split "."
>>= Array.NonEmpty.fromArray
<#> String.toLower
```
This works because `<#>`'s [[Precedence|precedence]] (1) is the same as `>>=`. The lower precedence on flipped map means you'll often need more parentheses wrapping its arguments `(..) <#> (..) >>= (..)` as opposed to entire expressions `.. <$> (.. =<< ..)`.
Personally, I try to stick to RTL except for expressions including bind.