Skip to content

Instantly share code, notes, and snippets.

@toraritte
toraritte / gcd.rb
Last active December 14, 2015 07:09
Greatest Common Divisor by Matma.Rex (Bartosz Dziewoński) on ruby-talk http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/405031
# original code
gcd=->(_){_.sort_by(&:-@).inject{|p,q|q.nonzero?&&gcd[[q,p-q]]||--p}}
$/=' ';gets;puts gcd[[$_,gets].map &(method :Integer)]
# original code modified (puts&gets removed, made example explicit, etc.)
# === 1st line ===
gcd=->(_){_.sort_by(&:-@).inject{|p,q|q.nonzero?&&gcd[[q,p-q]]||--p}}
# -@ unary minus—returns the receiver’s value, negated.
# Numeric.instance_methods(false).grep /@/ #=> [:+@, :-@]
@toraritte
toraritte / count_chars.erl
Created June 28, 2015 01:04
count_chars example from Programming Erlang 2nd ed that works
-module(count_chars).
-export([count_chars/1, count_characters/1]).
count_characters(Str) ->
count_characters(Str, #{}).
%% maps module functions cannot be used as guards (release 17)
%% or you'll get "illegal guard expression" error
count_characters([H|T], X) ->
@toraritte
toraritte / gist:2eddbbddddc4841d403c
Last active November 20, 2018 13:08 — forked from spacejam/gist:dd5901c8b2920bb0f1ec
net_kernel:connect/1 VS net_kernel:connect_node/1

=== CALL TRACE OF net_kernel:connect\1

% otp/lib/kernel/src/net_kernel.erl
connect(node_name@machine) -> do_connect(node_name@machine, normal, false). ->
  %          Node,              Type,   WaitForBarred
  do_connect(node_name@machine, normal, false           ) -> %% Type = normal | hidden
    case catch ets:lookup(sys_dist, Node) of
        % [{connection, ...}] if node is in sys_dist
        % []                  if not
	{'EXIT', _} ->
@toraritte
toraritte / linkmon.erl
Last active May 18, 2016 23:41
Process chaining with and without a receive block
% The cause of the second output is (where the
% receive block is commented out) is that because
% the process does not expect any messages and it
% ends naturally.
%
% On the other hand, the version with receive
% expects messages (spits out any message that
% arrives) and it keeps on running until it is
% told to stop.
@toraritte
toraritte / binaries.erl
Last active November 20, 2018 13:09
Erlang binaries and unicode
% because 256 needs 9 bits to represent it
% although it is 2^8
62> <<256>>.
<<0>>
% because the segments of an Erlang binary
% has to be divisible by 8. Hence
% 1000 0000, 0
57> <<256:9>>.
<<128,0:1>>
@toraritte
toraritte / elixir_meta.ex
Last active March 20, 2017 03:48
Saving different working use cases using Elixir metaprogramming
###################################################
### Solution to `invalid quoted expression`
### short version: use `Macro.escape/1`
###
### https://elixirforum.com/t/cant-seem-to-unquote-a-map/4009
###################################################
########################################
### (1) do smth with map, tuple etc.
### but only in the body of the
@toraritte
toraritte / snippets.js
Last active July 7, 2017 12:20
elixir_sundry.ex
defmodule A do
def vmi, do: IO.puts(27)
def balabab, do: IO.puts(7)
def lofa do
f = [&vmi/0, &balabab/0]
Enum.each(f, &(&1.()))
end
end
# Partial EEx template application
#
# As OvermindDL1 and Jose' pointed out, this doesn't cover for
# templates like "<%= a + b %>" and why not just simply collect
# the assigns, and apply them when we have them all.
# https://groups.google.com/forum/#!topic/elixir-lang-core/pYSfP3x7vwc
# I still think that there would be a use case for partial application
# and it was a nice mind exercise:)
@toraritte
toraritte / rsync.sh
Last active July 9, 2017 01:10
rsync to remote machine
rsync -avz --delete --progress \
-e "ssh -i ~/.ssh/my_private_key" \ # or just `-e ssh` (see below)
/SOURCE_PATH username@1.2.3.4:/DESTINATION_PATH
# Breaking down:
# -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
#
# -r, --recursive recurse into directories
# -l, --links copy symlinks as symlinks
@toraritte
toraritte / identical_nodes.cypher
Last active August 30, 2017 21:45
Testing the creation of "identical" Neo4j and AgensGraph nodes (i.e., multiple nodes with the same attributes and labels)
////////////////////////////////////////////
// NEO4J GRAPH SETUP
//
// http://console.neo4j.org/?id=jkwe2h
////////////////////////////////////////////
CREATE
(kilgore:Person {name: 'Kilgore'}),
(logan:Person {name: 'Logan'}),
(u1:Unit {name: 'Apt 7A'}),