Skip to content

Instantly share code, notes, and snippets.

View asaaki's full-sized avatar
🦀
I may be slow to respond. Ping me on Twitter instead: https://twitter.com/asaaki

Christoph Grabo asaaki

🦀
I may be slow to respond. Ping me on Twitter instead: https://twitter.com/asaaki
View GitHub Profile
@asaaki
asaaki / boolean.rb
Last active August 29, 2015 14:05
[Ruby] Boolean class
class Boolean
TRUE_VALUES = [true, 1, :true, "true", "1", "t", "T", "yes", "y", "Y"]
FALSE_VALUES = [false, 0, :false, "false", "0", "f", "F", "no", "n", "N"]
ALL_VALUES = TRUE_VALUES + FALSE_VALUES
InvalidInput = Class.new(StandardError)
def initialize(input)
@input = input
end
### WHY AND HOW
#
# ConCache has some nice features, the ones we will use here are:
#
# `get_or_store`:
# Retrieve a cached value or store a new one.
# Blocks other processes until the value is written,
# then the other processes will read the cached value.
#
# `callback` function:
@asaaki
asaaki / line-grep.sh
Created October 6, 2014 14:52
Grep a range of lines from a file
awk 'NR >= 100 && NR <= 300' file.in
sed -n '100,300p' file.in
# there are also solutions with head+tail, but it's not very intuitive for me
# example with bzipped files:
bzcat file.in | awk 'NR >= 100 && NR <= 300'
bzcat file.in | sed -n '100,300p'
@asaaki
asaaki / frp_stream_ticker.ex
Created October 17, 2014 16:15
[FRP] Stream Ticker
# Wrap it in a task (or just spawn a process) to not block your IEx shell
# or the following function calls:
Task.start(fn ->
Stream.interval(1000) # in ms, so quite high-resolution loops are possible
|> Stream.map(fn (_) -> "Tick!" end)
|> Stream.each(fn (v) -> IO.puts(v) end)
|> Stream.run # this will block the process forever!
end)
# For serious usage you also would need to supervise this process
@asaaki
asaaki / dir_of_script.sh
Created October 27, 2014 23:14
Get directory of script within script
#!/bin/sh
CURRENT_DIR=$( cd "$(dirname "$0")"; pwd )
CURRENT_DIR=$( cd ${0%/*} && pwd -P )
echo $CURRENT_DIR
@asaaki
asaaki / fun_with_string_next.rb
Created February 19, 2015 17:04
FunWithStringNext
class FunWithStringNext < ActiveRecord::Base
def create_identifier
total = FunWithStringNext.count
# ENUMERATOR: identifier_generator.take(total).last
generate_identifier_by_count(total)
end
def identifier_generator
Enumerator.new do |yielder|
init = INITIAL_IDENTIFIER.dup
@asaaki
asaaki / RESULTS
Created February 23, 2015 00:53
Test for `cond` truthy conditions
CondTest.run
*** &CondTest.cond_with_true/0 ***
72.2 sec 536M iterations 0.14 μs/op
*** &CondTest.cond_with_atom/0 ***
72.8 sec 536M iterations 0.14 μs/op
*** &CondTest.cond_with_truthy_evaluation/0 ***
76.4 sec 536M iterations 0.15 μs/op
@asaaki
asaaki / base_service.rb
Created March 18, 2015 10:35
Ruby: BaseService template
class BaseService
class << self
def add_error(name, message)
error_klass = Class.new(StandardError) do
define_method :initialize do |msg = message|
super(msg)
end
end
const_set(name, error_klass)
@asaaki
asaaki / hash_ext.rb
Created May 12, 2015 13:45
Ruby Hash extension: .subhash(*keys)
# Stolen from <http://stackoverflow.com/a/9025552/653173>
module HashExt
def subhash(*keys)
keys = keys.select { |k| key?(k) }
Hash[keys.zip(values_at(*keys))]
end
end
defmodule SigilZ do
defmacro sigil_z(input, flags) do
quote do
[input: unquote(input), flags: unquote(flags)]
end
end
end
# then: