Skip to content

Instantly share code, notes, and snippets.

@mikrofusion
mikrofusion / fibonacci.rb
Last active May 6, 2019 01:48
fibonacci (ruby)
#!/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)
@mikrofusion
mikrofusion / reverse_array.rb
Created March 16, 2014 18:44
reverse array (ruby)
#!/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
@mikrofusion
mikrofusion / pi.rb
Created March 17, 2014 04:28
calculate pi
#!/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
@mikrofusion
mikrofusion / find_duplicates.rb
Created March 17, 2014 05:16
find duplicates (ruby)
#!/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
@mikrofusion
mikrofusion / pre-commit
Created April 9, 2014 18:12
Add cane to git pre-commit hook
#!/bin/sh
# put this in your .git/hooks directory and name it pre-commit
bundle exec cane
@mikrofusion
mikrofusion / destructuring_assignment.coffee
Created May 11, 2014 17:22
Destructuring Assignment CoffeeScript
((options) ->
{one, two} = options
console.log one + ' ' + two
)({one: 'foo', two: 'bar'})
@mikrofusion
mikrofusion / secretDecoderRingTheWrongWay.js
Last active August 29, 2015 14:02
Secret Decoder Ring In Javascript / Node (Callback Hell Example)
#!/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) {
@mikrofusion
mikrofusion / secretDecoderRing.js
Last active August 29, 2015 14:02
Secret Decoder Ring In Javascript / Node (Callback Hell Example)
#!/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
@mikrofusion
mikrofusion / BaconToggle.coffee
Last active August 29, 2015 14:05
Creates a stream to toggle between 0 and 1 every second
toggle = Bacon
.update(0, [Bacon.interval(1000)], (x) -> x + 1)
.map (x) -> x % 2
toggle.log() # 1 0 1 0 ...
@mikrofusion
mikrofusion / delay.coffee
Created August 24, 2014 01:30
Baconjs - delay a second event by the result of the first event
doStuff = ->
Bacon.once {time: 1000}
result = doStuff()
newStream = result.flatMap (val) ->
Bacon.once(val)
.merge doStuff().delay(val.time)