banner
biuaxia

biuaxia

"万物皆有裂痕,那是光进来的地方。"
github
bilibili
tg_channel

Mastering TypeScript

title: Mastering TypeScript
date: 2022-02-17 16:02:14
toc: true
category:

  • typescript
    tags:
  • Mastering
  • typescript

First-Class Citizens and Higher-Order Functions#

First-Class Citizens: Variable types, values (literals), object fields, function parameters, and function returns can all be functions.

Higher-Order Functions: Functions that return functions and have functions as parameters. Each layer of wrapping counts as one order.

See code:

let a = [1, 2, 3, 4, 11, 22, 33, 44, 111, 222, 333, 444]
a.sort()
console.log(a)

const comp = createCompare({
    smallerFirst: true
})

function createCompare(p: { smallerFirst: boolean }) {
    if (p.smallerFirst) {
        return (a: number, b: number) => a - b
    } else {
        return (a: number, b: number) => b - a
    }
}

function logCompare(comp: (a: number, b: number) => number) {
    return (a: number, b: number) => {
        console.log(`compareing ${a} ${b}`)
        return comp(a, b)
    }
}

a.sort(logCompare(comp))
console.log(a)

Output:

[LOG]: [1, 11, 111, 2, 22, 222, 3, 33, 333, 4, 44, 444] 
[LOG]: "compareing 11 1" 
[LOG]: "compareing 111 11" 
[LOG]: "compareing 2 111" 
[LOG]: "compareing 2 11" 
[LOG]: "compareing 2 1" 
[LOG]: "compareing 22 11" 
[LOG]: "compareing 22 111" 
[LOG]: "compareing 222 11" 
[LOG]: "compareing 222 111" 
[LOG]: "compareing 3 22" 
[LOG]: "compareing 3 2" 
[LOG]: "compareing 3 11" 
[LOG]: "compareing 33 11" 
[LOG]: "compareing 33 111" 
[LOG]: "compareing 33 22" 
[LOG]: "compareing 333 22" 
[LOG]: "compareing 333 111" 
[LOG]: "compareing 333 222" 
[LOG]: "compareing 4 22" 
[LOG]: "compareing 4 3" 
[LOG]: "compareing 4 11" 
[LOG]: "compareing 44 22" 
[LOG]: "compareing 44 222" 
[LOG]: "compareing 44 111" 
[LOG]: "compareing 44 33" 
[LOG]: "compareing 444 22" 
[LOG]: "compareing 444 111" 
[LOG]: "compareing 444 333" 
[LOG]: [1, 2, 3, 4, 11, 22, 33, 44, 111, 222, 333, 444]

The types of variables, functions, and methods are as follows:

let a: number[]

const comp: (a: number, b: number) => number

function createCompare(p: { smallerFirst: boolean }): (a: number, b: number) => number

function logCompare(comp: (a: number, b: number) => number): (a: number, b: number) => number

(method) Array<number>.sort(compareFn?: ((a: number, b: number) => number) | undefined): number[]

Closures#

See code:

const a = [1, 2, 3, 4, 11, 22, 33, 44, 111, 222, 333, 444, 5, 55, 555, 6, 66, 666]

// config
const GOOD_FACTOR = 2
// config end

function isGoodNum(goodFactor: number, v: number) {
    return v % goodFactor == 0
}

function filterArray(a: number[], f: (v: number) => boolean) {
    return a.filter(f)
}

function partiallyApply(f: (a: number, b: number) => boolean, a: number) {
    return (b: number) => f(a, b)
}

console.log(filterArray(a, (v) => isGoodNum(GOOD_FACTOR, v)))
console.log(filterArray(a, partiallyApply(isGoodNum, 3)))

Output:

[LOG]: [2, 4, 22, 44, 222, 444, 6, 66, 666] 
[LOG]: [3, 33, 111, 222, 333, 444, 555, 6, 66, 666] 
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.