Created
November 21, 2013 20:59
-
-
Save rafaelss/7589524 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
// node promise.js | |
function Promise() { | |
this.callbacks = []; | |
this.resolved = false; | |
this.resolve = function(value) { | |
if(this.resolved) { | |
throw "Promise already resolved"; | |
} | |
else { | |
this.callbacks.forEach(function(cb) { | |
cb(value); | |
}); | |
this.resolved = true; | |
this.value = value; | |
} | |
}; | |
this.success = function(cb) { | |
if(this.resolved) { | |
cb(this.value); | |
} | |
else { | |
this.callbacks.push(cb); | |
} | |
}; | |
} | |
var foo = new Promise(); | |
var bar = new Promise(); | |
foo.resolve('hello'); | |
setTimeout(function(){ | |
bar.resolve('world'); | |
}, 500); | |
foo.success(function(result){ | |
console.log(result); | |
}); | |
bar.success(function(result){ | |
console.log(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
# ruby rotx.rb | |
def rotx(x, string, encrypt = true) | |
x = x - 26 while x > 26 | |
x = (26 - x).abs unless encrypt | |
string.chars.map do |chr| | |
x.times do | |
next unless chr =~ /[A-Za-z]/ | |
chr = chr.succ[0] | |
end | |
chr | |
end.join("") | |
end | |
raise "wrong!" unless rotx(10, "Hello, World") == "Rovvy, Gybvn" | |
raise "wrong!" unless rotx(10, "Rovvy, Gybvn", false) == "Hello, World" | |
raise "wrong!" unless rotx(13, "HELLO") == "URYYB" | |
raise "wrong!" unless rotx(13, "URYYB", false) == "HELLO" | |
raise "wrong!" unless rotx(36, "Hello, World") == "Rovvy, Gybvn" | |
raise "wrong!" unless rotx(36, "Rovvy, Gybvn", false) == "Hello, World" | |
raise "wrong!" unless rotx(100, "Everlane") == "Aranhwja" | |
raise "wrong!" unless rotx(100, "Aranhwja", false) == "Everlane" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment