[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已经开始向每 ...
随机推荐
- c# 进行AE开发时,如何在地图上定位出一个点
一.文本形式的气泡提示框 由于本人是初学,所以具体的含义尚未弄清楚,直接给出代码吧!
- Android中TabHost嵌套TabHost
在嵌套TabHost时,先后遇到了以下情况: 问题1:内部TabHos无显示,只显示了其中的一个Activity: 解决:按下文比对主子TabHos的布局文件和java文件并修改: 问题2:如上所做后 ...
- Android欢迎界面的创建方法
1.制作一张启动图片splash.png,放置在res->drawable-hdpi文件夹中.2.新建布局文件splash.xml <?xml version="1.0" ...
- setImageResource和setImageDrawable区别
ImageView设置图片的方式有很多钟,可以在xml里面写android:src=”@drawable/xxx”,也可以在java代码里面设置. 在java里面的设置方式也有多种,方法包括:setI ...
- Codeforces Round #361 (Div. 2) 套题
A - Mike and Cellphone 问有没有多解,每个点按照给出的序列用向量法跑一遍 #include<cstdio> #include<cstring> #incl ...
- python的Requests模块使用tips
post方法提交的是表单,要用data放dict get方法请求的是参数,要用params放dict HTTP头部是大小写不敏感的
- C ~ char int 等数据转换问题
1,char型数字转换为int型 "; printf(]-');//输出结果为3 2,int转化为char (1)字符串转换成数字,用atoi,atol,atof,分别对应的是整型,long ...
- 《锋利的Jquery第二版》读书笔记 第一章
按照书本介绍顺序整理jquery库相关的语法.要点. window.onload与$(document).ready()功能类似,前者需要所有资源加载完毕,且不能同时编写多个:后者加载完DOM结构即执 ...
- CSS Sprite的优缺点分析
目前大多数的开发人员对这个技术都有相当地掌握,也有很多关于它的教程和文章.几乎所有的文章中都宣称设计师和开发人员都应该使用 CSS sprite 来减少 HTTP 请求数,并且节省一些流量.这个技术被 ...
- JAVA与数据库开发(JDBC-ODBC、SQL Server、MySQL)
1)配置数据库环境和驱动 2)设计数据库结构并创建数据库 3)对数据库进行增删改查操作...