This file contains 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
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) |
This file contains 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
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 |
This file contains 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
require "money" | |
class Decorator < BasicObject | |
def initialize(component) | |
@component = component | |
end | |
def method_missing(name, *args, &block) | |
@component.send(name, *args, &block) | |
end |
This file contains 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
>> %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: |
This file contains 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
[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} |
This file contains 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
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) } |
This file contains 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
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] |
This file contains 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
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() |
This file contains 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
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 |
This file contains 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
# 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'} |
OlderNewer