mirror of
https://github.com/Fechin/reference.git
synced 2026-11-20 21:23:11 +03:00
Release the latest generator
This commit is contained in:
1 parent
b520119d38
commit
15c93e54ee
484 files changed
+4969
-856
No files matched your search
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: How to box handler in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to box handler only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const boxHandler = x => ({ next: f => boxHandler(f(x)), done: f => f(x) });
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
const getPercentNumber = str => boxHandler(str).next(str => str.replace(/\%/, '')).next(str => parseFloat(str)).done(res => res * 0.01);
|
||||
|
||||
getPercentNumber('50%'); // 0.5
|
||||
|
||||
// Example 2
|
||||
const getMoney = (price) => Number.parseFloat(price.replace(/\$/, ''));
|
||||
const getPercent = (percent) => Number.parseFloat(percent.replace(/\%/)) * 0.01;
|
||||
|
||||
const getDiscountPrice = (price, discount) => boxHandler(getMoney(price)).done(cents => boxHandler(getPercent(discount)).next(save => cents - (cents * save))).done(res => res);
|
||||
|
||||
getDiscountPrice('$6.00', '20%'); // 4.8
|
||||
```
|
||||
@@ -0,0 +1,30 @@
|
||||
---
|
||||
title: How to check if a value is a function in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to check if a value is a function only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const isFunction = v => ['[object Function]', '[object GeneratorFunction]', '[object AsyncFunction]', '[object Promise]'].includes(Object.prototype.toString.call(v));
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
isFunction(function() {}); // true
|
||||
isFunction(function*() {}); // true
|
||||
isFunction(async function() {}); // true
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to check if a value is a generator function in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to check if a value is a generator function only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const isGeneratorFunction = v => Object.prototype.toString.call(v) === '[object GeneratorFunction]';
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
isGeneratorFunction(function() {}); // false
|
||||
isGeneratorFunction(function*() {}); // true
|
||||
```
|
||||
@@ -0,0 +1,30 @@
|
||||
---
|
||||
title: How to check if a value is an async function in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to check if a value is an async function only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const isAsyncFunction = v => Object.prototype.toString.call(v) === '[object AsyncFunction]';
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
isAsyncFunction(function() {}); // false
|
||||
isAsyncFunction(function*() {}); // false
|
||||
isAsyncFunction(async function() {}); // true
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
title: How to compose functions from left to right in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to compose functions from left to right only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
// Compose functions from left to right
|
||||
const pipe = (...fns) => x => fns.reduce((y, f) => f(y), x);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
const lowercase = str => str.toLowerCase();
|
||||
const capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
|
||||
const reverse = str => str.split('').reverse().join('');
|
||||
|
||||
const fn = pipe(lowercase, capitalize, reverse);
|
||||
|
||||
// We will execute `lowercase`, `capitalize` and `reverse` in order
|
||||
fn('Hello World') === 'dlrow olleH';
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
title: How to compose functions in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to compose functions only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
// Compose functions from right to left
|
||||
const compose = (...fns) => x => fns.reduceRight((y, f) => f(y), x);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
const lowercase = str => str.toLowerCase();
|
||||
const capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
|
||||
const reverse = str => str.split('').reverse().join('');
|
||||
|
||||
const fn = compose(reverse, capitalize, lowercase);
|
||||
|
||||
// We will execute `lowercase`, `capitalize` and `reverse` in order
|
||||
fn('Hello World') === 'dlrow olleH';
|
||||
```
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
---
|
||||
title: How to create a function that accepts a single argument in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to create a function that accepts a single argument only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const unary = fn => arg => fn(arg);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
['1', '2', '3', '4', '5'].map(unary(parseInt)); // [1, 2, 3, 4, 5]
|
||||
```
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: How to create an empty function in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to create an empty function only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const noop = () => {};
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const noop = Function();
|
||||
// calling Function() might be detected as using eval by some security tools
|
||||
```
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
title: How to curry a function in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to curry a function only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const curry = (fn, ...args) => fn.length <= args.length ? fn(...args) : curry.bind(null, fn, ...args);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
const sum = (a, b, c) => a + b + c;
|
||||
curry(sum)(1)(2)(3); // 6
|
||||
curry(sum)(1, 2, 3); // 6
|
||||
curry(sum, 1)(2, 3); // 6
|
||||
curry(sum, 1)(2)(3); // 6
|
||||
curry(sum, 1, 2)(3); // 6
|
||||
curry(sum, 1, 2, 3); // 6
|
||||
```
|
||||
@@ -0,0 +1,33 @@
|
||||
---
|
||||
title: How to delay the evaluation of a function in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to delay the evaluation of a function only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
// returns a new version of `fn` that returns values as lazy evaluable
|
||||
const thunkfy = fn => (...args) => () => fn(...args);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
const heavyComputation = x => doStuff(x);
|
||||
const unnecessarySlow = manyThings.map(heavyComputation)
|
||||
.find(result => result.criteria);
|
||||
const probablyFaster = manyThings.map(thunkfy(heavyComputation))
|
||||
.find(thunk => thunk().criteria);
|
||||
```
|
||||
@@ -0,0 +1,32 @@
|
||||
---
|
||||
title: How to execute a function once in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to execute a function once only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const once = fn => ((ran = false) => () => ran ? fn : (ran = !ran, fn = fn()))();
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
let n = 0;
|
||||
const incOnce = once(() => ++n);
|
||||
incOnce(); // n = 1
|
||||
incOnce(); // n = 1
|
||||
incOnce(); // n = 1
|
||||
```
|
||||
@@ -0,0 +1,39 @@
|
||||
---
|
||||
title: How to flip the arguments of a function in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to flip the arguments of a function only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
// Reverse the order of arguments
|
||||
const flip = fn => (...args) => fn(...args.reverse());
|
||||
|
||||
// For binary functions
|
||||
const flip = fn => (b, a) => fn(a, b);
|
||||
```
|
||||
|
||||
### Or for curried functions
|
||||
|
||||
```js {.wrap}
|
||||
const flip = fn => b => a => fn(a)(b);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
const isParent = (parent, child) => parent.children.includes(child);
|
||||
const isChild = flip(isParent);
|
||||
```
|
||||
@@ -0,0 +1,22 @@
|
||||
---
|
||||
title: How to identity function in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to identity function only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const identity = x => x;
|
||||
```
|
||||
@@ -0,0 +1,45 @@
|
||||
---
|
||||
title: How to logical xor operator in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to logical xor operator only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
// returns `true` if one of the arguments is truthy and the other is falsy
|
||||
|
||||
const xor = (a, b) => (a && !b) || (!a && b);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const xor = (a, b) => !(!a && !b) && !(a && b);
|
||||
```
|
||||
|
||||
### Or
|
||||
|
||||
```js {.wrap}
|
||||
const xor = (a, b) => Boolean(!a ^ !b);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
xor(true, true); // false
|
||||
xor(false, false); // false
|
||||
xor(true, false); // true
|
||||
xor(false, true); // true
|
||||
```
|
||||
@@ -0,0 +1,36 @@
|
||||
---
|
||||
title: How to memoize a function in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to memoize a function only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const memoize = fn => ((cache = {}) => arg => cache[arg] || (cache[arg] = fn(arg)))();
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
// Calculate Fibonacci numbers
|
||||
const fibo = memoize(n => n <= 2 ? 1 : fibo(n - 1) + fibo(n - 2));
|
||||
|
||||
fibo(1); // 1
|
||||
fibo(2); // 1
|
||||
fibo(3); // 2
|
||||
fibo(4); // 3
|
||||
fibo(5); // 5
|
||||
fibo(6); // 8
|
||||
```
|
||||
@@ -0,0 +1,30 @@
|
||||
---
|
||||
title: How to partially apply a function in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to partially apply a function only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const partial = (fn, ...a) => (...b) => fn(...a, ...b);
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
const sum = (x, y) => x + y;
|
||||
const inc = partial(sum, 1);
|
||||
inc(9); // 10
|
||||
```
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
title: How to uncurry a function in JavaScript
|
||||
tags:
|
||||
- JavaScript
|
||||
- ES6
|
||||
- one-line
|
||||
- js
|
||||
- Function
|
||||
layout: note
|
||||
---
|
||||
|
||||
|
||||
|
||||
In this Article we will go through how to uncurry a function only using single line of code in JavaScript.
|
||||
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
|
||||
<br/>
|
||||
<br/>
|
||||
Let's define this short function:
|
||||
|
||||
```js {.wrap}
|
||||
const uncurry = (fn, n = 1) => (...args) => (acc => args => args.reduce((x, y) => x(y), acc))(fn)(args.slice(0, n));
|
||||
```
|
||||
Parameters `fn` is a curried function.
|
||||
|
||||
|
||||
Parameters `n` is the depth of parameters.
|
||||
|
||||
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
```js {.wrap}
|
||||
const sum = a => b => c => a + b + c;
|
||||
uncurry(sum, 1)(1)(2)(3); // 6
|
||||
uncurry(sum, 2)(1, 2)(3); // 6
|
||||
uncurry(sum, 3)(1, 2, 3); // 6
|
||||
```
|
||||
Reference in new issue
Block a user