This file contains hidden or 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
let filter_take ~f:valid n list = | |
let rec take_from ?(taken = []) ?(n_taken = 0) list = | |
if n_taken = n then taken | |
else | |
match list with | |
| [] -> taken | |
| e :: remaining -> | |
let taken, n_taken = | |
if valid e then (e :: taken, n_taken + 1) else (taken, n_taken) | |
in |
This file contains hidden or 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
module ValueObject | |
def ==(other) | |
case other | |
when ValueObject | |
self.object_value == other.object_value | |
else | |
false | |
end | |
end |
This file contains hidden or 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 "open3" | |
require "json" | |
class ShellCommand | |
attr_reader :line, :stdin, :binmode, :stdout, :stderr, :status | |
def initialize(line, stdin, binmode, stdout, stderr, status) | |
@line = line | |
@stdin = stdin |
This file contains hidden or 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 Hash | |
def shape | |
self.class.shape_of self | |
end | |
def self.shape_of(hash) | |
hash.sort_by{|k,v| k.to_s }.map do |k,v| | |
v = case v | |
when Array |
This file contains hidden or 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 Hash | |
def deep_slice(schema) | |
self.class.deep_slice(self, schema) | |
end | |
def self.deep_slice(hash, schema) | |
nested_schemas = schema.last.is_a?(Hash) ? schema.last : {} | |
keys = schema.reject{|e| e.is_a?(Hash) } + nested_schemas.keys |
This file contains hidden or 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 ApplicationRecord | |
def self.string_enum(column_name, values, **options) | |
enum column_name => values.map(&:to_s).index_by(&:itself), **options | |
end | |
end |
This file contains hidden or 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 ApplicationRecord | |
def add_error(attribute, type, message=nil) | |
attribute = attribute.to_s.to_sym | |
type = type.to_s.to_sym | |
errors.add attribute, type, message: message | |
end | |
def has_error?(attribute, type) |
This file contains hidden or 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 ApplicationRecord | |
def self.random | |
max = maximum(primary_key) | |
return if max.nil? | |
find_random = lambda do | |
order(primary_key).find_by(primary_key => (rand(max)..)) | |
end |
This file contains hidden or 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 lazy_demo(n, group_size:n, groups:n) | |
(1..Float::INFINITY) | |
.lazy | |
.map{|x| x * n } | |
.select{|product| product.even? } | |
.each_slice(group_size) | |
.map{|group| group.map(&:to_s).join(",") } | |
.first(groups) | |
end |
OlderNewer