ava/infix-operators.md

43 lines
697 B
Markdown

# Infix Operators
Example of defining `->` to do `map` for any functor:
```
given F[*] :: Functor, A, B
infix ->: (fa: F[A], f: (A) => B) => F[B]
map (fa) (f)
end infix
```
```
let xs: List[Int] := list(1, 2, 3)
let addOne := (x: Int32) => x + 1
-- [2, 3, 4]
let ys := xs -> addOne
-- [4, 5, 6]
let zs := xs -> addOne -> addOne -> addOne
-- note, in order precedence: ((xs -> addOne) -> addOne) -> addOne
```
## Precedence
In order precedence.
## Using Parentheses to Group
Infix operators for numeric operations are defined for tuples of size one.
```
(12 - 2) / 2
```
## Composition
```
given A, B, C
infix ∘: (f: B => C, g: A => B) => (A) => C
compose(f, g)
end infix
```