[Ramda] Basic Curry with Ramda
var _ = R; /*****************************************
C U R R Y I N G E X A M P L E
******************************************/ // We've got a nice multiply function.
// It takes two arguments. console.log( _.multiply(3, 4) ); // But it has been secretly curried already
// so we can feed it fewer arguments and it
// will return a new function.
//
// How about making a function to double a
// value? Done.
var double = _.multiply(2); console.log( double(13) ); /*****************************************
Y O U R T U R N
******************************************/ // _.split pulls a string apart around a
// given value
console.log( _.split('i', 'mississippi') ); // -- Challenge 1 ------------------------
// Make a function called "words" which
// returns a list of words in a string.
// Use only the split function and
// currying. console.log("Testing challenge 1..."); var words = _.split(' '); // change this
assertEqualArrays(
['one', 'two', 'three'],
words('one two three')
);
console.log("passed"); // -- Challenge 2 ------------------------
// Create a function to triple every
// number in a list using only
// _.multiply and _.map. console.log("Testing challenge 2..."); var tripleList = _.map(_.multiply(3));
assertEqualArrays([3,6,9], tripleList([1,2,3])); console.log("passed"); // -- Challenge 3 ------------------------
// Create a function to find the largest
// number in a list. You can use the
// greater(a,b) function which returns the
// greater of its two inputs. You can do
// this with currying and one of the list
// functions _.map, _.filter, or _.reduce. console.log("Testing challenge 3..."); var greater = function(a,b) {
return a > b ? a : b;
}; var max = _.reduce(greater, -Infinity);
assertEqual(9, max([1,-3483,9,7,2]));
assertEqual(-1, max([-21,-3483,-2,-1])); console.log("passed"); console.log("All tests pass.");
/******************************************
B A C K G R O U N D C O D E
*******************************************/ function assertEqualArrays(x,y) {
if(x.length !== y.length) throw("expected "+x+" to equal "+y);
for(var i in x) {
if(x[i] !== y[i]) {
throw("expected "+x+" to equal "+y);
}
}
}
function assertEqual(x,y){
if(x !== y) throw("expected "+x+" to equal "+y);
}
[Ramda] Basic Curry with Ramda的更多相关文章
- [Ramda] Handle Errors in Ramda Pipelines with tryCatch
Handling your logic with composable functions makes your code declarative, leading to code that's ea ...
- [Ramda] Curry and Uncurry Functions with Ramda
Most of the functions offered by the ramda library are curried by default. Functions you've created ...
- [Ramda] R.project -- Select a Subset of Properties from a Collection of Objects in Ramda
In this lesson we'll take an array of objects and map it to a new array where each object is a subse ...
- 从函数式编程到Ramda函数库(二)
Ramda 基本的数据结构都是原生 JavaScript 对象,我们常用的集合是 JavaScript 的数组.Ramda 还保留了许多其他原生 JavaScript 特性,例如,函数是具有属性的对象 ...
- [Javascript] Monads
Monads allow you to nest computations. They are a pointed functor that adds mjoin and chain function ...
- [Javascript] What is JavaScript Function Currying?
Currying is a core concept of functional programming and a useful tool for any developer's toolbelt. ...
- js函数式编程(二)-柯里化
这节开始讲的例子都使用简单的TS来写,尽量做到和es6差别不大,正文如下 我们在编程中必然需要用到一些变量存储数据,供今后其他地方调用.而函数式编程有一个要领就是最好不要依赖外部变量(当然允许通过参数 ...
- dWebpack编译速度优化实战
当你的应用的规模还很小时,你可能不会在乎Webpack的编译速度,无论使用3.X还是4.X版本,它都足够快,或者说至少没让你等得不耐烦.但随着业务的增多,嗖嗖嗖一下项目就有上百个组件了,也是件很简单的 ...
- 2019年11个javascript机器学习库
Credits: aijs.rocks 虽然python或r编程语言有一个相对容易的学习曲线,但是Web开发人员更喜欢在他们舒适的javascript区域内做事情.目前来看,node.js已经开始向每 ...
随机推荐
- oracle 统计语句 与常见函数的归纳(未完待续)
一.统计语句 1. count count(*)与count(0)语句的区别: count(*)统计所有数量 count(0)统计第一列不为空的 2. 两个统计量的减法 select (select ...
- webdriver(python)学习笔记一
最近有python开发的项目,也正打算要学习自动化与python语言.因此想通过学习python版本的webdriver来一同学习. 学习过程中参考资料有乙醇的博客:https://github.co ...
- 【LeetCode 99】Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- WebStorm2016.1 破解 激活
WebStorm2016.1 破解 激活 方法来自 Rover12421 大神. 1.从官网下载WebStorm2016.1安装. 2.下载 破解补丁 并解压,记住路径 3.编辑WebStorm安 ...
- Google Maps API显示地图的小示例
来源:http://www.ido321.com/1089.html 效果(新版Firefox中测试): 代码: <!DOCTYPE> <html> <head> ...
- Leetcode Largest Number c++ solution
Total Accepted: 16020 Total Submissions: 103330 Given a list of non negative integers, arrange t ...
- Hive1.3 JDBC连接-代码片段
package com.hive.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Re ...
- 【Hadoop代码笔记】Hadoop作业提交之JobTracker接收作业提交
一.概要描述 在上一篇博文中主要描述了JobTracker接收作业的几个服务(或功能)模块的初始化过程.本节将介绍这些服务(或功能)是如何接收到提交的job.本来作业的初始化也可以在本节内描述,但是涉 ...
- 【hadoop代码笔记】Mapreduce shuffle过程之Map输出过程
一.概要描述 shuffle是MapReduce的一个核心过程,因此没有在前面的MapReduce作业提交的过程中描述,而是单独拿出来比较详细的描述. 根据官方的流程图示如下: 本篇文章中只是想尝试从 ...
- Codeforces 377
简单说一下. A 搜索出任意一个剩余细胞个数的联通块.剩下的填X. B 二分加贪心加数据结构. /* * Problem: * Author: Shun Yao */ #include <str ...