Skip to content

Instantly share code, notes, and snippets.

View orend's full-sized avatar

Oren Dobzinski orend

View GitHub Profile
def bench(descr)
start = Time.now
yield
puts "#{descr} #{Time.now-start} seconds"
end
def without_pipelining(u)
r = Redis.new
10000.times {
r.sadd(u.deleted_fls.key, 43)
@orend
orend / gist:1180957
Created August 30, 2011 14:00
synchrony driver benchmarking
So using the synchrony driver I get the following with concurrency level of 1:
Server Software: thin
Server Hostname: 0.0.0.0
Server Port: 5000
Document Path: /vsync/sync
Document Length: 3002 bytes
require "money"
class Decorator < BasicObject
def initialize(component)
@component = component
end
def method_missing(name, *args, &block)
@component.send(name, *args, &block)
end
@orend
orend / gist:3305160
Created August 9, 2012 15:33
games with curry
>> %w(NYC TLV LSW).product(["airport"]).map{|x| x * ' '}
# equivalent to
>> %w(NYC TLV LSW).product(["airport"]).map{|x| x.join(' ')}
[
[0] "NYC airport",
[1] "TLV airport",
[2] "LSW airport"
]
# now using curry:
@orend
orend / gist:3691396
Created September 10, 2012 15:10
&method
[1,2,3].each(&method(:puts)) # & creates a block from a proc, Method has a to_proc method. This is a shortcut for [1,2,3].each(&method(:puts).to_proc)
[1, 2, 3].map(&2.method(:*))
# => [2, 4, 6]
p = method(:puts)
[1,2,3].tap(&p).inject(&:+) # if you need to print something in the middle of a call chain
#something else:
>> (1..10).zip(11..20).map{|x, y| x+y}
@orend
orend / gist:3698516
Created September 11, 2012 13:34
memoization using a hash. from rubies in the rough
def fibonacci(nth, series = method(:fibonacci))
if nth < 2
nth
else
series[nth - 2] + series[nth - 1]
end
end
# The easiest memoization in the world:
fibs = Hash.new { |series, nth| series[nth] = fibonacci(nth, series) }
@orend
orend / gist:3767279
Created September 22, 2012 18:15
ruby's magic underscore
people = {
"Alice" => ["green", 34, "alice@example.com"],
"Bob" => ["brown", 27, "bob@example.com"]
}
# No problem. Just re-use the underscore:
people.map { |name, (_, _, email)| [name, email] }
a, _, b, _, c = [1, 2, 3, 4, 5]
@orend
orend / gist:3768467
Created September 23, 2012 01:31
ex1 scala course
package recfun
import common._
object Main {
def main(args: Array[String]) {
println("Pascal's Triangle")
for (row <- 0 to 10) {
for (col <- 0 to row)
print(pascal(col, row) + " ")
println()
@orend
orend / gist:3797536
Created September 28, 2012 01:50
structs and ostructs
Point = Struct.new(:x, :y)
origin = Point(0,0)
# OStructs are particularly good for configuration objects. Since any method works to set data in an OStruct, you don't have to
# worry about enumerating every single option that you need:
require 'ostruct'
def set_options
opts = OpenStruct.new
@orend
orend / gist:3797650
Created September 28, 2012 02:41
each_with_object instead of inject
# replace this
%w(foo bar).inject({}) { |hsh, str| hsh[str] = str.upcase; hsh }
# with this. note that memo is second here
%w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase }
# => {'foo' => 'FOO', 'bar' => 'BAR'}