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 pretty_print_attributes(q, attributes) | |
id = "%x" % (__id__ * 2) | |
id.sub!(/\Af(?=[[:xdigit:]]{2}+\z)/, '') if id.sub!(/\A\.\./, '') | |
klass = self.class.pretty_inspect.chomp | |
q.group(2, "\#<#{klass}:0x#{id}", '>') do | |
q.seplist(attributes, lambda { q.text ',' }) do |key| | |
q.breakable |
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
pry(main):1> class Foo; attr_writer :bar; end | |
=> nil | |
pry(main):2> x = Foo.new | |
=> #<Foo:0x46040d8> | |
pry(main):3> x.bar ||= 3 # errors | |
NoMethodError: undefined method `bar' for #<Foo:0x000000046040d8> | |
from (pry):3:in `__pry__' | |
pry(main):4> (x.bar = x.bar if defined?(x.bar)) || x.bar = 3 # no error | |
=> 3 |
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
import System.Random (newStdGen, randoms) | |
import Control.DeepSeq | |
import Control.Monad.Par | |
import Data.List (sort) | |
mergeSort :: Ord a => [a] -> [a] | |
mergeSort [] = [] | |
mergeSort [x] = [x] | |
mergeSort xs = | |
let (firstHalf, secondHalf) = split xs |
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
class Wrong | |
def method_missing(m, *) | |
if m =~ /\Ahello_(.+)\z/ | |
puts "Hello, #{$1.capitalize}" | |
else | |
super | |
end | |
end | |
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
def unnormalized_fft(x, start_i = 0, n = x.size, step = 1) | |
if n == 1 | |
Vector[x[start_i]] | |
else | |
even = unnormalized_fft(x, start_i, n/2, step * 2) | |
odd = unnormalized_fft(x, start_i + step, n/2, step * 2) | |
out = Array.new(n) | |
(n/2).times do |k| | |
out[k] = even[k] + Complex.polar(1, -2*Math::PI*k/n)*odd[k] |
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
static bool read_token(const char **src, const char *str) { | |
const char *start = *src; | |
while (**src && *str && **src == *str) { | |
str++; | |
(*src)++; | |
} | |
if (*str) { | |
*src = start; | |
return false; |
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
Array.new(2) { |i| | |
Thread.new do | |
binding.pry_remote("localhost", 6000 + i) | |
end | |
}.each(&:join) |
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
class Solver | |
BaseCapacitors = [1e-9, 100e-9, 1e-6, 9e-9, 12e-9] | |
Capacitors = BaseCapacitors + | |
BaseCapacitors.product(BaseCapacitors).map { |a, b| a*b/(a+b) } + | |
BaseCapacitors.product(BaseCapacitors).map { |a, b| a+b } | |
Capacitors.uniq! | |
BaseResistors = [100.0, 1e3, 10e3, 100e3, 1e6, | |
2.2e3, | |
3.3e3] |
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 'drb' | |
host = "localhost" | |
port = 9876 | |
uri = "druby://#{host}:#{port}" | |
10.times do | |
local_ip = UDPSocket.open {|s| s.connect(host, 1); s.addr.last} | |
DRb.start_service "druby://#{local_ip}:0" |
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
declare | |
% Prend une liste de liste et les concatène toutes en une seule liste. | |
fun {Concat Xs} {FoldL Xs Append nil} end | |
% Applique F à chaque élément de Xs puis concatène tous les résultats. | |
fun {ConcatMap F Xs} {Concat {Map Xs F}} end | |
fun {Mul2 A B} | |
{ConcatMap fun {$ X} {Map B fun {$ Y} X#Y end} end A} |