[Functional Programming] Functional JS - Pointfree Logic Functions
JS Programming functional Functions Logic
2023-09-14 08:59:14 时间
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]';
相关文章
- table框架调用js clob字段的使用
- JS框架_(JQuery.js)网页文字评论弹幕
- JS框架_(coolShow.js)图片旋转动画特效
- JS框架_(Progress.js)圆形动画进度条
- 【华为云技术分享】【一统江湖的大前端(8)】matter.js 经典物理
- js 设置cookie
- js倒计时天时分秒[转]
- 理解JS回调函数
- [Node.js] Mock an API for Local Development in React with Mirage JS
- [Functional Programming] Rewrite a reducer with functional state ADT
- [Node.js] mongoose schema
- [Node.js] Mock an API for Local Development in React with Mirage JS
- [Vue-rx] Watch Vue.js v-models as Observable with $watchAsObservable and RxJS
- [Node.js] Use "prestart" in scripts
- Node.js 的模块系统
- 如何使用微信JS-SDK实际分享功能
- 15 个常见的 Node.js 面试问题及答案
- Atitit 研发体系 codelib 代码库的建设 目录 1. 概念与组成2 1.1. Java代码2 1.2. Js代码2 1.3. H5 代码 js+css+htm+txt2 1.4.
- 华为OD机试 - 二叉树中序遍历(Java & JS & Python)
- 华为OD机试 - 匿名信(Java & JS & Python)
- Vue.js如何导入新组件
- js——截取url地址获取参数
- 最小的js编译器
- JS:使用Mock.js生成随机数据,拦截 Ajax 请求
- js-jquery-插件开发(二)【最终插件是最后一个,中间是过程】
- [Ext JS 4] 实战Chart 协调控制(单一的坐标,两个坐标)
- js产生随机数
- 【JS高级】js之闭包对象_04
- 【JS高级】js之函数、重载、匿名函数、作用域及作用域链_03
- 【JS高级】js之正则相关函数以及正则对象_02