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
#!/usr/bin/ruby | |
def fibonacci_recursive(limit, count, first, second) | |
if count == limit then return [first + second] end | |
[first + second].concat fibonacci_recursive(limit, count + 1, second, first + second) | |
end | |
def fibonacci(limit) | |
[1, 1].concat fibonacci_recursive(limit, 0, 1, 1) |
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
#!/usr/bin/ruby | |
class Array | |
def reverse_recursive(arr) | |
if arr.length == 1 then return arr end | |
a = arr.shift | |
reverse_recursive(arr) << a | |
end |
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
#!/usr/bin/ruby | |
def pi_recursive(depth, count, i) | |
if count >= depth then return 0 end | |
r = 4.0 / (i..(i + 2)).to_a.inject(1) { |sum, x| sum * x } | |
pi_recursive(depth, count + 1, i + 2) + | |
if count % 2 == 1 then + r else - r end | |
end |
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
#!/usr/bin/ruby | |
def find_duplicates(arr) # O(n) | |
hash = Hash.new(0) | |
duplicates = [] | |
arr.each do |val| | |
if hash.has_key? val and hash[val] == 1 then duplicates.push val end | |
hash[val] += 1 | |
end |
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
#!/bin/sh | |
# put this in your .git/hooks directory and name it pre-commit | |
bundle exec cane |
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
((options) -> | |
{one, two} = options | |
console.log one + ' ' + two | |
)({one: 'foo', two: 'bar'}) |
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
#!/usr/bin/node | |
fs = require('fs'); | |
function secretDecoderRing(fileSecret, fileKey) { | |
fs.readFile(fileKey, 'utf-8', function(err, key) { | |
if (!err) { | |
fs.readFile(fileSecret, 'utf-8', function(err, secret) { | |
if (!err) { | |
var numbers = secret.split(' '); | |
var result = numbers.map(function(num) { |
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
#!/usr/bin/node | |
fs = require('fs'); | |
Q = require('q'); | |
function secretDecoderRing(fileSecret, fileKey) { | |
Q.all([readFile(fileSecret), readFile(fileKey)]) | |
.then(decodeArray) | |
.then(concatArray) | |
.then(console.log) | |
.fail(console.log); // log any errors |
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
toggle = Bacon | |
.update(0, [Bacon.interval(1000)], (x) -> x + 1) | |
.map (x) -> x % 2 | |
toggle.log() # 1 0 1 0 ... |
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
doStuff = -> | |
Bacon.once {time: 1000} | |
result = doStuff() | |
newStream = result.flatMap (val) -> | |
Bacon.once(val) | |
.merge doStuff().delay(val.time) |
OlderNewer