A function is called a higher-order function if its arguments and values are allowed to be functions.
This is an important property that most good programming languages possess.
The composition and tupling operations are examples of functions that take other functions as arguments and return functions as results.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createFibWithMemoization() { | |
const cached: { [num: number]: number } = {} | |
return function fib(n: number): number { | |
if (n <= 1) return n | |
!cached[n-1] && (cached[n-1] = fib(n - 1)) | |
!cached[n-2] && (cached[n-2] = fib(n - 2)) | |
return cached[n-2] + cached[n-1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Numeral = string | number | |
function addInt(x: Numeral, y: Numeral): Numeral { | |
const xx = Number(x) | |
const yy = Number(y) | |
const result = xx + yy | |
if (typeof x === 'string') { | |
return `${result}` | |
} | |
if (typeof x === 'number') { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type AnyFuntion = (...args: any[]) => any | |
type Many<T> = T | ReadonlyArray<T>; | |
declare const _: _.MyFpImpl | |
declare namespace _ { | |
interface MyFpImpl { | |
compose: FlowRight | |
} | |
interface FlowRight { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param fn - Any function that need to convert to *unary* function | |
* @returns *unary* function (one parameter function) | |
* @description `unary` is use to make function, then it can support point-free which 1 parameter | |
* | |
* @example | |
* ```typescript | |
* // ! DON'T | |
* console.log(['1', '2', '3'].map( parseInt )) // [1, NaN, NaN] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @description `not` is a function which input any function return boolean and negated its output | |
* @example | |
* ```typescript | |
* function isOdd (x: number) { | |
* // the possible value of `x % 2` is `0` or `1` | |
* return x % 2 === 1 | |
* } | |
* const isEven = not( isOdd ) | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Expect<T extends true> = T | |
type Equal<X, Y> = | |
(<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) | |
? true | |
: false | |
type NotEqual<X, Y> = true extends Equal<X, Y> | |
? false | |
: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ReturnOf<F> = F extends { (...args: any[]): infer RT } ? RT : never | |
const value: ReturnOf<String['valueOf']> = 'VALUE'.valueOf() | |
console.log(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface DataEntity { | |
id: string; | |
name: string; | |
} | |
interface Movie extends DataEntity { | |
author: string; | |
} | |
interface Song extends DataEntity { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"id": 1, | |
"subject": "รบกวน Deploy งานให้หน่อยครับ", | |
"description": "Deploy Staging 21.02.17.R1 ติดแท็กให้ด้วยนะ", | |
"user": { | |
"id" 11, | |
"email": "xyz@example.com", | |
"firstName": "John", | |
"lastName": "Doe" | |
} |