[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 AppStateand generate an Array of Cards.
liftA2 from crocks.js "Ever see yourself wanting to
mapa binary or trinary function, butmaponly allows unary functions? Both of these functions allow you to pass in your function as well as the number ofApplicatives (containers that provide bothofandapfunctions) 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']))
[Javascript] Multiply Two Arrays over a Function in JavaScript的更多相关文章
- Dreamweaver 扩展开发: Calling a C++ function from JavaScript
After you understand how C-level extensibility works in Dreamweaver and its dependency on certain da ...
- 理解callback function in javascript
以下内容主要摘自[1,2] (1)In javascript, functions are first-class objects, which means functions can be used ...
- JavaScript学习09 函数本质及Function对象深入探索
JavaScript学习09 函数本质及Function对象深入探索 在JavaScript中,函数function就是对象. JS中没有方法重载 在JavaScript中,没有方法(函数)重载的概念 ...
- JavaScript基础知识(JSON、Function对象、原型、引用类型)
19.JSON 概念:JavaScript 对象表示法(JavaScript Object Notation),是一种轻量级的数据交换格式 特点:易于程序员编写和查看:易于计算机解析和生成 数据结构 ...
- 理解javascript中的立即执行函数(function(){})()
之前看了好多代码,都有用到这种函数的写法,但是都没认真的去想为什么会这样写,今天开始想学习下jquery的源码,发现jquery也是使用这种方式,用(function(window, undefine ...
- javascript中的立即执行函数(function(){…})()
javascript中的立即执行函数(function(){…})() 深入理解javascript中的立即执行函数,立即执行函数也叫立即调用函数,通常它的写法是用(function(){…})()包 ...
- javascript 转化一个数字数组为function数组(每个function都弹出相应的数字)
javascript 转化一个数字数组为function数组(每个function都弹出相应的数字) var arrNum = [2,3,4,5,6,10,7]; var arrFun = []; f ...
- how to disabled alert function in javascript
how to disabled alert function in javascript alert 阻塞主线程 default alert; // ƒ alert() { [native code] ...
- 45 Useful JavaScript Tips, Tricks and Best Practices(有用的JavaScript技巧,技巧和最佳实践)
As you know, JavaScript is the number one programming language in the world, the language of the web ...
随机推荐
- POJ3321Apple Tree Dfs序 树状数组
出自——博客园-zhouzhendong ~去博客园看该题解~ 题目 POJ3321 Apple Tree 题意概括 有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作: 1.改变其 ...
- 初识(试)LoadRunner
一.安装和破解 1.傻瓜式安装.[注意:最好不要默认路径安装,因为64位的win7系统安装LR11时,会默认安装到“Program files (x86)”的目录中,该目录名称有空格,会导致录制“We ...
- 【JavaScript】jQuery
No1: jQuery能帮我们干这些事情: 消除浏览器差异:你不需要自己写冗长的代码来针对不同的浏览器来绑定事件,编写AJAX等代码: 简洁的操作DOM的方法:写$('#test')肯定比docume ...
- [ 高危 ] my网任意账户登陆
该网站的任意登录其实都已经提交得差不多了,本来以为这个漏洞会是一个重复的,然而试了一下发现思路奇葩. 任意登录,一般都为验证码爆破,4位手机验证码,而用于拦截的图片验证码没有或者可以重复使用,所以就能 ...
- 安排~~炒鸡全的JS兼容问题,码上-----【XUEBIG】
如何处理兼容问题 如果两个都是属性,用逻辑||做兼容 如果有一个是方法,用三元做兼容 如果是多个属性或方法,封装函数做兼容 两个小知识点: 1.取消拖拽的默认行为: document.ondragst ...
- hr相关的
1.自我介绍? 2.为什么要离职?之前几家公司离职的原因分别是什么? 3.从上一家公司离职的原因? 4.目前就职的公司最大的收获是什么? 从上家公司到目前公司的最大收获是什么,要突出目标明确,在当前的 ...
- JVM之基本结构
1. Java虚拟机的架构 1.1 Java的NIO库允许Java程序使用直接内存,访问直接内存的速度优于Java堆.出于性能的考虑,读写频繁的场合会考虑使用直接内存. 1.2 本地方法栈和Java栈 ...
- asp.net core 依赖注入实现全过程粗略剖析(3)
接着 前面,前面的过程是普遍常用的依赖注入解析过程,我们正常都是在startup类中注入依赖服务,但是,笔者这周开发的时候遇到个问题,不同服务的生命周期不同,不能调用服务.举个例子,AddDbCont ...
- SQL varbinary varchar 互转
--============================================== -- FUNCTION varbin2hexstr -- 将 varbinary 类型的数据转换为 v ...
- 治愈 JavaScript 疲态的学习计划【转载】
来源:伯乐在线 - Rose Wang 像其他人一样,最近我读了 Jose Aguinaga 的文章 <How it feels to learn JavaScript in 2016>. ...