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的更多相关文章

  1. [Ramda] Handle Errors in Ramda Pipelines with tryCatch

    Handling your logic with composable functions makes your code declarative, leading to code that's ea ...

  2. [Ramda] Curry and Uncurry Functions with Ramda

    Most of the functions offered by the ramda library are curried by default. Functions you've created ...

  3. [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 ...

  4. 从函数式编程到Ramda函数库(二)

    Ramda 基本的数据结构都是原生 JavaScript 对象,我们常用的集合是 JavaScript 的数组.Ramda 还保留了许多其他原生 JavaScript 特性,例如,函数是具有属性的对象 ...

  5. [Javascript] Monads

    Monads allow you to nest computations. They are a pointed functor that adds mjoin and chain function ...

  6. [Javascript] What is JavaScript Function Currying?

    Currying is a core concept of functional programming and a useful tool for any developer's toolbelt. ...

  7. js函数式编程(二)-柯里化

    这节开始讲的例子都使用简单的TS来写,尽量做到和es6差别不大,正文如下 我们在编程中必然需要用到一些变量存储数据,供今后其他地方调用.而函数式编程有一个要领就是最好不要依赖外部变量(当然允许通过参数 ...

  8. dWebpack编译速度优化实战

    当你的应用的规模还很小时,你可能不会在乎Webpack的编译速度,无论使用3.X还是4.X版本,它都足够快,或者说至少没让你等得不耐烦.但随着业务的增多,嗖嗖嗖一下项目就有上百个组件了,也是件很简单的 ...

  9. 2019年11个javascript机器学习库

    Credits: aijs.rocks 虽然python或r编程语言有一个相对容易的学习曲线,但是Web开发人员更喜欢在他们舒适的javascript区域内做事情.目前来看,node.js已经开始向每 ...

随机推荐

  1. 抽屉显示控件SlidingDrawer入门

    SlidingDrawer是一个抽屉控件,代码具体路径为:android.widget.SlidingDrawer,该控件从API  Level3引入,在API 17及之后的版本将不再被支持.具体效果 ...

  2. asp调用.net xml web services

    来源:http://www.cnblogs.com/notus/archive/2006/08/10/473000.html#2662503 (是不是实际上可以用这个办法调用任何xml web ser ...

  3. 《Python 学习手册4th》 第六章 动态类型简介

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书)“重点笔 ...

  4. selenium打开带有扩展的chrome

    每当用跑用例失败的时候,第一反应就是查看元素定位是不是正确,帮助定位的扩展是必不可少的,但是selenium一般打开的是不带扩展的干净的浏览器,如果操作步骤很长的话,就得手动去执行直到那一步去检查元素 ...

  5. 【和我一起学python吧】Python 启航

    话说万张高楼平地起,不会走之前先学会爬吧.尤其对于我等的小菜同学来说更是这样,不管怎么先code first吧,等我等小菜们翅膀硬了才test first吧. 1, 怎么运行python? 先到pyt ...

  6. PHP:6种GET和POST请求发送方法

    在i94web博客中,我试过了畅言和多说两种社会化评论框,后来还是抛弃了畅言,不安全. 无论是畅言还是多说,我都需要从远程抓取文章的评论数,然后存入本地数据库.对于多说,请求的格式如下: // 获取评 ...

  7. iOS开发相关图书推荐

    Objective-C编程之道:iOS设计模式解析 作      者 [美] Carlo Chung 著:刘威 译 出 版 社 人民邮电出版社 出版时间 2011-11-01 版      次 1 页 ...

  8. hdu5909-Tree Cutting(树形dp)

    偷偷抄bestcoser上面hnust_zhaozhixuan的代码 = = 因为题解看不懂阿 #include <cstdio> #include <cstring> typ ...

  9. cocos2d-x生成随机数

            //获取系统时间         //time_t是long类型,精确到秒,通过time()函数可以获得当前时间和1970年1月1日零点时间的差         time_t tt; ...

  10. chrome 在home下生成 libpeerconnection.log

    chrome 在home下生成 libpeerconnection.log,比较烦恼. google了下,可以有方法绕过去,如下. /opt/google/chrome/google-chrome 找 ...