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
# a heavy int is one where the average of the digits is greater than 7 | |
# eg: 8678 is heavy because (8 + 6 + 7 + 8) / 4 = 7.25 | |
# 8677 is not heavy because ( 8 + 6 + 7 + 7) / 4 = 7 | |
def num_heavy_ints(a, b): | |
""" | |
Return the number of heavy integers between a and b (inclusive). | |
Assume that a and b are positive integers. | |
>>> num_heavy_ints(8675, 8689) |
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
// Scala is great | |
def ipToInt(ip: String) = ip.split('.').map(_.toInt).foldLeft(0)((sum, i) => sum * 256 + i) |
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
function when(x) { | |
return function () { | |
for (var i in arguments) { | |
var result = arguments[i](x); | |
if (result !== false) { | |
return result; | |
} | |
} | |
throw "No patterns matched when(" + x + ")"; | |
}; |
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
function maybe(value) { | |
var obj = null; | |
function isEmpty() { return value === undefined || value === null } | |
function nonEmpty() { return !isEmpty() } | |
obj = { | |
map: function (f) { return isEmpty() ? obj : maybe(f(value)) }, | |
getOrElse: function (n) { return isEmpty() ? n : value }, | |
isEmpty: isEmpty, | |
nonEmpty: nonEmpty | |
} |
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 book = db.query("SELECT * FROM book WHERE id = 1"); | |
function printTitle(book) { console.log(book.title) } | |
maybe(book).map(printTitle); |
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
function tryo(fn) { | |
var res = null; | |
try { | |
res = fn(); | |
} catch (e) { | |
res = null; | |
} | |
return maybe(res); | |
} |
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
function map(f, a) { | |
return a.length ? [].concat(f(a[0]), map(f, a.slice(1))) : [] | |
} | |
function filter(f, a) { | |
return map(function (x) { return f(x) ? x : [] }, a) | |
} | |
var even = function (x) { return x % 2 == 0 }; |
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
function lens(get, set) { | |
var f = function (a) { return get(a); }; | |
f.set = set; | |
f.mod = function (f, a) { return set(a, f(get(a))); }; | |
return f; | |
} | |
var first = lens( | |
function (a) { return a[0]; }, | |
function (a, b) { return [b].concat(a.slice(1)); } |
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
function drawMap(map) { | |
var i, x, y, height, pos, | |
width = map[0].length, | |
total_nodes = map.length * width; | |
for (i = 0; i < total_nodes; i++) { | |
x = i % width; | |
y = Math.floor(i / width); | |
height = { | |
top: map[y][x], | |
right: map[y][x+1], |
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
function drawMap(map) { | |
var x, y, | |
width = map[0].length, | |
current_node = map.length * width; | |
while (current_node--) { | |
x = current_node % width; | |
y = ~~(current_node / width); | |
drawTile({ | |
x: x, | |
y: y |
OlderNewer