Basic.md
June 6, 2020 ยท View on GitHub
Basic Programming examples with ZArrow
val plusOne = (_: Int) + 1
val mulTwo = (_: Int) * 2
val add1: ZArrow[Nothing, Int, Int] = ZArrow(plusOne)
val mul2: ZArrow[Nothing, Int, Int] = ZArrow(mulTwo)
(add1 >>> mul2).run(6) // add1 andThen mul2 == 14
(add1 <<< mul2).run(6)) // add1 compose mul2 == 13
(add1 *** mul2).run((3, 3)) // add1 split mul2 == (4, 6)
(add1 &&& mul2).run(3) // add1 merge mul2 == (4 -> 6))
(add1 <*> mul2)(_ -> _).run(3)) // add1 zipWith mul2 == (4 -> 6))
// Complex composition
val composed:Int => Int = (add1 >>> mul2) >>> (add1 <*> mul2)
val res = composed.run(1) // (5,8)
Looking at those examples, we can conclude the following properties of Arrow Programming:
-
Succinct
No local variables nor method arguments. We operate only on methods, giving a compiler to implicitly provide temporarily variables -
Clear
We operate methods and explicitly specify programming intent with symbol operations -
Composition Friendly
As the complexity of software grows, we, the Programmers, tend to express complex algorithms thru chains of simple and manageble methods.ZArrowprovides this API and simplifies development a lot