[Javascript Crocks] Create a Maybe with a `safe` Utility Function
JavaScript with create Function safe Utility
2023-09-14 08:59:17 时间
In this lesson, we’ll create a safe
function that gives us a flexible way to create Maybe
s based on a value and a predicate function that we supply. We’ll verify its behavior with values that satisfy the predicate, and values that do not.
We can write more functional approach, for example write predicate functions:
const isNumber = n => typeof n === 'number' ? Maybe.Just(n) : Maybe.Nothing(); const isString = s => typeof s === 'string' ? Maybe.Just(s) : Maybe.Nothing();
High order function:
const safe = pred => val => pred(val); const safeNum = safe(isNumber); const safeStr = safe(isString);
Those functions are useful when we want use in large scale application, because those are composable.
Full code demo:
const {inc, upper} = require('./utils'); const Maybe = require('crocks/Maybe'); const isNumber = n => typeof n === 'number' ? Maybe.Just(n) : Maybe.Nothing(); const isString = s => typeof s === 'string' ? Maybe.Just(s) : Maybe.Nothing(); const safe = pred => val => pred(val); const safeNum = safe(isNumber); const safeStr = safe(isString); const inputN = safeNum(2); // Just 3 -> 3 const inputS = safeStr('test'); // Just TEST -> TEST const input = safeStr(undefined); // Nothing -> 0 const result = inputS .map(upper) .option(""); console.log(result);
Crocks lib also provides those functions, you actually don't need to write it by yourself.
https://evilsoft.github.io/crocks/docs/functions/predicate-functions.html
const {inc, upper} = require('./utils'); const Maybe = require('crocks/Maybe'); const safe = require('crocks/Maybe/safe'); const { isNumber, isString} = require('crocks/predicates'); /* const isNumber = n => typeof n === 'number' ? Maybe.Just(n) : Maybe.Nothing(); const isString = s => typeof s === 'string' ? Maybe.Just(s) : Maybe.Nothing(); const safe = pred => val => pred(val); */ const safeNum = safe(isNumber); const safeStr = safe(isString); const inputN = safeNum(2); // Just 3 -> 3 const inputS = safeStr('test'); // Just TEST -> TEST const input = safeStr(undefined); // Nothing -> 0 const result = inputS .map(upper) .option(""); console.log(result);
相关文章
- JavaScript经典代码【一】【javascript HTML控件获取值】
- [Javascript] Broadcaster + Operator + Listener pattern -- 22. mapError, wrap fetch with broadcaster with cancellation
- [Javascript] Wrap a Javascript Built-in constructor with Proxy
- [Javascript] Wait for the Fastest JavaScript Promise to Be Fulfilled with Promise.any()
- [Javascript] Replicate JavaScript Constructor Inheritance with Simple Objects (OLOO)
- [CSSinJS] Convert Sass (SCSS) Styled Button to CSSinJS with JavaScript Templates and Variables
- [Poi] Build and Analyze Your JavaScript Bundles with Poi
- [Javascript] Chunk array
- [Javascript] Safely Access a Property on a JavaScript Array with Optional Chaining
- [Javascript] Broadcaster + Operator + Listener pattern -- 22. mapError, wrap fetch with broadcaster with cancellation
- [Functional Programming] Create Reusable Functions with Partial Application in JavaScript
- [Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures
- [Algorithms] Solve Complex Problems in JavaScript with Dynamic Programming
- [Javascript] Classify text into categories with machine learning in Natural
- [Jest] Test JavaScript with Jest
- 在 JavaScript 中访问对象属性的 3 种方法