[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已经开始向每 ...
随机推荐
- (原创)LAMP教程2-安装虚拟机软件VirtualBox
大家好,今天我们讲的是第二章,安装虚拟机软件VirtualBox 我先讲一下我的电脑的环境,可以看下面的图片说明 大家也看到了我的机子是64位的win7系统(为什么讲这个,因为接下来我们要下载的是ce ...
- hdu 2594-Simpsons’ Hidden Talents(KMP)
题意: 给你两个串a,b,求既是a的前缀又是b的后缀的最长子串的长度. 分析: 很自然的想到把两个串连接起来,根据KMP的性质求即可 #include <map> #include < ...
- Jmeter教程
博客园 http://www.cnblogs.com/yangxia-test/category/431240.html 1)分析能力没有LoadRunner详细 2)jmeter不支持IP欺骗,而L ...
- Qt Style Sheets制作UI特效
使用Qt Style Sheets制作UI特效 博客出处:http://developer.nokia.com/community/wiki/%E4%BD%BF%E7%94%A8Qt_Style_S ...
- C++实现网格水印之调试笔记(三)—— 初有结果
错误: error C2338: THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD 这种错误 ...
- mysql操作时遇到的小问题
mysql数据库在程序中执行sql语句时,或者在执行sql时,数据库表可能会有一些特殊的字符,比如说#,.等,这样在执行时 可能会遇到问题 如以下的表名,backup_2014.2.22, 这个表在查 ...
- [LeetCode] Add Digits (a New question added)
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- linux进程调度函数浅析(基于3.16-rc4)
众所周知,进程调度使用schedule()函数来完成,下面我们从分析该函数开始,代码如下(kernel/sched/core.c): asmlinkage __visible void __sched ...
- ERROR (ClientException): Unexpected API Error
- jar,war,ear区别及java基础杂七八
jar,war,earqu区别 这三种文件都可以看作是java的压缩格式,其实质是实现了不同的封装: jar--封装类war--封装web站点ear--封装ejb.它们的关系具体为:jar: ...