Skip to content

Instantly share code, notes, and snippets.

View BrianHicks's full-sized avatar

Brian Hicks BrianHicks

View GitHub Profile
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"
// 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.
// 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);
# 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
@BrianHicks
BrianHicks / xmpl.py
Last active December 15, 2015 10:29
# define our higher-order function
def more_verboser(func):
def inner(x):
val = func(x)
print val
return val
return inner
# defined without decorator
// reducer
function reduce(arr, func, init) {
for (var i = 0; i < arr.length; i++) {
init = func(init, arr[i], i, arr);
}
return init
}
$ 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
(defn euclidian [a b]
(Math/sqrt (+
(Math/pow (- (a 0) (b 0)) 2)
(Math/pow (- (a 1) (b 1)) 2))))
//// 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)
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