[Functional Programming] From simple implementation to Currying to Partial Application
Let's say we want to write a most simple implementation 'avg' function:
const avg = list => {
let sum = 0;
for(let i = 0; i < list.length; i++) {
sum += list[i]
}
return sum / list.length
}
Basiclly, the 'avg' function doing two things:
- Calculate sum
- Divide sum / length
It works fine for tiny / small application, but for the large application, we need to think about reuseablitiy. We want to breakdown one function and think about any reuseable partten, which later can be reused.
In the following examples, We want to bring in two libarays which are commonly used in FP. One is Ramda, another one is Crocks.
Currying:
First, we want to write 'sum' and 'devide' functions by ourselves:
const { curry, reduce, compose } = require("crocks");
const R = require("ramda"); const sum = reduce(R.add, 0);
// divideByLen :: [Number] -> Number -> Number
const divideByLen = curry(
compose(
R.flip(R.divide),
R.length
)
);
'sum' is simple, using 'reduce' from Crocks, you can also write JS reduce, doesn't matter.
What we need to explain is 'divideByLen' function.
- Why 'curry'?
Basic we want to call divideByLen in two ways:
divideByLen([1,2,3], sum([1,2,3]))
divideByLen([1,2,3])(sum([1,2,3]))
[Notice] You need to bring in 'curry' from Crocks, it is more flexable.
- Why 'flip'?
Because R.divide(sum, length), we need to feed the divide function with sum as first argement, then length as second arguement. But when we write code, length will be feeded frist, sum will be partially applied, it will come second, therefore we need to call 'flip'.
Bring all together:
const avg = list =>
compose(
divideByLen(list),
sum
)(list);
We notice that, we have to pass 'list' to both Sum(list) and divideByLen(list). The code looks not so good. Whenever you are facing the situation, you need to pass the same arguement to two functions in parallel. You can consider to using 'Partial Application'.
Partial Application:
// Ramda
const avg = R.converge(R.divide, [R.sum, R.length]);
We are using 'Ramda's converge' function, bascilly you have pass in a data, the data will be passed to R.sum(data) & R.length(data), the return results of those two functions, will be passed to R.divide(resOfSum, resOfLength).
//Crocks:
const { curry, fanout, merge, compose } = require("crocks"); const avg = compose(
merge(R.divide),
fanout(R.sum, R.length)
);
We are using the Pair ADT, the data will be passed to R.sum(data) & R.length(data) thought 'fanout' function, it returns Pair(resOfSum, resOfLength).
Then we use 'merge', it works with Pair ADT, we merge two results by R.divide(resOfSum, resOfLength).
[Functional Programming] From simple implementation to Currying to Partial Application的更多相关文章
- Currying vs Partial Application
柯里化相当于函数重构: 偏函数相当于函数适配. So, what is the difference between currying and partial application? As we s ...
- [Functional Programming] Write simple Semigroups type
An introduction to concatting items via the formal Semi-group interface. Semi-groups are simply a ty ...
- [Functional Programming] Compose Simple State ADT Transitions into One Complex Transaction
State is a lazy datatype and as such we can combine many simple transitions into one very complex on ...
- Functional Programming without Lambda - Part 1 Functional Composition
Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
- a primary example for Functional programming in javascript
background In pursuit of a real-world application, let’s say we need an e-commerce web applicationfo ...
- Functional programming
In computer science, functional programming is a programming paradigm, a style of building the struc ...
- BETTER SUPPORT FOR FUNCTIONAL PROGRAMMING IN ANGULAR 2
In this blog post I will talk about the changes coming in Angular 2 that will improve its support fo ...
随机推荐
- Java列表、数组、字符串
列表(list) list中添加,获取,删除元素 添加方法是:.add(e): 获取方法是:.get(index): 删除方法是:.remove(index), 按照索引删除: .remove(Obj ...
- Chakra GC内存管理(未完)
这一部分是我在网上找Chakra资料的时候偶然发现的zenhumany师傅在Hitcon2015上的议题<Microsoft Edge MemGC Internals>,感觉正好可以了解一 ...
- Android动态设置纯色图标的颜色
https://blog.csdn.net/qq_20082961/article/details/73718363 以前做了一个流量悬浮窗,悬浮窗里有当前网络状态的图标和网速的文字,想实现改变文字颜 ...
- hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)
Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在 ...
- 【AtCoder】ARC082
C - Together 用一个数组记一下一个数给它本身,左右贡献都是1,看看哪个数的总贡献最大 #include <bits/stdc++.h> #define fi first #de ...
- CI入门
CI入门 一.[查]按条件获取一条记录 获取数据有返回数组形式或对象形式,row_array().result_array()是以数组形式返回:row().result()是以对象形式返回.同样的,更 ...
- 洛谷 P1352 没有上司的舞会【树形DP】(经典)
<题目链接> <转载于>>> > 题目描述: 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的 ...
- Codeforces 862B (二分图染色)
<题目链接> 题目大意: 给出一个有n个点的二分图和n-1条边,问现在最多可以添加多少条边使得这个图中不存在自环,重边,并且此图还是一个二分图. 解题分析: 此题不难想到,假设二分图点集数 ...
- Gephi可视化(二)
继在园子里写的<Gephi可视化(一)——使用Gephi Toolkit创建Gephi应用>介绍了如何使用Gephi Toolkit工具集进行可视化编程后,本篇对Gephi Toolkit ...
- C#如何打开一个窗体,同时关闭该窗体