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
object Loops extends App { | |
print("for (i <- 1 to 10)\n\t") | |
// for/Range using to | |
for (i <- 1 to 10) { | |
print("#" + i) | |
} | |
print("\nfor (i <- 1 until 10)\n\t") |
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
def sqrt(x: Double): Double = { | |
def isGoodEnough(guess: Double): Boolean = | |
abs(square(guess) - x) / x < 0.001 | |
def improve(guess: Double): Double = | |
(guess + x / guess) / 2 | |
def sqrtIter(guess: Double): Double = | |
if (isGoodEnough(guess)) guess | |
else sqrtIter(improve(guess)) |
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
// If the second parameter isn't set to eval as CBN | |
// then infinite loops will hang before the method even begins | |
def and(x: Boolean, y: => Boolean) = | |
if (x) y else false | |
and(true, false) | |
and(false, true) | |
and(false, false) | |
and(true, true) |
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
_.chain([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) | |
// Grab each sublist and... | |
.map(function(sublist) { | |
// Sum each of its elements up! | |
return _.reduce(sublist, function(total, num) { | |
return total += num; | |
}); | |
}).value(); |
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 unflatten(list, group) { | |
var result = []; | |
for (var j = 0; j < Math.ceil(list.length/group); j++) { | |
for (var i = 0, sublist = [], cache = list[i+(j*group)]; i < group; i++) { | |
cache ? sublist.push(cache) : sublist; | |
} | |
result.push(sublist); | |
} | |
return result; | |
} |
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
var selectAll = function() { | |
views.filter(function(view) { | |
if (view.$el.hasClass("active") !== toggle) { | |
return true; | |
} | |
}).map(function(view) { | |
return view.$el; | |
}).forEach(function($el) { | |
$el.toggleClass("active", toggle) | |
.find("input:checkbox") |
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 unflatten(list, count) { | |
return _.map(_.range(Math.ceil(list.length/count)), function(mul) { | |
return list.slice(mul*count, mul*count+count); | |
}); | |
} | |
console.log(unflatten(_.range(1, 14), 3)); | |
// [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13]] |
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
def loop(num): | |
print("num: ", num) | |
if (num > 0): | |
loop(num - 1) | |
loop (10) |
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
var data = { | |
first: ["Alvaro", "Lewis", "Craig"], | |
last: ["Carrasco", "Moronta", "Pottinger"] | |
}, result = []; | |
var combineNames = function() { | |
return _.zip(_.values(data)[0], _.values(data)[1]); | |
}; | |
var assignKeys = function(name) { |
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
object Main extends App { | |
// Find any place in the triangle using n choose k | |
def pascal2(r: Int, c: Int): Int = { | |
def factorial(n: Int): Int = | |
if (n < 1) 1 else n * factorial(n - 1) | |
factorial(r) / (factorial(c) * factorial(r - c)) | |
} | |
def pascal(r: Int, c: Int): Int = { |
OlderNewer