Skip to content

Instantly share code, notes, and snippets.

View animatedlew's full-sized avatar

Lewie [m80] animatedlew

View GitHub Profile
@animatedlew
animatedlew / ranges.scala
Last active December 27, 2015 11:29
Scala Ranges & Loops
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")
@animatedlew
animatedlew / sqrt.scala
Last active December 27, 2015 13:09
This function uses the Newton method to estimate a square root function. The iterator starts off with a guess of 1.0. This guess is then squared to see if the difference is small enough to call it 'good.' For really large/small values this estimate is normalized before comparing it to an epsilon. The results are gradually improved by taking the …
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))
@animatedlew
animatedlew / logic.scala
Created November 6, 2013 05:31
Here I implement 'and' & 'or' functions using standard if-else expressions.
// 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)
@animatedlew
animatedlew / reduceSubLists.js
Created November 7, 2013 02:26
Here is an example of reducing sublists to their sums using functional JavaScript.
_.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();
@animatedlew
animatedlew / imperativeFunk.js
Last active December 27, 2015 15:29
Here is a mix of imperative and functional programming.
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;
}
@animatedlew
animatedlew / selectAll.js
Created November 7, 2013 03:00
This is an example of functional programming using JavaScript's built-in filter, map, and forEach functions. Note that this isn't a fully functional style as there are side effects.
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")
@animatedlew
animatedlew / unflatten.js
Last active December 27, 2015 15:29 — forked from panda01/Unflatten
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]]
def loop(num):
print("num: ", num)
if (num > 0):
loop(num - 1)
loop (10)
@animatedlew
animatedlew / transformData.js
Last active December 27, 2015 17:29
In this exercise, I transform given data into a list of objects that have 'first' and 'last' labels as keys.
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) {
@animatedlew
animatedlew / pascal.scala
Last active December 27, 2015 18:09
A little program that uses recursion, and uses combinations to draw out Pascal's Triangle.
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 = {