Skip to content

Instantly share code, notes, and snippets.

View gist:9eaf97b6cf6f9adf7a467472196abb6f
git branch | grep -v master | sed s/^..// | xargs git branch -D
@haazime
haazime / eratos.rb
Created Apr 24, 2016
Sieve of Eratosthenes
View eratos.rb
def compute(number)
sieve((2..number).to_a, [], Math.sqrt(number))
end
def sieve(list, primes, stopper)
return primes + list if list.first >= stopper
primes << list.shift
list.reject! { |n| n % primes.last == 0 }
sieve(list, primes, stopper)
end
View app.rb
require 'bundler/setup'
Bundler.require
require 'active_record'
require 'action_controller/railtie'
require 'action_view/railtie'
# ActiveRecord::Config
ActiveRecord::Base.configurations = {'development' => {:adapter => 'sqlite3', :database => ':memory:'}}
ActiveRecord::Base.establish_connection :development
@haazime
haazime / regexp_union.rb
Created Aug 18, 2014
配列をもとに正規表現オブジェクトをつくる ref: http://qiita.com/haazime/items/9b39af6708cc214b2651
View regexp_union.rb
words = %w(foo bar baz)
regexp = Regexp.union(words)
@haazime
haazime / i_f.go
Created Aug 12, 2014
Goのインタフェースとその実装、およびstructへの埋め込み ref: http://qiita.com/haazime/items/2cc00097215ee8828224
View i_f.go
package main
import "fmt"
// 料理を作るI/F
type Cooker interface {
Cook() string
}
// 塗るI/F
View 1_before_karaoke_machine.rb
class KaraokeMachine
def initialize(melody_string)
@melody = Melody.parse(melody_string)
end
def transpose(amount)
@melody.transpose(amount).present
end
end
@haazime
haazime / mixin.go
Last active Apr 13, 2022
mixin with golang
View mixin.go
package main
import "fmt"
// 料理を作るI/F
type Cooker interface {
Cook() string
}
// 塗るI/F
View gist:9908372
#logs
@timeline = Timeline.find(current_user)
@timeline.each do |activity|
p activity.date
p activity.balance
p activity.outgo
p activity.income
end
@haazime
haazime / group_by.pl
Created Feb 13, 2014
PerlでRubyのEnumerable#group_by
View group_by.pl
sub group_by(&@) {
my $block = shift;
my %result;
for (@_) {
my $key = $block->($_);
$result{$key} = [] unless exists $result{$key};
push @{ $result{$key} }, $_;
}