Created
February 9, 2020 08:12
-
-
Save fduxiao/a66bfd0bf5bceb5e02a9aefa7b0bf49d to your computer and use it in GitHub Desktop.
js奇葩设计
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
class Number { | |
constructor(x) { | |
this.x = x | |
} | |
apply(f) { | |
return f(this.x) | |
} | |
} | |
class Add { | |
constructor(x) { | |
this.x = x | |
} | |
add(y) { | |
return this.x + y | |
} | |
addTwice(y) { | |
let n = new Number(y) | |
return y + n.apply(this.add) | |
} | |
addThreeTimes(y) { | |
let n = new Number(y) | |
return y + n.apply(this.addTwice) | |
} | |
} | |
add1 = new Add(1) | |
console.log(add1.add(2)) | |
console.log(add1.addTwice(2)) | |
console.log(add1.addThreeTimes(2)) |
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
class Number: | |
def __init__(self, x): | |
self.x = x | |
def apply(self, f): | |
return f(self.x) | |
class Add: | |
def __init__(self, x): | |
self.x = x | |
def add(self, y): | |
return self.x + y | |
def addTwice(self, y): | |
return y + Number(y).apply(self.add) | |
def addThreeTimes(self, y): | |
return y + Number(y).apply(self.addTwice) | |
add1 = Add(1) | |
print(add1.add(2)) | |
print(add1.addTwice(2)) | |
print(add1.addThreeTimes(2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment