[TOC] ###### tags: `lang` `js` `fp` # Guides for Sanctuary, Fluture, and Daggy ## Must Read * FP Concept: [Bakke@gitconnected](https://levelup.gitconnected.com/@janvidar), [Prof.Frisby](https://mostly-adequate.gitbooks.io/mostly-adequate-guide/) * **Sanctuary** [DavidChambers@ReactConf](https://www.youtube.com/watch?v=a2astdDbOjk) * **Fluture** [Avaq@dev.to](https://dev.to/avaq/fluture-a-functional-alternative-to-promises-21b), [async-problem](https://github.com/plaid/deprecated-async-problem) * Daggy.js ## Coding Convention ### Naming ```JavaScript /* main packages */ const S = require ('sanctuary') // THE library const $ = require ('sanctuary-def') // types, def const Z = require ('sanctuary-type-classes') // type classes const F = require ('fluture') // async w. Future type const F$ = require ('fluture-sanctuary-types') // Future type const FN = require ('fluture-node') // Future toolbox /* other helpers */ const show = require ('sanctuary-show') const type = require ('sanctuary-type-identifiers') const daggy = require ('daggy') ``` ### FYI from Issues * [`)(` Spacing](https://github.com/sanctuary-js/sanctuary/issues/438) * [Async Test](https://github.com/fluture-js/Fluture/issues/77) * [eitherToFuture](https://stackoverflow.com/questions/58706276/execute-fluture-task-with-sancuary-either) ### Debugging Functionally * `x => console.log({ x }) || x` * `x => !console.log({ x }) && x` * `tap(console.log)` ## Reference * [sanctuary](https://sanctuary.js.org/) * [sanctuary-def](https://github.com/sanctuary-js/sanctuary-def) * [sanctuary-type-classes](https://github.com/sanctuary-js/sanctuary-type-classes) * [sanctuary-style](https://github.com/sanctuary-js/sanctuary-style) * [fluture](https://github.com/fluture-js/Fluture) * [fluture-sanctuary-types](https://github.com/fluture-js/fluture-sanctuary-types) * [fluture-node](https://github.com/fluture-js/fluture-node) * [fluture-express](https://github.com/fluture-js/fluture-express) * [fluture-hooks](https://github.com/fluture-js/fluture-hooks) * [booture](https://github.com/fluture-js/booture) * [daggy](https://github.com/fantasyland/daggy), [fantasy-land](https://github.com/fantasyland/fantasy-land), [ramda](https://ramdajs.com/docs/), [ramda-adjunct](https://github.com/char0n/ramda-adjunct) * [ramda->sanctuary](https://github.com/sanctuary-js/sanctuary/wiki/Ramda-to-Sanctuary) * [Combinators@Avaq](https://gist.github.com/Avaq/1f0636ec5c8d6aed2e45) --- ## More Resource * [Prof. Frisby's Course](https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript) * [Tom Harding's FantasyLand](http://www.tomharding.me/fantasy-land/) * [Eric Elliott's Composing SW](https://medium.com/javascript-scene/composing-software-the-book-f31c77fc3ddc) * [Parse, don't validate!](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/), -- [Type Safety](https://www.parsonsmatt.org/2017/10/11/type_safety_back_and_forth.html) * https://www.fluidinfunctional.com/ * [Monads in JS](https://curiosity-driven.org/monads-in-javascript) * [Monadic Transformation@LinkedIn](https://www.linkedin.com/pulse/monad-transformers-javascript-vladim%C3%ADr-gorej/) * [JS FP Design Pattern: Binding Functions (aka Monads!)](https://adambard.com/blog/javascript-design-pattern-binding-functions/) ## Advanced: Monadic Programming ### State, Reader, Writer Monad https://github.com/dicearr/monastic ### Do in JS https://github.com/russellmcc/fantasydo https://github.com/pelotom/burrido simple implementation ```javascript= // TODO: trampoline function Do (type) { return function (generator) { const gen = generator () return (function next (err, v) { if (err) console.error (err) const { done, value } = gen.next (v) return ( done ? S.of (type) (value) : /* otherwise */ S.chain (x => next (null, x) || S.of (type) (x)) (value) ) }) () } } ``` ## Terms * ROP (Railway Oriented Programming) * Monadic Programming * Continuation Passing Style * Functional Mixins * Recursion, Corecursion, X-morphisms * ... -- # Exercise ## Many-way map, filter, reduce 0. Immutability, Pointfree 1. map-filter-reduce * `map` by for-loop * `map` by resursion * `map` by `reduce` * `map` as an object method * `reduce` and `reduceLeft` * `map` for Containers (Identity Functor) * ... 2. compositions * `S.I`, `B`, `compose`, and `pipe` ## Type Inference Practice * Exercise: define <h,k> : c -> a x b via `S.lift2`, `S.Pair` ``` S.lift2 :: Apply f => (a -> b -> c) -> f a -> f b -> f c S.Pair :: a -> b -> Pair a b S.lift2 (S.Pair) :: f a -> f b -> f (Pair a b) For h, k... h :: c -> a k :: c -> b We have, S.lift2 (S.Pair) (h) (k) :: c -> Pair a b ``` * Exercise: Use `S.flip` (the C combinator) to do structured functions application `S.flip :: Functor f => f (a -> b) -> a -> f b` 1. The C combinator: ``` For op :: x -> y -> z Write op :: x -> (y -> z) ^^^^^^ a -> b S.flip (op) :: y -> x -> z ``` 2. For list of functions ``` fs :: [x -> y] ^^^^^^^^^^ f (x -> y) S.flip (fs) :: [x -> y] -> x -> [y] ``` 3. For StrMap of functions ``` fs :: StrMap (x -> y) S.flip (fs) :: (StrMap (x -> y)) -> x -> StrMap y ```