We annihilate the need for the ol' nested for loop using Applicatives. For example we have this kind of nested loop code: for(x in xs){ for(x in ys){ for(z in zs){ } } } We can refactor it by using List comprehension: ,,])); console.log(res1) // List…
Working our way backwards from solution to problem, we define an applicative functor, then use it to apply a function of multiple arguments. For example we have this line of code: ).ap(Box())// Box(3); We want to use a funciton 'ap' (apply) on Box. A…
What is applicative functor: the ability to apply functors to each other. For example we have tow functors: Container(2), Container(3) // We can't do this because the numbers are bottled up. add(Container.of(), Container.of()); // NaN We cannot just…
Let's examine a pointfree way to write these applicative calls. Since we know map is equal to of/ap, we can write generic functions that will ap as many times as we specify: const liftA2 = curry((g, f1, f2) => f1.map(g).ap(f2)); const liftA3 = curry(…
In functional programming, we create large functions by composing small functions; in object-oriented programming, we create large objects by composing small objects. They are different types of composition. They might sound similar, but there is som…
We find a couple of DOM nodes that may or may not exist and run a calculation on the page height using applicatives. For example we want to get the main content section size by reduce the height of header and footer, nomarlly we will do like this: 1.…
引言 转入Scala一段时间以来,理解Functor.Applicative和Monad等概念,一直是我感到头疼的部分.虽然读过<Functors, Applicatives, And Monads In Pictures> 一文,但深感未得甚解,仍是翻书了然.关书茫然. 于是转而"曲线救国"--选择学习更加纯粹的FP语言Haskell,尝试结合着对范畴论的一点粗鄙理解,再从Haskell与Scala不同编码实现的角度去比较,结果真有柳暗花明又一村的感觉.参考书籍为著名的&…