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. springboot(@Service,@Mapper)注解失效导致无法注入service和mapper

    给我来灵感的博客:感谢:http://blog.51cto.com/xingej/2053297?utm_source=oschina-app 因为使用了注解的类在使用时是通过new出来的,导致注解注 ...

  2. day40 mycql 视图,触发器,存储过程,函数

    视图,触发器,存储过程,自定义函数 -- 回顾 1.mysql 约束 1.非空 not null 2. 主键约束 primary key 3. 唯一约束 unique 4. 外键约束 foreign ...

  3. Cygwin命令

    Cygwin是一个用于在Windows上模拟Linux环境的软件. 通过cygwin,可以在windows环境下使用linux的程序,像find.tar等一些工具也可以在windows下使用,让我们可 ...

  4. Idea中lombok不生效原因

    我们可以通过在maven中插入配置信息 <dependency> <groupId>org.projectlombok</groupId> <artifact ...

  5. hdu1202解题报告

    #include<stdio.h>int main(){    int n,i;    double sum,s,p,sum1;    while(scanf("%d" ...

  6. POJ 1200 Crazy Search 【hash】

    <题目链接> 题目大意: 给定n,nc,和一个字符串,该字符串由nc种字符组成,现在要你寻找该字符串中长度为n的子字符串有多少种. 解题分析: 因为要判重,所以讲这些字符串hash一下,将 ...

  7. Jmeter实现Basic Auth方式登录

    背景 在实际测试日常中,产品有两种登录方式,一种是普通登录方式,另一种则是Basic Auth方式登录.两种登录对应着产品中不同的操作,对权限进行了一个划分. Postman登录 使用Postman登 ...

  8. 使用CCS调试基于AM335X的SPL、Uboot(原创)

    使用CCS调试基于AM335X的SPL.Uboot 一.开发环境 1.硬件平台:创龙AM3359核心板 2.SDK版本:ti-processor-sdk-linux-am335x-evm-03.00. ...

  9. CMake结合Visual Studio中开发Qt应用程序注意事项

    Qt工程管理 个人比较偏爱于使用CMake来管理C++工程,因为只要编写一个CMakeLists.txt文件,就可以在Windows和Mac上生成各自的IDE工程.在Windows上, CMake自然 ...

  10. C#如何Json转字符串;字符串转Json;Newtonsoft.Json(Json.Net)

    Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库(下载地址http://json.codeplex.com/). 下面是Json序列化和反序列化的简单封装: /// & ...