Skip to content

Instantly share code, notes, and snippets.

View fResult's full-sized avatar

Sila Setthakan-anan fResult

View GitHub Profile
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]
@fResult
fResult / True definition of Higher-Order Function from the Math book.md
Last active January 14, 2024 16:19
True definition of Higher-Order Function from Mathematics for Working Programmers class, by lect. Dave Rawitat Pulam

Higher-Order Functions 😡😡😡

See something from an actual Maths book.

Definition

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.

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') {
@fResult
fResult / Compose.ts
Last active September 8, 2022 02:24
Implementing follows 'lodash/fp'
type AnyFuntion = (...args: any[]) => any
type Many<T> = T | ReadonlyArray<T>;
declare const _: _.MyFpImpl
declare namespace _ {
interface MyFpImpl {
compose: FlowRight
}
interface FlowRight {
/**
* @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]
/**
* @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 )
*
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
type ReturnOf<F> = F extends { (...args: any[]): infer RT } ? RT : never
const value: ReturnOf<String['valueOf']> = 'VALUE'.valueOf()
console.log(value)
@fResult
fResult / DataStoreType.ts
Last active June 7, 2022 18:47
Try type w/ Mike North
interface DataEntity {
id: string;
name: string;
}
interface Movie extends DataEntity {
author: string;
}
interface Song extends DataEntity {
{
"id": 1,
"subject": "รบกวน Deploy งานให้หน่อยครับ",
"description": "Deploy Staging 21.02.17.R1 ติดแท็กให้ด้วยนะ",
"user": {
"id" 11,
"email": "xyz@example.com",
"firstName": "John",
"lastName": "Doe"
}