[Javascript] Multiply Two Arrays over a Function in JavaScript
Just like the State ADT an Array is also an Applicative Functor. That means we can do the same tricks with liftA2
with Array
that we have been doing with State
.
While the Applicative aspect of State
allows use to combine multiple stateful transitions over a function, Array
allows us to create a new Array
that contains the results of calling every permutation of each element in two arrays over a function. We will use this ability to pull from two separate locations in our AppState
and generate an Array
of Card
s.
liftA2 from crocks.js "Ever see yourself wanting to
map
a binary or trinary function, butmap
only allows unary functions? Both of these functions allow you to pass in your function as well as the number ofApplicative
s (containers that provide bothof
andap
functions) you need to get the mapping you are looking for."lift from Ramda.js "lifts" a function of arity > 1 so that it may "map over" a list, Function or other object that satisfies the FantasyLand Apply spec.
// Crocks.js
const buildCard = curry((color, shape) => console.log(color, shape) || ({
id: `${color}-${shape}`,
color,
shape
}));
// buildCards :: [String] -> [String] -> [Card]
const buildCards = liftA2(buildCard);
// Ramda.js
const build = lift(buildCard);
---
const {prop,assoc, pick, State, identity, omit, curry, filter, converge,map, composeK, liftA2, equals, constant,option, chain, mapProps, find, propEq, isNumber, compose, safe} = require('crocks'); const {get, modify, of} = State; const {lift} = require('ramda'); const state = { colors: [ 'orange', 'green', 'blue', 'yellow' ], shapes: [ 'square', 'triangle', 'circle' ] }; const getState = (key) => get(prop(key)) const getColors = () => getState('colors').map(option([])); const getShapes = () => getState('shapes').map(option([])); const buildCard = curry((color, shape) => console.log(color, shape) || ({ id: `${color}-${shape}`, color, shape })); // buildCards :: [String] -> [String] -> [Card] const buildCards = liftA2(buildCard); const generateCards = converge( liftA2(buildCards), getColors, getShapes ) console.log( generateCards() .evalWith(state) ) const build = lift(buildCard); console.log('build', build([1,2,3], ['c', 'd']))
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2018-01-16 [Javascript] Transduce over any Iteratable Collection
2018-01-16 [Javascript] Improve Composition with the Compose Combinator
2017-01-16 [Angular Directive] Create a Template Storage Service in Angular 2
2017-01-16 [Angular Directive] Assign a Structual Directive a Dynamic Context in Angular 2
2017-01-16 [Angular Directive] Implement Structural Directive Data Binding with Context in Angular
2015-01-16 [MODx] 10. Using Babel for Muti-languages support