Learning notes. Video.

Less than:

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

  1. 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'.

  1. const {flip} = require('crocks')
  2. const {lt} = require('ramda')
  3.  
  4. // isLessTen :: Number -> Boolean
  5. const isLessTen = flip(lt, 10)
  6. isLessThen(9) // true

If/Else:

  1. const diff10 = v => {
  2. let result = null;
  3. if (v < 10) {
  4. result = v - 10
  5. } else {
  6. result = v + 10
  7. }
  8. }

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

  1. const {not, ifElse} = require('crocks');
  2. const {add, lt} = require('ramda');
  3.  
  4. const declarative = ifElse(
  5. not(flip(lt, 10)), // if the given number is greater than 10
  6. add(10), // then plus 10
  7. add(-10) // go negitive
  8. )

or/and:

  1. /**
  2. * Or && And
  3. */
  4. // Just check one object has length prop is not enough
  5. // Because Array has length, function has length
  6. // Array is also object
  7. const _hasLengthProp = x =>
  8. (isObject(x) && x.length !== undefined) || isArray(x);
  9.  
  10. // hasLengthProp :: a -> Boolean
  11. const hasLengthProp = or(isArray, and(isObject, hasProp('length')));
  12. 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:

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

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

  1. const _isArray = x => Object.prototype.toString.call(x) === '[object Array]';
  2. 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. 2018牛客网暑假ACM多校训练赛(第三场)D Encrypted String Matching 多项式 FFT

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-D.html 题目传送门 - 2018牛客多校赛第三场 D ...

  2. JavaSE| String常用方法

    字符串 * java.lang.String类型:字符串类型 * 1.String类型是final修饰,不能被继承的 * 2.Java 程序中的所有字符串字面值(如 "abc" ) ...

  3. hibernate.properties not found

    在配置hibernate的主键生成策略的时候突然报出如下错误,寻找了很长时间,虽然不是什么严重的错误,但是希望可以警醒自己 问题: 11:26:21,611 INFO Version:37 - HHH ...

  4. POJ 2352 Stars【树状数组】

    <题目链接> 题目大意: 题目给出n个点,这些点按照y坐标的升序,若y相同,则按照x的升序顺序输入,问,在这些点中,左下角的点的数量分别在0~n-1的点分别有多少个,写出它们的对应点数. ...

  5. mysql底层原理

    1.索引底层数据结构B+Tree详解 索引的本质 索引是帮助MySQL高效获取数据的排好序的数据结构 索引存储在文件里 索引的结构 二叉树 (红黑树[平衡二叉树]).HASH.BTREE[多叉树] 索 ...

  6. Django项目中使用Redis

    Django项目中使用Redis DjangoRedis 1 redis Redis 是一个 key-value 存储系统,常用于缓存的存储.django-redis 基于 BSD 许可, 是一个使 ...

  7. Callable与Runable接口 submit与execute区别

    execute(Runnable x) 没有返回值.可以执行任务,但无法判断任务是否成功完成. submit(Runnable x) 返回一个future.可以用这个future来判断任务是否成功完成 ...

  8. 彻底理解this 的值到底是什么?

    作者:方应杭 来源:知乎 你可能遇到过这样的 JS 面试题: var obj = { foo: function(){ console.log(this) } } var bar = obj.foo ...

  9. Nginx 反向代理 -- 一路上的坑

    前些天刚过来新公司上班,公司的项目都挺多的,只不过项目都是第三方公司团队开发的,现在本公司要组建自己的团队,我作为一个Java后台人员去接手第三方公司的全部项目,我已经是直接崩溃了(先解释一下我崩溃的 ...

  10. 常用的Lambda表达式

     Java 8 引入Lambda表达式,对于Java开发人员来说是一大福利,简化了代码,提高了开发效率. 本文主要讲解日常开发中使用频率比较高的几类Lambda表达式. 集合 Lambda表达式的引入 ...