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 / playground.rs
Created January 26, 2018 08:42 — forked from anonymous/playground.rs
Rust code shared from the playground
use std::ops::Deref;
struct StateFn(fn(&mut Machine) -> StateFn);
impl Deref for StateFn {
type Target = fn(&mut Machine) -> StateFn;
fn deref(&self) -> &Self::Target {
&self.0
}
@asaaki
asaaki / hello.rs
Last active January 26, 2018 01:57
parcel.js/Rust: return a string
/*
gist is: return a ref to c_char and the length of it (via 2 functions)
C example: https://wasdk.github.io/WasmFiddle//?w590f
*/
use std::ffi::CString;
use std::os::raw::c_char;
static HELLO: &'static str = "hello from rust";
@asaaki
asaaki / shell-script-snippets.sh
Last active December 16, 2017 01:48
Shell Snippets
#!/usr/bin/sh
# Count lines of multiple files (recursively):
find source_dir -name "*.ext" -exec wc -l {} +
# 7 digit sha1 transformed timestamp name:
### simple: date +%s%N | \
### more unique
/usr/lib/systemd/systemd-timestamp | \
sha1sum | \
@asaaki
asaaki / private_constants.rb
Created November 1, 2017 12:50
[Ruby] Private constants check
require 'active_support'
module MyConcern
extend ActiveSupport::Concern
class PrivateClass
def self.pcall
:pcall_result
end
end
@asaaki
asaaki / do.rb
Last active July 28, 2017 14:45
What Ruby does …
# Ruby's do notation
# Don't do!
module Foo; end
module FooDo do; end
#=> SyntaxError: unexpected keyword_do, expecting ';' or '\n'
class Foo; end
class FooDo do; end
@asaaki
asaaki / hash-with-indifferent-access.rb
Created July 3, 2017 11:38
Simple hash with indifferent access (string and symbol keys)
# ref:
# https://github.com/sinatra/sinatra/blob/8c2504c/lib/sinatra/base.rb#L1082-L1085
module HashWithIndifferentAccess
def self.new
Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
end
end
# ref:
# https://github.com/sinatra/sinatra/blob/8c2504c/lib/sinatra/base.rb#L1068-L1080
@asaaki
asaaki / value.sh
Last active May 9, 2017 11:38
Shell scripting: check if value is in string as conditional
#!/bin/sh
POSSIBLE_VALUES="foo bar baz"
TEST_VALUE=$1
echo $POSSIBLE_VALUES | grep $TEST_VALUE 1>/dev/null
exit $?
@asaaki
asaaki / currying.ex
Last active March 30, 2017 23:49
Partial Functions / Currying
# Another example for: http://onor.io/2014/03/31/partial-function-application-in-elixir/
defmodule PartFuncs do
defp addfun(x, y), do: x + y
# return a partially applied function
def add(a), do: &addfun(a, &1)
# could also easily be written as: addfun(a, b)
def add(a, b), do: (&addfun/2).(a, b)
@asaaki
asaaki / operator_foo.rb
Last active February 26, 2017 14:53
[Ruby] `&&` vs. `and`
# Helper:
# alternative `puts` with a non-nil return value
# similar to many method calls which could be useful with logical operations
def puts_with_true(arg)
puts(arg)
true
end
# puts returns `nil` by default
puts 1 && 2
@asaaki
asaaki / elixir-library-error-handling.ex
Created February 16, 2017 13:15
Error Handling in Elixir Libraries
# example from:
# http://michal.muskala.eu/2017/02/10/error-handling-in-elixir-libraries.html
defmodule YourLibrary do
defmodule Error do
defexception [:reason]
def exception(reason),
do: %__MODULE__{reason: reason}
def message(%__MODULE__{reason: reason}),