Skip to content

Instantly share code, notes, and snippets.

View hirotnk's full-sized avatar

Yoshihiro Tanaka hirotnk

  • Microsoft
  • Seattle
View GitHub Profile
8> Z = fun(F) -> Core = fun(X) -> fun(M) -> (F(X(X)))(M) end end, Core(Core) end.
#Fun<erl_eval.6.50752066>
9> Z(fun(F) -> fun(0) -> 1;(N) -> N*F(N-1) end end).
#Fun<erl_eval.6.50752066>
10> ZZ = Z(fun(F) -> fun(0) -> 1;(N) -> N*F(N-1) end end).
#Fun<erl_eval.6.50752066>
11> ZZ(5).
120
12>
@hirotnk
hirotnk / erl_eval_example.txt
Last active April 3, 2017 17:57
el_eval small example
48> {ok, Tokens, _} = erl_scan:string("A+1.").
{ok,[{var,1,'A'},{'+',1},{integer,1,1},{dot,1}],1}
49> {ok, Tree} = erl_parse:parse_exprs(Tokens).
{ok,[{op,1,'+',{var,1,'A'},{integer,1,1}}]}
50> erl_eval:exprs(Tree, [{'A',3}]).
{value,4,[{'A',3}]}
@hirotnk
hirotnk / parse_transform example
Last active March 29, 2017 05:24
parse_transeform example - from tlist to case statement
localhost [/home/yoshi_vagrant/egit%] erlc tlist_to_case.erl
localhost [/home/yoshi_vagrant/egit%] erlc -pa . +"{parse_transform,tlist_to_case}" tl_parset.erl
localhost [/home/yoshi_vagrant/egit%] cat case_statement.txt
do(Name) ->
case Name of
"US" ->
"001";
"GBP" ->
"002"
end.
@hirotnk
hirotnk / makecent.erl
Created December 21, 2016 09:15
Write a program that outputs all possibilities to put + or - or nothing between the numbers 1, 2, ..., 9 (in this order) such that the result is always 100. E.g.: 1 + 2 + 34 – 5 + 67 – 8 + 9 = 100.
1 -module(makecent).
2 -export([do/0,parse0/1,parse1/1]).
3
4 do() ->
5 lists:filter(
6 fun(E) ->
7 parse1(parse0(E)) == 100
8 end, genall(lists:seq(1,9))).
9
10 parse1([]) -> 0;
@hirotnk
hirotnk / rebar rel packaging memo
Created May 7, 2014 14:17
When packaging with rebar, I needed to edit retool.config. this is a memo where I had to modify the file.
$ [/Users/yoshihiro.tanaka/gith/sunaba%] tree [feature/gen_server_pool]
.
├── Makefile
├── README.md
├── deps
├── doc
│   ├── edoc-info
│   ├── erlang.png
│   ├── index.html
│   ├── modules-frame.html

Erlang Tracing: more than you wanted to know

Rough Outline

  • What can be traced?
  • How can trace events be specified?
  • "match specifications": twisty passages, all alike
  • WTF, can I just use DTrace and drink my coffee/beer/whisky in peace?
  • Trace delivery mechanisms: pick one of two
@hirotnk
hirotnk / topo.erl
Created January 17, 2011 06:53
Topological sort
-module(topo).
-compile(export_all).
% topological sort
add({A, B}, [], New) -> [{A, 0, [B]}] ++ New;
add({A, B}, [{A, Cnt, L}|T], New) -> [{A, Cnt+1, [B|L]}] ++ T ++ New;
add({A, B}, [{C, Cnt, L}|T], New) -> add({A, B}, T, [{C, Cnt, L}|New]).
add(E, L) -> add(E, L, []).
@hirotnk
hirotnk / floyd.pl
Created January 14, 2011 22:01
Floyd's algorithm with Perl
#!/usr/bin/perl
use strict;
# This program applies Floyd-Warshall
# All-Pairs Shortest Pairs Algorithm
# to aquire the shortest weiths for all pairs.
#
# Input: the graph below.
#
# | 1| 2| 3| 4| 5
@hirotnk
hirotnk / prim.pl
Created January 14, 2011 21:58
Prim's algorithm with Perl
#!/usr/bin/perl
use strict;
#
# This program calculates MST(minimum spanning tree) using
# Prim's Algorithm
#
#
# Input: the graph below.
# Output: minimum spanning tree
@hirotnk
hirotnk / eightqueen.pro
Created January 14, 2011 21:56
8 Queen solver
% eightqueen.pro
%
nocheck(_,[]).
nocheck(X/Y, [X1/Y1 | Rest]) :- X=\=X1,Y=\=Y1,
abs(Y1-Y) =\= abs(X1-X),
nocheck(X/Y,Rest).
legal([]).
legal([X/Y | Rest]) :-