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. 删除Docker中所有已停止的容器

    方法一: #显示所有的容器,过滤出Exited状态的容器,取出这些容器的ID, sudo docker ps -a|grep Exited|awk '{print $1}' #查询所有的容器,过滤出E ...

  2. Cforeach的详细用法--【转】

    运行截图: <!-- 多选框-还需要实现true选中和分行 --> <c:forEach items="${users}" var="item" ...

  3. python3解析库lxml

    阅读目录 1.python库lxml的安装 2.XPath常用规则 (1)读取文本解析节点 (2)读取HTML文件进行解析 (3)获取所有节点 (4)获取子节点 (5)获取父节点 (6)属性匹配 (7 ...

  4. vue源码的构建

    一.vue构建的基本了解 1,开始学习vue的源码的学习,vue.js是基于rollup构建的 它的配置在 scripts下面 rollup是webpack的简小版针对于js进行压缩的,没有提供复杂的 ...

  5. 上海市2019年公务员录用考试第一轮首批面试名单(A类)

    上海市2019年公务员录用考试第一轮首批面试名单(A类) 注册编号 总成绩 注册编号 总成绩 注册编号 总成绩 注册编号 总成绩 4016574 127.4 5112479 145.9 5125732 ...

  6. Maven使用lib下的包

    Maven使用中央仓库的同时,使用lib下的包 pom.xml添加如下配置 <build> <plugins> <plugin> <artifactId> ...

  7. django-debug-toolbar和Django 日志配置

    django-debug-toolbar介绍 django-debug-toolbar 是一组可配置的面板,可显示有关当前请求/响应的各种调试信息,并在单击时显示有关面板内容的更多详细信息. gith ...

  8. BZOJ.5338.[TJOI2018]xor(可持久化Trie)

    BZOJ LOJ 洛谷 惊了,18年了还有省选出模板题吗= = 做这题就是练模板的,我就知道我忘的差不多了 询问一就用以DFS序为前缀得到的可持久化Trie做,询问二很经典的树上差分. 注意求询问二的 ...

  9. 函数指针的理解 from 数据结构

    今天在学习数据结构中遇到一些问题,函数的指针不知道怎么用,给自己科普一哈 1 int LocateElem_Sq(SqList L, LElemType_Sq e, Status(*Compare)( ...

  10. React Native小白入门学习路径——一

    前言 过去这段时间一直忙着实验室考核任务,拼尽全力完成了自己的任务之后.正准备开始高强度的实验室的学习的时候,实验室组织了新老生交流会,这也应该是头一次这么近距离的面对大四前辈交流想法.感觉自己受益颇 ...