Skip to content

Instantly share code, notes, and snippets.

# List comprehension
# Haskell
[x*2 | x <- [1..10]] #=> [2,4,6,8,10,12,14,16,18,20]
# Ruby
(1..10).map { |x| x*2 } #=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
# List comprehension with condition
# Haskell
[x*2 | x <- [1..10], x*2 >= 12] #=> [12,14,16,18,20]
class FastBeustSequence
class << self
def find_all(max)
@listener = []
zero = Digit.new(nil, 0)
one = zero.next
start = Time.now
(1..10).each {|length| find(one, zero, length, 0, max, @listener)}
diff --git a/dygraph.js b/dygraph.js
index 3d3326e..374c355 100644
--- a/dygraph.js
+++ b/dygraph.js
@@ -1521,8 +1521,18 @@ Dygraph.prototype.drawGraph_ = function(data) {
for (var i = 1; i < data[0].length; i++) {
if (!this.visibility()[i - 1]) continue;
+ // Reduce the resolution of the dataset if necessary.
+ var data_length = data.length
#!/usr/local/bin/ruby
print "Input /^\\d \\d [NSEW] [LMR]+$/: "
raise "Bad input" unless input = gets.chomp.match(/^(\d+)\s(\d+)\s([NSEW])\s([LMR]+)$/)
x, y, direction, moves = input[1].to_i, input[2].to_i, input[3], input[4]
compass = %w{ N E S W }
i = compass.index(direction)
fns = lambda { y+=1 }, lambda { x+=1 }, lambda { y-=1 }, lambda { x-=1 }
#!/usr/bin/ruby
class RoverRemote
def initialize(cmds)
parse(cmds)
end
def parse(cmds)
cmds.split("\n").each do |cmd|
case cmd
@krishicks
krishicks / gist:1073563
Created July 9, 2011 12:52
Double-polymorphic has_many through modeling in Rails
class Article < ActiveRecord::Base
has_many :content_relationships, as: :origin, dependent: :destroy
has_many :related_articles, through: :content_relationships, source: :related_content, source_type: "Article"
has_many :related_videos, through: :content_relationships, source: :related_content, source_type: "Video"
end
class Video < ActiveRecord::Base
has_many :content_relationships, as: :origin, dependent: :destroy
@krishicks
krishicks / bit_rover.rb
Created July 27, 2011 02:11
yet another mars rover solution, this time using bit shifting and a hash that maps between squares of two and x,y coordinates
# lines 3..14 create a hash with squares of 2 pointing at their
# associated x,y coordinates as well as the inverse
width = 5
left = []
width.times { |i| left += [i] * (width + 1) } # [0,0,0,0,0,1,1,1,1,1..]
right = [*0..width] * width # [0,1,2,3,4,5,0,1,2,3,4,5..]
tuples = right.zip left # [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [0, 1], [1, 1]..
@krishicks
krishicks / .gitconfig
Last active September 26, 2015 14:37
.gitconfig
[core]
whitespace = indent-with-non-tab tab-in-indent trailing-space -blank-at-eof
[grep]
line-number = true
[help]
autocorrect = 1
[alias]
st = status
di = diff
co = checkout
@krishicks
krishicks / constantize.rb
Created September 30, 2011 14:34
active_support_inflector => ActiveSupport::Inflector
require 'active_support/inflector'
constantize = lambda { |str|
result = ""
str.split("_").inject("") { |acc, klass|
begin
try = acc + klass.classify
try.constantize
@krishicks
krishicks / chunkify.rb
Last active December 11, 2015 21:19
chunkify
def chunkify enum, out, limit, &block
enum.inject([]) do |acc, e|
size = (acc+[e]).join(",").size
if size > limit
out += yield(acc)
acc = [e]
elsif e == enum.last
out += yield(acc + [e])
else