Skip to content

Instantly share code, notes, and snippets.

@fduxiao
Created February 9, 2020 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fduxiao/a66bfd0bf5bceb5e02a9aefa7b0bf49d to your computer and use it in GitHub Desktop.
Save fduxiao/a66bfd0bf5bceb5e02a9aefa7b0bf49d to your computer and use it in GitHub Desktop.
js奇葩设计
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))
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