-
-
Save robotlolita/74e84e23bdca25c95486 to your computer and use it in GitHub Desktop.
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
var Clonable = { | |
clone: function(v) { | |
var r = Object.create(this) | |
for (var k in v) r[k] = v[k] | |
return r | |
} | |
} | |
Number.prototype.sqrt = function(){ | |
return Math.sqrt(this) | |
} | |
Number.prototype.round = function(){ | |
return Math.round(this) | |
} | |
Number.prototype.eq = function(a) { | |
return this.valueOf() === a.valueOf()? True : False | |
} | |
var Range = Clonable.clone({ | |
make(start, end) { | |
return this.clone({ start: () => start, end: () => end }) | |
}, | |
start: () => 0, | |
end: () => 0, | |
map(f) { | |
return Array.apply(null, Array((this.end() - this.start()) + 1)).map((_, i) => i + this.start()) | |
} | |
}) | |
var True = Clonable.clone({ | |
thenElse(f, g){ | |
return f() | |
} | |
}) | |
var False = Clonable.clone({ | |
thenElse(f, g){ | |
return g() | |
} | |
}) | |
var Door = Clonable.clone({ | |
make(index){ | |
this.clone({ index: () => index }) | |
}, | |
isOpen(){ | |
return False | |
}, | |
isClosed(){ | |
return False | |
} | |
}) | |
var OpenDoor = Door.clone({ | |
toString(){ | |
return "Door " + this.index().toString() + " is open." | |
}, | |
toEmoji(){ | |
return "_" | |
}, | |
isOpen(){ | |
return True | |
} | |
}) | |
var ClosedDoor = Door.clone({ | |
toString(){ | |
return "Door " + this.index().toString() + " is closed." | |
}, | |
toEmoji(){ | |
return "🚪" | |
}, | |
isClosed(){ | |
return True | |
} | |
}) | |
var doors = Clonable.clone({ | |
state(){ | |
Range(1, 100).map(i => | |
(i.sqrt().eq(i.sqrt().round())).thenElse(() => OpenDoor.make(i), | |
() => ClosedDoor.make(i))) | |
}, | |
open(){ | |
return this.state().filter(x => x.isOpen()).map(x => x.index()) | |
}, | |
closed(){ | |
return this.state().filter(x => x.isClosed()).map(x => x.index()) | |
} | |
}) | |
console.log("What state are the doors in after the last pass?") | |
console.log(doors.state().map(x => x.toEmoji()).join("")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment