Learning notes. Video.

Less than:

If you use 'ramda', you maybe know 'lt, gt'..

R.lt(2, 1); //=> false

Is '2' less than '1' , the result is false. We can see that the data is actually come first which is 2.

Normally in FP, we want data come last. What we can do is using 'flip' from 'crocks.js'.

const {flip} = require('crocks')
const {lt} = require('ramda') // isLessTen :: Number -> Boolean
const isLessTen = flip(lt, 10)
isLessThen(9) // true

If/Else:

const diff10 = v => {
let result = null;
if (v < 10) {
result = v - 10
} else {
result = v + 10
}
}

We can use 'ifElse' from 'crocks.js':

const {not, ifElse} = require('crocks');
const {add, lt} = require('ramda'); const declarative = ifElse(
not(flip(lt, 10)), // if the given number is greater than 10
add(10), // then plus 10
add(-10) // go negitive
)

or/and:

/**
* Or && And
*/
// Just check one object has length prop is not enough
// Because Array has length, function has length
// Array is also object
const _hasLengthProp = x =>
(isObject(x) && x.length !== undefined) || isArray(x); // hasLengthProp :: a -> Boolean
const hasLengthProp = or(isArray, and(isObject, hasProp('length')));
log(hasLengthProp([])) // true

[100] === [100]?

The Answer is : false

JS consider each [] is a new Object.

In this case, we can use 'propEq' from 'crocks.js' to save us some safe checking:

const _aIs100A = x => isObject(x) && x.a === [100];
log(
_aIs100A({a: [100]})
) // false, because it consider [100] is a new object
const aIs100A = and(isObject, propEq('a', [100]))
log(
aIs100A({a: [100]}) // true
)

ES5 way to check Array is typeof Array, and Date is typeof Date:

const _isArray = x => Object.prototype.toString.call(x) === '[object Array]';
const _isDate = x => Object.prototype.toString.call(x) === '[object Date]';

[Functional Programming] Functional JS - Pointfree Logic Functions的更多相关文章

  1. [Functional Programming] Using JS, FP approach with Arrow or State Monad

    Using Naive JS: const {modify, get} = require('crocks/State'); const K = require('crocks/combinators ...

  2. [Functional Programming Monad] Substitute State Using Functions With A State Monad (get, evalWith)

    We take a closer look at the get construction helper and see how we can use it to lift a function th ...

  3. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

  8. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  9. Java 中的函数式编程(Functional Programming):Lambda 初识

    Java 8 发布带来的一个主要特性就是对函数式编程的支持. 而 Lambda 表达式就是一个新的并且很重要的一个概念. 它提供了一个简单并且很简洁的编码方式. 首先从几个简单的 Lambda 表达式 ...

随机推荐

  1. NOIP2016提高组Day1T2 天天爱跑步 树链剖分 LCA 倍增 差分

    原文链接https://www.cnblogs.com/zhouzhendong/p/9275606.html 题目传送门 - 洛谷P1600 题目传送门 - LOJ#2359 题目传送门 - Vij ...

  2. BZOJ1433 [ZJOI2009]假期的宿舍 二分图匹配 匈牙利算法

    原文链接http://www.cnblogs.com/zhouzhendong/p/8372785.html 题目传送门 - BZOJ1433 题解 我们理一理题目. 在校的学生,有自己的床,还可以睡 ...

  3. [转,讲的非常精彩]CIDR地址块及其子网划分(内含原始IP地址分类及其子网划分的介绍)

    http://blog.csdn.net/dan15188387481/article/details/49873923 CIDR地址块及其子网划分(内含原始IP地址分类及其子网划分的介绍)   1. ...

  4. 04. Pandas 3| 数值计算与统计、合并连接去重分组透视表文件读取

    1.数值计算和统计基础 常用数学.统计方法 数值计算和统计基础 基本参数:axis.skipna df.mean(axis=1,skipna=False)  -->> axis=1是按行来 ...

  5. JavaSE| 包装类| 时间

    包装类 * 因为Java是面向对象的语言,所以很多API或新特性都是针对“对象”来设计的,但是Java设计之初时,是C语言很盛行的时候,所以Java保留了C语言中的8种基本数据类型, 保留它们有好处, ...

  6. CSS规范 - 分类方法

    CSS文件的分类和引用顺序 通常,一个项目我们只引用一个CSS,但是对于较大的项目,我们需要把CSS文件进行分类. 我们按照CSS的性质和用途,将CSS文件分成“公共型样式”.“特殊型样式”.“皮肤型 ...

  7. HDU 5592 ZYB's Game 【树状数组】+【二分】

    <题目链接> 题目大意: 给你一个由1~n,n个数组成的序列,给出他们每个的前缀逆序数,现在要求输出这个序列. 解题分析: 由前缀逆序数很容易能够得到每个数的逆序数.假设当前数是i,它前面 ...

  8. The more, The Better

    The more, The Better依赖背包+树形dpf[x][j+1]=max(f[x][j+1],f[x][j+1-k]+f[i->n][k]);我的一个疑问就是这k个节点会不会选重复, ...

  9. 语音VLAN异常流量分析

    现象描述:有线网络里,接入层交换机上,凡是被划入语音vlan的端口,都会呈现出相同的流量表现,而且瞬间流量都很大,30 - 40 MB级别,对有线网络的稳定性有很大的影响. 设备型号: IP 电话:A ...

  10. BZOJ.3227.[SDOI2008]红黑树tree(树形DP 思路)

    BZOJ orz MilkyWay天天做sxt! 首先可以树形DP:\(f[i][j][0/1]\)表示\(i\)个点的子树中,黑高度为\(j\),根节点为红/黑节点的最小红节点数(最大同理). 转移 ...