Last active
June 16, 2020 02:00
-
-
Save idhowardgj94/92601651c6063ae6842f1b1c79c6fbea to your computer and use it in GitHub Desktop.
prac
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
// Import stylesheets | |
import './style.css'; | |
// Write TypeScript code! | |
const appDiv: HTMLElement = document.getElementById('app'); | |
appDiv.innerHTML = `<h1>TypeScript Starter</h1>`; | |
// const id = x => x | |
// const a = 1 | |
// create a Box which turns this | |
// const cps1 = (x, cb) => cb(x + 1) | |
// const cps2 = (x, cb) => cb(x * 2) | |
// const cps3 = (x, cb) => cb(x * x) | |
// cps1(a, b => ( | |
// cps2(b, c => ( | |
// cps3(c, id) | |
// )) | |
// )) | |
// // to this | |
// const f1 = fromCPS(cps1) | |
// const f2 = fromCPS(cps2) | |
// const f3 = fromCPS(cps3) | |
// f1(a) | |
// .bind(f2) | |
// .bind(f3) | |
// .toCPS(id) | |
type CPS = (x: number, cb: (x) => void) => number | |
const id = x => x | |
const a = 1 | |
const cps1 = (x, cb) => cb(x + 1) | |
const cps2 = (x, cb) => cb(x * 2) | |
const cps3 = (x, cb) => cb(x * x) | |
// toCPS: function (id) { | |
// return obj(a, id) | |
// }, | |
// bind: function (f) { | |
// return f(this.toCPS(id)) | |
// } | |
const fromCPS = (cps: CPS, next = undefined) => (a?:number) => { | |
const obj: CPS = cps; | |
return { | |
next: next, | |
toCPS: function (id: any): number { | |
// take previous return value | |
if(this.a !== undefined) { | |
a = this.a | |
} | |
// invoke next op | |
if(this.next !== undefined) { | |
const self = this | |
// store last id (callback function) | |
this.next.store(id) | |
// bind this to solve this undefined problem | |
return obj(a, self.next.take.bind(this.next)) | |
} else { | |
return obj(a, id) | |
} | |
}, | |
store: function(id: any) { | |
this.last = id | |
}, | |
take: function(x: number) { | |
this.a = x; | |
return this.toCPS(this.last) | |
}, | |
bind: function (f: (a?: number) => any): any { | |
if(this.next === undefined) { | |
this.next = f() | |
} else { | |
this.next.bind(f) | |
} | |
return this | |
} | |
} | |
} | |
const f1 = fromCPS(cps1) | |
const f2 = fromCPS(cps2) | |
const f3 = fromCPS(cps3) | |
const b = f1(a) | |
b.bind(f2).toCPS(console.log) | |
b.bind(f3).toCPS(console.log) | |
let result = f1(a) | |
.bind(f2) | |
.bind(f3) | |
.toCPS(id) | |
console.log("result" , result) |
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
// Import stylesheets | |
import './style.css'; | |
// Write TypeScript code! | |
const appDiv: HTMLElement = document.getElementById('app'); | |
appDiv.innerHTML = `<h1>TypeScript Starter</h1>`; | |
// const id = x => x | |
// const a = 1 | |
// create a Box which turns this | |
// const cps1 = (x, cb) => cb(x + 1) | |
// const cps2 = (x, cb) => cb(x * 2) | |
// const cps3 = (x, cb) => cb(x * x) | |
// cps1(a, b => ( | |
// cps2(b, c => ( | |
// cps3(c, id) | |
// )) | |
// )) | |
// // to this | |
// const f1 = fromCPS(cps1) | |
// const f2 = fromCPS(cps2) | |
// const f3 = fromCPS(cps3) | |
// f1(a) | |
// .bind(f2) | |
// .bind(f3) | |
// .toCPS(id) | |
type CPS = (x: number, cb: (x) => void) => number | |
const id = x => x | |
const a = 1 | |
const cps1 = (x, cb) => cb(x + 1) | |
const cps2 = (x, cb) => cb(x * 2) | |
const cps3 = (x, cb) => cb(x * x) | |
// toCPS: function (id) { | |
// return obj(a, id) | |
// }, | |
// bind: function (f) { | |
// return f(this.toCPS(id)) | |
// } | |
const fromCPS = (cps: CPS, next = undefined) => (a?:number) => { | |
const obj: CPS = cps; | |
const fList = []; | |
return { | |
next: next, | |
toCPS: function (id: any): number { | |
// take previous return value | |
if(this.a !== undefined) { | |
a = this.a | |
} | |
// invoke next op | |
if(this.next !== undefined) { | |
const self = this | |
// store last id (callback function) | |
this.next.store(id) | |
// bind this to solve this undefined problem | |
return obj(a, self.next.take.bind(this.next)) | |
} else { | |
return obj(a, id) | |
} | |
}, | |
store: function(id: any) { | |
this.last = id | |
}, | |
take: function(x: number) { | |
this.a = x; | |
return this.toCPS(this.last) | |
}, | |
bind: function (f: (a?: number) => any): any { | |
if(this.next === undefined) { | |
this.next = f() | |
} else { | |
this.next.bind(f) | |
} | |
return this | |
} | |
} | |
} | |
const f1 = fromCPS(cps1) | |
const f2 = fromCPS(cps2) | |
const f3 = fromCPS(cps3) | |
const b = f1(a) | |
b.bind(f2).toCPS(console.log) | |
b.bind(f3).toCPS(console.log) | |
let result = f1(a) | |
.bind(f2) | |
.bind(f3) | |
.toCPS(id) | |
console.log("result" , result) |
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
// Import stylesheets | |
import './style.css'; | |
// Write TypeScript code! | |
const appDiv: HTMLElement = document.getElementById('app'); | |
appDiv.innerHTML = `<h1>TypeScript Starter</h1>`; | |
// const id = x => x | |
// const a = 1 | |
// create a Box which turns this | |
// const cps1 = (x, cb) => cb(x + 1) | |
// const cps2 = (x, cb) => cb(x * 2) | |
// const cps3 = (x, cb) => cb(x * x) | |
// cps1(a, b => ( | |
// cps2(b, c => ( | |
// cps3(c, id) | |
// )) | |
// )) | |
// // to this | |
// const f1 = fromCPS(cps1) | |
// const f2 = fromCPS(cps2) | |
// const f3 = fromCPS(cps3) | |
// f1(a) | |
// .bind(f2) | |
// .bind(f3) | |
// .toCPS(id) | |
type CPS = (x: number, cb: (x) => void) => number | |
const id = x => x | |
const a = 1 | |
const cps1 = (x, cb) => cb(x + 1) | |
const cps2 = (x, cb) => cb(x * 2) | |
const cps3 = (x, cb) => cb(x * x) | |
// toCPS: function (id) { | |
// return obj(a, id) | |
// }, | |
// bind: function (f) { | |
// return f(this.toCPS(id)) | |
// } | |
const fromCPS = (cps: CPS, next = undefined) => (a?:number) => { | |
const obj: CPS = cps; | |
const fList = []; | |
return { | |
next: next, | |
toCPS: function (id): number { | |
if(this.a !== undefined) { | |
console.log(this.a) | |
a = this.a | |
} | |
if(this.next !== undefined) { | |
console.log('inisde if', obj) | |
return this.next.take(obj(a)).toCPS(id) | |
} else { | |
return obj(a, id) | |
} | |
}, | |
take: function(x: number) { | |
this.a = x | |
return this | |
}, | |
bind: function (f: (a?: number) => any): any { | |
if(this.next === undefined) { | |
this.next = f() | |
} else { | |
this.next.bind(f) | |
} | |
return this | |
} | |
} | |
} | |
const f1 = fromCPS(cps1) | |
const f2 = fromCPS(cps2) | |
const f3 = fromCPS(cps3) | |
const b = f1(a) | |
b.bind(f2).toCPS(console.log) | |
// b.bind(f3).toCPS(console.log) | |
// let result = f1(a) | |
// .bind(f2) | |
// .bind(f3) | |
// .toCPS(id) | |
// console.log("result" , result) |
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
// Import stylesheets | |
import './style.css'; | |
// Write TypeScript code! | |
const appDiv: HTMLElement = document.getElementById('app'); | |
appDiv.innerHTML = `<h1>TypeScript Starter</h1>`; | |
// const id = x => x | |
// const a = 1 | |
// create a Box which turns this | |
// const cps1 = (x, cb) => cb(x + 1) | |
// const cps2 = (x, cb) => cb(x * 2) | |
// const cps3 = (x, cb) => cb(x * x) | |
// cps1(a, b => ( | |
// cps2(b, c => ( | |
// cps3(c, id) | |
// )) | |
// )) | |
// // to this | |
// const f1 = fromCPS(cps1) | |
// const f2 = fromCPS(cps2) | |
// const f3 = fromCPS(cps3) | |
// f1(a) | |
// .bind(f2) | |
// .bind(f3) | |
// .toCPS(id) | |
const id = x => x | |
const a = 1 | |
const cps1 = (x, cb) => cb(x + 1) | |
const cps2 = (x, cb) => cb(x * 2) | |
const cps3 = (x, cb) => cb(x * x) | |
const fromCPS = (cps) => a => { | |
const obj = cps; | |
const f = [] | |
return { | |
toCPS: function (id) { | |
return obj(a, id) | |
}, | |
bind: function (f) { | |
return f(this.toCPS(id)) | |
} | |
} | |
} | |
const f1 = fromCPS(cps1) | |
const f2 = fromCPS(cps2) | |
const f3 = fromCPS(cps3) | |
let result = f1(a) | |
.bind(f2) | |
.bind(f3) | |
.toCPS(id) | |
console.log("result" , result) |
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
// Import stylesheets | |
import './style.css'; | |
// Write TypeScript code! | |
const appDiv: HTMLElement = document.getElementById('app'); | |
appDiv.innerHTML = `<h1>TypeScript Starter</h1>`; | |
// const id = x => x | |
// const a = 1 | |
// create a Box which turns this | |
// const cps1 = (x, cb) => cb(x + 1) | |
// const cps2 = (x, cb) => cb(x * 2) | |
// const cps3 = (x, cb) => cb(x * x) | |
// cps1(a, b => ( | |
// cps2(b, c => ( | |
// cps3(c, id) | |
// )) | |
// )) | |
// // to this | |
// const f1 = fromCPS(cps1) | |
// const f2 = fromCPS(cps2) | |
// const f3 = fromCPS(cps3) | |
// f1(a) | |
// .bind(f2) | |
// .bind(f3) | |
// .toCPS(id) | |
// cps1(a, b => ( | |
// cps2(b, c => ( | |
// cps3(c, id) | |
// )) | |
// )) | |
// 可以拆成 | |
// handler2 = c => cps3(c, id); | |
// handler1 = b => cps2(b, handler2); | |
// cps1(a, handler1) | |
// 但是builder的順序是 | |
// cps1(a, handler1) | |
// handler1 = b => cps2(b, handler2); | |
// handler2 = c => cps3(c, id); | |
// 執行順序與建立順序反過來就很反直覺 | |
// 問題在於要如何遞補缺少的handler參數 | |
type CB<T> = { | |
(a: T, cb: (any) => any): void; | |
} | |
type CBO<T> = { | |
obj: CB<T>; | |
prev: CBO<T> | undefined; | |
next?: CBO<T>; | |
toCPS(id: CB<T>): any; | |
bind<B extends any>(f: (a?: T) => CBO<T>): CBO<T>; | |
} | |
const id = x => x | |
const a:number = 1 | |
const cps1: CB<number> = (x: number, cb: (any) => any) => cb(x + 1) | |
const cps2: CB<number> = (x: number, cb: (any) => any) => cb(x * 2) | |
const cps3: CB<number> = (x: number, cb: (any) => any) => cb(x * x) | |
const fromCPS = <T, >(cps: CB<T>, prev?: CBO<T>) => (a?:T): CBO<T> => { | |
return { | |
obj: cps, | |
prev, | |
next: undefined, | |
toCPS: function (id: CB<T>): any { | |
return this.prev != undefined ? this.prev.toCPS(x => this.obj(x, id)) : this.obj(a, id) | |
}, | |
bind: function <B>(f: (a: T) => CBO<B>): CBO<B> { | |
// m a -> (a -> m b) -> m b, | |
// bind: <B>(a => CPS<B>) => CPS<B> | |
return fromCPS<B>(f().obj, this)() | |
return fromCPS<B>((x: B, cb: (any) => any) => cb(x))() | |
} | |
} | |
} | |
const f1 = fromCPS<number>(cps1) | |
const f2 = fromCPS<number>(cps2) | |
const f3 = fromCPS<number>(cps3) | |
let result = f1(a) | |
.bind<number>(f2) | |
.bind<number>(f3) | |
.toCPS(id) | |
console.log("result" , result) |
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
// Import stylesheets | |
import './style.css'; | |
// Write TypeScript code! | |
const appDiv: HTMLElement = document.getElementById('app'); | |
appDiv.innerHTML = `<h1>TypeScript Starter</h1>`; | |
// const id = x => x | |
// const a = 1 | |
// create a Box which turns this | |
// const cps1 = (x, cb) => cb(x + 1) | |
// const cps2 = (x, cb) => cb(x * 2) | |
// const cps3 = (x, cb) => cb(x * x) | |
// cps1(a, b => ( | |
// cps2(b, c => ( | |
// cps3(c, id) | |
// )) | |
// )) | |
// // to this | |
// const f1 = fromCPS(cps1) | |
// const f2 = fromCPS(cps2) | |
// const f3 = fromCPS(cps3) | |
// f1(a) | |
// .bind(f2) | |
// .bind(f3) | |
// .toCPS(id) | |
const id = x => x | |
const a = 1 | |
const cps1 = (x, cb) => cb(x + 1) | |
const cps2 = (x, cb) => cb(x * 2) | |
const cps3 = (x, cb) => cb(x * x) | |
const fromCPS = (cps) => a => { | |
const obj = cps; | |
const fList = []; | |
return { | |
toCPS: function (id, count = 0) { | |
if(fList.length > 0 && count < fList.length) { | |
return fList[count](obj(a, id)).concat(fList).toCPS(id, count+1) | |
} else { | |
return obj(a, id) | |
} | |
return obj(a, id) | |
}, | |
bind: function (f) { | |
// f(this.toCPS(id)) | |
fList.push(f); | |
return {...this} | |
}, | |
concat: function (fs) { | |
fList.push(...fs) | |
return {...this} | |
} | |
} | |
} | |
const f1 = fromCPS(cps1) | |
const f2 = fromCPS(cps2) | |
const f3 = fromCPS(cps3) | |
let result = f1(a) | |
.bind(f2) | |
.bind(f3) | |
.toCPS(id) | |
console.log("result" , result) |
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
// Import stylesheets | |
import './style.css'; | |
// Write TypeScript code! | |
const appDiv: HTMLElement = document.getElementById('app'); | |
appDiv.innerHTML = `<h1>TypeScript Starter</h1>`; | |
// const id = x => x | |
// const a = 1 | |
// create a Box which turns this | |
// const cps1 = (x, cb) => cb(x + 1) | |
// const cps2 = (x, cb) => cb(x * 2) | |
// const cps3 = (x, cb) => cb(x * x) | |
// cps1(a, b => ( | |
// cps2(b, c => ( | |
// cps3(c, id) | |
// )) | |
// )) | |
// // to this | |
// const f1 = fromCPS(cps1) | |
// const f2 = fromCPS(cps2) | |
// const f3 = fromCPS(cps3) | |
// f1(a) | |
// .bind(f2) | |
// .bind(f3) | |
// .toCPS(id) | |
const id = x => x | |
const a = 1 | |
const cps1 = (x, cb) => cb(x + 1) | |
const cps2 = (x, cb) => cb(x * 2) | |
const cps3 = (x, cb) => cb(x * x) | |
const fromCPS = (cps, ...binds) => a => { | |
const obj = cps; | |
const cpsList = binds; | |
return { | |
toCPS: (id, count = undefined) => { | |
if(count === undefined && cpsList === undefined) { | |
return obj(a, id) | |
} | |
}, | |
bind: (f) => { | |
return fromCPS(cps, [...cpsList, f]) | |
} | |
} | |
} | |
const f1 = fromCPS(cps1) | |
const f2 = fromCPS(cps2) | |
const f3 = fromCPS(cps3) | |
let result = f1(a) | |
// .bind(f2) | |
.toCPS(id) | |
console.log("result" , result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackblitz.com/edit/typescript-t3atwt?file=index.ts