This file contains hidden or 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
function compare(choice1, choice2) { | |
if (choice1 == choice2) return "The result is a tie!"; | |
if (choice1 == "rock") { | |
if (choice2 == "paper") { | |
return "paper wins"; | |
} else { // scissors | |
return "rock wins"; | |
} | |
// or with a ternary statement... | |
//return (choice2 == "paper" ? "paper" : "rock") + " wins" |
This file contains hidden or 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
// say you have the following object: | |
var friends = { | |
bob: {name: "Bob Robertson", nickname: "Bob the Terrible"}, | |
jim: {name: "Jim Jameson", nickname: "Jim the Magnificent"}, | |
bill: {name: "Bill Williamson", nickname: "Doodles"}, | |
} | |
// So if you want to get Bob's nickname, you could access it like so: | |
console.log(friends.bob.nickname); // prints "Bob the Terrible" | |
// but say you just have Bill's name in a variable, and you need to look it up. |
This file contains hidden or 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
// mostly stolen from http://phrogz.net/JS/classes/OOPinJS2.html | |
// first we define a mammal... | |
function Mammal(name) { | |
this.name = name; | |
this.offspring = []; | |
} | |
Mammal.prototype.haveABaby = function() { | |
var newBaby = new Mammal("Baby " + this.name); | |
this.offspring.push(newBaby); |
This file contains hidden or 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
# Big O notation is "worst case" time of an algorithm: | |
# - O is used as a function | |
# - n the input (mostly assumed to be an integer) | |
# - any transformations done to n (say 2n) are representative of the operations inside the algorithm | |
# | |
# so take the following: | |
def count_to_n(n): | |
"count up to n (obviously contrived)" | |
# do some preparation work |
This file contains hidden or 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
# define our higher-order function | |
def more_verboser(func): | |
def inner(x): | |
val = func(x) | |
print val | |
return val | |
return inner | |
# defined without decorator |
This file contains hidden or 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
// reducer | |
function reduce(arr, func, init) { | |
for (var i = 0; i < arr.length; i++) { | |
init = func(init, arr[i], i, arr); | |
} | |
return init | |
} |
This file contains hidden or 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
$ history | awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}' | sort -rn | head -n 20 | |
2502 git # Developing - everything is managed with git | |
1046 t # Taskwarrior alias | |
589 vim # I use vim constantly | |
303 rm # Sometimes files just need to go | |
269 cd # moving around | |
258 ./manage.py # Django management | |
253 pip # installing/uninstalling/freezing python packages | |
240 curl # useful for developing APIs | |
235 mv # ... and sometimes files need to move |
This file contains hidden or 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
(defn euclidian [a b] | |
(Math/sqrt (+ | |
(Math/pow (- (a 0) (b 0)) 2) | |
(Math/pow (- (a 1) (b 1)) 2)))) |
This file contains hidden or 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
//// SLICE //// | |
// first we need an object | |
x = [1, 2, 3] | |
// slicing gets a new value, without modifying the old one | |
x.slice(0, 2) == [1, 2] // true | |
x == [1, 2, 3] // true | |
// so we can call it multiple times and x is never mutated | |
x.slice(0, 2) |
This file contains hidden or 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
function inRange(min, max, value) { | |
return (min == null || min <= value) && | |
(max == null || max >= value) | |
} | |
// evalutation of inRange(1, 2, 1.5) | |
(1 == null || 1 <= 1) && (2 == null || 2 >= 1) | |
(false || true) && (false || true) | |
true && true | |
true |