[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']))
[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 ...
随机推荐
- NOIP2017提高组Day1T3 逛公园 洛谷P3953 Tarjan 强连通缩点 SPFA 动态规划 最短路 拓扑序
原文链接https://www.cnblogs.com/zhouzhendong/p/9258043.html 题目传送门 - 洛谷P3953 题目传送门 - Vijos P2030 题意 给定一个有 ...
- Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第一集之安装VMware】
[Java全技术路线开发]:https://jq.qq.com/?_wv=1027&k=5knQcPc 由于自己已经安装了VMware,本想转载一篇百度经验的教程,但是我对安装过程有些异议 ...
- Hadoop| MapReduce01 概述
概述 分布式运算程序: 优点:易于编程:良好扩展性:高容错性:适合PB级以上海量数据的离线处理: 缺点:不擅长实时计算:不擅长流式计算:不擅长DAG有向图计算: 核心思想: 1)分布式的运算程序往往需 ...
- echarts入门
一直好奇,今晚就学习了一番,算是入门的级别,学习总是一个渐进的过程. 一路记录,一路足迹. 一:学习资料 1.主要参考的代码 https://github.com/shengxinjing/imooc ...
- 基于Keil软件的MCU环境搭建
我们在开发一款新的MCU的时候,偶尔会遇到Keil软件没有对应的Device设备选型,以下,我们以STM32F407VGT6作为实例来演示整个环境的搭建过程: 一.如下所示,我需要选择的是ST公司的S ...
- Shell学习之条件测试(四)
Shell学习之条件测试 目录 逻辑测试 文件测试 数值比较 字符串比较 逻辑测试 格式: [ 表达式 ] 操作符 [ 表达式2 ] …… 命令1 操作符 命令2 …… 常用的操作符 ( 注意:-a和 ...
- RIPng 配置
一.拓扑图 二.配置过程 1)首先将各个端口的IP地址按照如上图所示配置完毕 2)在全局模式下创建rip ipv6 router rip [name] 3)在各个路由器上进入所连接的端口启用RIPng ...
- ES2018正则表达式更新
如果你是一个初学者,这篇文章可以拓展你对正则表达式用法的理解,不过建议你先阅读一些正则表达式入门文章,比如经典的<正则表达式30分钟入门教程>.如果你对正则表达式有一定的认识,那么这篇文章 ...
- Java中的泛型 --- Java 编程思想
前言 我一直都认为泛型是程序语言设计中一个非常基础,重要的概念,Java 中的泛型到底是怎么样的,为什么会有泛型,泛型怎么发展出来的.通透理解泛型是学好基础里面中非常重要的.于是,我对<Ja ...
- 【C#】Convert.ToInt32、(int)和int.Parse三者的区别
前者适合将object类类型转换成int类型 (int)适合简单数据类型之间的转换: int.Parse适合将string类类型转换成int类型.