[Compose] 15. Applicative Functors for multiple arguments
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:
const res = Box(x => x +1).ap(Box(2))// Box(3);
We want to use a funciton 'ap' (apply) on Box. And x will be 2.
To define 'ap' function.
const Box = x => ({ chain: f => f(x), ap: other => other.map(x), map: f => Box(f(x)), fold: f => f(x), inspect: () => `Box(${x})` })
So '
Box(x => x +1).ap(Box(2))
'
Can be translated to:
Box(2) => Box(2).map(x => x + 1);
This can be useful when apply curry function:
const res = Box(x => y => x + y).ap(Box(1)).ap(Box(2)); console.log(res.inspect()); //Box(3)
after apply .ap(Box(1)), it becomes to:
Box(y => 1 +y).ap(Box(2))
after apply .ap(Box(2)), it becomes to:
Box(1 +2 )
It ends up, we have a function and continue to using 'ap':
const add = x => y => x + y; const res = Box(add).ap(Box(1)).ap(Box(2));
This partten is called click-functor!
The rule is:
F(val).map(fn) === F(fn).ap(F(val))
For example now we have:
const liftA2 = (fn, Fx, Fy) => F(fn).ap(Fx).ap(Fy);
The problem is we don't know what 'F' it is here...
So what we can do is transform accorind to the rule we have:
const liftA2 = (fn, Fx, Fy) => Fx.map(fn).ap(Fy)
Therefore we don't need to memtion any Functor.
Example:
const res2 = liftA2(add, Box(1), Box(2)); console.log(res2.inspect()); //Box(3)
Applicate Functor is really good to work with Async functor, because async by natural, data arrives different time:
const add = x => y => z=> x + y + z; const addAsyncNumbers = liftA3(add); const res = addAsyncNumbers( Async.of(5), Async((_, res) => { setTimeout(() => { console.log('resolve 2'); res(2) }, 500) }), Async((_, res) => { setTimeout(() => { console.log('resolve 3'); res(3) }, 600) })); res.fork(e => console.error(e), x => console.log('async', x)) // 10
相关文章
- Educational Codeforces Round 132 (Rated for Div. 2) A·B·C
- Keka for Mac(压缩解压工具) v1.2.58中文版
- Tenorshare UltData for Mac(专业iOS数据恢复工具)v9.7.15中文激活版
- Default Folder X for Mac(mac专业搜索优化工具)v6.0d21激活版
- AutoCAD 2024 for Mac(cad2024) v2024.3.61.182中文激活版
- Days for mac(菜单栏日历提醒软件)
- iStat Menus for Mac(系统状态信息监测工具)
- ORA-24369: required callbacks not registered for one or more bind handles ORACLE 报错 故障修复 远程处理
- ORA-29375: sum of values string for level string, plan string exceeds string ORACLE 报错 故障修复 远程处理
- ORA-55339: a virtual model already exists for the specified model-rulebase combination ORACLE 报错 故障修复 远程处理
- ORA-06904: CMX: no transport address available for remote application ORACLE 报错 故障修复 远程处理
- ORA-13052: unsupported geometric type for geometry string.string ORACLE 报错 故障修复 远程处理
- ORA-14755: Invalid partition specification for FOR VALUES clause. ORACLE 报错 故障修复 远程处理
- MySQL使用For循环实现数据操作(mysql写for循环)
- 的应用使用For语句提高Linux效率(linux中for语句)
- 微信 for Android v7.0.15 Google Play 正式版
- Zabbix安装(四):Zabbix Agent For Linux
- StepbyStep Guide to Configuring Oracle for Optimal Performance(oraclecfg)
- 与Oracle中的FOR语句实现数据删除(oracle中for删除)
- 学习Oracle中的For语句搭建数据库应用的基础(oracle for语法)