Live Note

Remain optimistic

Reducer

Reducer 將多個 input fold 成一個 output.

1
2
3
4
const add = (a, b) => a + b
const multiply = (a, b) => a * b
const concatString = (a, b) => a + b
const concatArray = (a, b) => [...a, ...b]

Transducer

Transducer 做的事情大致相同,但是與普通的 reducer 不同的是,它可以通過多個 function 組合而成.而普通的 reducer 不能組合,因爲他們接受兩個參數,但是只返回一個值,所以不能將這次的結果傳入下一個 reducer:

1
2
3
4
5
6
7
// reducer
f: (a, c) => a
g: (a, c) => a

// transducer
f: (reducer) => reducer
g: (reducer) => reducer
Read more »

川普不对伊朗动武 美股涨 金价回落 油价跳水

伊朗袭击美军驻伊拉克基地后,美国总统川普表示将升级经济制裁取代付诸武力。市场乐观情绪上扬,美股上涨,金价回落,油价则大跳水。

川普 1 月 8 日在白宫发表谈话,伊朗袭击并未伤害到任何美国人,称伊朗有所退让,暗示不会对伊朗动武,伊朗外交部长也称该国不寻求升级事态,这些消息都缓解了投资人对美伊局势的不安。

8 日美国股市上涨,道琼工业指数收涨 161.41 点或 0.56%,报 28745.09 点。标普 500 指数收涨 15.87 点或 0.49%,报 3253.05 点。那斯达克指数收涨 60.66 点或 0.67%,报 9129.24 点。费城半导体指数上涨 0.31 点或 0.02%,报 1867.59 点。

那斯达克指数创收盘新高,道琼工业指数、标普 500 指数、费城半导体指数徘回历史次高位。

Read more »

Classes

1
2
3
4
5
6
7
8
9
10
11
class Greeter {
greeting: string

constructor(message: string) {
this.greeting = message
}

greet(): string {
return "Hello, " + this.greeting
}
}

Private

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Animal {
constructor(private name: string) {}
}

class Horse extends Animal {
constructor() {
super("Horse")
}
}

class Human {
constructor(private name: string) {}
}

let animal = new Animal("Lucy")
let horse = new Horse()
let human = new Human("Jack")
animal = horse // success
animal = human // failed
Read more »

Numeric enums

1
2
3
4
5
6
7
8
9
10
11
12
enum Direction {
UP,
DOWN,
LEFT,
RIGHT,
}

function move(length: number, direction: Direction): void {
console.log(`move: ${direction} ${length}meters.`)
}

move(10, Direction.UP)

String enums

1
2
3
4
5
6
enum Direction {
UP = "UP",
DOWN = "DOWN",
LEFT = "LEFT",
RIGHT = "RIGHT",
}
Read more »

Function Types

Typing the function

1
2
3
4
5
6
7
function foo(x: number, y: string): string {
return x + y
}

let fo = function (x: number, y: string): string {
return x + y
}

Writing the function type

1
2
3
4
5
6
let foo: (x: number, y: string) => string = function (
x: number,
y: string
): string {
return x + y
}

Inferring types

1
2
3
let foo: (x: number, y: string) => string = function (x, y) {
return x + y
}
Read more »