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. 咸鱼入门到放弃1--JDBC

    JDBC参考微博https://www.cnblogs.com/surfcater/p/10224502.html 主要内容 1.JDBC相关概念 2.JDBC常用接口 driver connecti ...

  2. BZOJ2724 [Violet 6]蒲公英 分块

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2724.html 题目传送门 - BZOJ2724 题意 求区间最小众数,强制在线. $n$ 个数,$m ...

  3. 008 RestFul API 拦截器

    一:任务 1.任务 过滤器Filter 拦截器Interceptor 切片Aspect 二:过滤器 1.新建包 2.自定义过滤器程序 加了注解,这个过滤器在springboot中就起作用了 packa ...

  4. svn图标的含义

    http://www.cnblogs.com/genhaosan/articles/5129791.html

  5. 用面向对象重写thread 实现多次调用一个线程

    思路: 利用thread类中,run方法在子线程中调用,其他方法在主线程调用,所以将生产者写入主线程,将消费者写入run函数中在子线程中执行,完成生产者消费者模型 注意: 1. 要在 init 函数中 ...

  6. Scala-Unit7-Scala并发编程模型AKKA

    一.Akka简介 Akka时spark的底层通信框架,Hadoop的底层通信框架时rpc. 并发的程序编写很难,但是Akka解决了spark的这个问题. Akka构建在JVM平台上,是一种高并发.分布 ...

  7. java.util.List API解读

    list的API 如下: 下面是我对这段API的翻译 An ordered collection (also known as a sequence). 一个有序的集合(也被称为序列) The use ...

  8. UML图快速入门

    UML(Unified Modeling Language)统一建模语言的概念已经出现了近20年,虽然并不是所有的概念都非常有实践意义,但常见的用例图.类图.序列图和状态图却实实在在非常有效,是项目中 ...

  9. Nagios安装与配置

    安装包获取 Nagios https://sourceforge.net/projects/nagios/files/ Nagios Plugins https://www.nagios.org/do ...

  10. AE文件特别大

    解决AE的输出文件太大_百度经验 在渲染队列的输出组件中把格式选项改为H.264,然后你会得到一个MP4文件. 然后最关键的一步,打开格式选项,把目标比特率和最大比特率均设10(比特率越高,视频质量越 ...