[Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge

When doing comparisons inside of functions, you end of relying heavily on the argument passed into the function. Ramda‘s converge allows you to do comparisons in a Point-Free style allowing you more flexibility with composing and constructing functions. This lesson walks through refactoring a function to Point-Free style using Ramda‘s Converge.

For example we want to find the whether the first item of an array is the biggest number:

const shouldBeTrue = [6, 3, 4, 5, 2, 1]
const shouldBeFalse = [3, 4, 5, 2, 1]

const isFirstBiggest = (xs) => xs[0] == xs.sort((a, b) => b - a)[0]

console.log(isFirstBiggest(shouldBeTrue)) // true
console.log(isFirstBiggest(shouldBeFalse)) // false

In the code we can see this:

const isFirstBiggest = (xs) => xs[0] === xs.sort((a, b) => b - a)[0]

You can find that, param ‘xs‘ appears both on the left and right side of euqals sign.

If match this partten, we actually can use ‘converge‘ from  Ramda.

invoked, this new function is applied to some arguments, each branching function is applied to those same arguments. The results of each branching function are passed as arguments to the converging function to produce the return value.

For example:

const shouldBeTrue = [6, 3, 4, 5, 2, 1]
const shouldBeFalse = [3, 4, 5, 2, 1]

import {
  converge, equals, head, sort, descend, identity, compose
}
from ‘ramda‘

const biggestNumberOfArray = compose(
  head,
  sort(descend(identity))
);
const isFirstBiggest = converge(
  equals, [
    head,
    biggestNumberOfArray
  ]
);

// xs =>
//     xs[0] == xs.sort((a, b) => b - a)[0]

console.log(isFirstBiggest(shouldBeTrue))
console.log(isFirstBiggest(shouldBeFalse))

So in the code:

converge(
  equals, [
    head,
    biggestNumberOfArray
  ]
)

converge takes two params, first params is ‘R.equals‘. It tells what should do with the second param. Here is checking whether they are equal.

Second param is an array, take tow expersions.

R.head --> xs[0]
biggestNumberOfArray --> xs.sort((a,b) => b-a)[0]

More example:

var average = R.converge(R.divide, [R.sum, R.length])
average([1, 2, 3, 4, 5, 6, 7]) //=> 4

var strangeConcat = R.converge(R.concat, [R.toUpper, R.toLower])
strangeConcat("Yodel") //=> "YODELyodel"

[Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge

时间: 2024-10-17 21:10:27

[Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge的相关文章

[Ramda] Declaratively Map Predicates to Object Properties Using Ramda where

Sometimes you need to filter an array of objects or perform other conditional logic based on a combination of factors. Ramda's where function gives you a concise way to declaratively map individual predicates to object properties, that when combined,

[Ramda] Convert Object Methods into Composable Functions with Ramda

In this lesson, we'll look at how we can use Ramda's invoker and constructNfunctions to take methods of an object and turn them into reusable utility functions that are curried and accept their object as the last argument. We'll convert a dot-chained

App Extension编程指南(iOS8/OS X v10.10):常见问题的处理方案

本节由CocoaChina翻译组成员DevTalking (博客)翻译自苹果官方文档App Extension Programming Guide--Handling Common Scenarios一节,敬请勘误.欢迎加入我们的翻译小组,详情请参看:CocoaChina编辑和译者招募! 当编写自定义代码以执行app扩展任务时,你可能需要处理一些其他多种类型扩展也会出现的情况.在这一章节中,我们将帮助你如何应对和处理这些常见的问题. 使用内嵌框架共享代码 你可以创建一个内嵌框架,用于在应用扩展和

javascript的Function 和其 Arguments

http://shengren-wang.iteye.com/blog/1343256 javascript的Function属性:1.Arguments对象2.caller 对调用单前函数的Function的引用,如果是顶层代码调用, 则返回null(firefox返回undefined). 注:只有在代码执行时才有意义3.length 声明函数是指定的命名参数的个数(函数定义是,定义参数的个数)4.prototype 一个对象,用于构造函数,这个对象定义的属性和方法 由构造函数创建的所有对象

Google C++ Style Guide----英文版

转载请注明出处<http://blog.csdn.net/qianqin_2014/article/details/51354326> Background C++ is the main development language used by many of Google's open-source projects. As every C++ programmer knows, the language has many powerful features, but this power

Google C++ Style Guide的哲学

Google C++ Style Guide并不是一个百科全书,也不是一个C++使用指南,但它描述适用于Google及其开源项目的编码指南,并不追求全面和绝对正确,也有许多人置疑它的一些规则.但作为一个最具影响力的编码规范,它里面有许多内容值得我们研究学习. 以下主要摘自GSG负责人Titus Winters在CppCon 2014上的演讲. 制订Google C++ Style Guide的目的 引导开发去做对的事,同时不易犯错. 哲学总结 关注于读者,而非作者 (Optimize for t

Google JavaScript Style Guide

转自:http://google.github.io/styleguide/javascriptguide.xml Google JavaScript Style Guide Revision 2.93 Aaron Whyte Bob Jervis Dan Pupius Erik Arvidsson Fritz Schneider Robby Walker Each style point has a summary for which additional information is ava

Javascript学习之函数(function)

在JS中,Function(函数)类型实际上是对象;每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象的指针. 一 函数的声明方式 //1.函数声明方式 function add(num1,num2){ return num1+num2; } //2.函数表达式定义函数 var add= function(num1,num2){ // 通过变量box即可引用函数; return num1+num2; }; // 注

[Transducer + Ramda] Write highly performance / functional code by using transducer-js and ramda.js libs

Tansducer-js lib A high performance Transducers implementation for JavaScript. Transducers are composable algorithmic transformations. They are independent from the context of their input and output sources and specify only the essence of the transfo