Skip to content

Instantly share code, notes, and snippets.

@sile
sile / exception_test.erl
Created May 14, 2014 14:50
Erlangで例外スタックトレースに引数情報を乗せる方法 ref: http://qiita.com/sile/items/2b11cdd0399e20d34dcc
-module(exception_test).
-export([hoge/1, fuga/1]).
%% @doc erlang:error/1を使って例外送出
hoge(_Arg) ->
error(badarg).
%% @doc erlang:error/2を使って例外送出
fuga(Arg) ->
@sile
sile / json_decode_1.erl
Created May 17, 2014 15:45
Erlangコード最適化メモ: JSONデコード処理(1): まず基本形 ref: http://qiita.com/sile/items/6df1400ce45f870b4b03
-module(json_decode_1).
-export([decode/1]).
-type json_value() :: null | boolean() | json_number() |
json_string() | json_array() | json_object().
-type json_number() :: non_neg_integer().
-type json_string() :: binary().
-type json_array() :: [json_value()].
-type json_object() :: {[json_object_member()]}.
@sile
sile / file0.erl
Created May 17, 2014 18:51
Erlangコード最適化メモ: JSONデコード処理(2): HiPEを使う ref: http://qiita.com/sile/items/b7aa22a79ac3785736fa
$ erl
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:2:2] [async-threads:10] [hipe]
Eshell V5.10.4 (abort with ^G)
%% nativeをコンパイルオプションに、指定するとHiPEコンパイラが使われる
> c(hoge, [native]).
{ok, hoge}.
%% hipe:help/0 でヘルプが表示できる
> hipe:help().
@sile
sile / 1x.json
Created May 20, 2014 19:51
Erlangコード最適化メモ: JSONデコード処理(3): JSON文字列パースの効率化 ref: http://qiita.com/sile/items/a29a2ab4124ff3c8ae92
{
"id": 1,
"jsonrpc": "2.0",
"total": 1,
"result": [
{
"id": 1,
"avatar": "images/user_1.png",
"age": 38,
"admin": false,
@sile
sile / oosc-ch14-8-10.md
Last active August 29, 2015 14:02
オブジェクト指向入門 第2版 原則・コンセプト - 第14章

14.8 暫定クラスの役割

継承に関係したメカニズム、中でも暫定クラスは、ソフトウェア構築の問題(この本の はじめに示した)解決に大きな役割を果たす。

14.8.1 抽象データ型に戻る

  • 暫定クラス + 表明 ≒ 抽象データ型(に近い)
  • 暫定クラスを使ったスタックの例: p638
  • スタックの抽象データ型仕様(ADT:第六章)と(実際のプログラムコードが)かなり類似している
@sile
sile / bench.erl
Last active August 29, 2015 14:03
Erlang/OTP 17.1でmapsを使用する場合の注意点 ref: http://qiita.com/sile/items/48f1268d2e03915ca479
-module(bench).
-export([bench/0]).
-spec bench() -> [Result] when
Result :: {module(), ElementSize::pos_integer(), InsertAverageNanoSeconds::non_neg_integer()}.
bench() ->
ElementSizeList = [1, 10, 100, 1000, 10000, 100000],
InputList = [shuffle(lists:seq(0, Size - 1)) || Size <- ElementSizeList],
@sile
sile / fib.cc
Created July 14, 2014 16:27
Fibonacci Number Benchmarks
// g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4)
#include <iostream>
#include <cstdlib>
unsigned fib(unsigned n) {
if (n < 2) {
return n;
} else {
return fib(n - 2) + fib(n - 1);
@sile
sile / oosc-ch11-9-12.md
Last active August 29, 2015 14:04
オブジェクト指向入門 第2版 原則・コンセプト - 第11章

11.9 クラスが正しいのはいつか

11.9.1 クラスの正しさ

  • クラスが正しいかどうかは、クラス自身によるのではなく仕様に基づかなければならない
  • 事前条件、事後条件、不変表明によってクラステキスト事態に、仕様のあたるものを入れ込むことが可能

定義: クラスの正しさ

@sile
sile / oosc-ch15-3-8.md
Last active August 29, 2015 14:05
オブジェクト指向入門 第2版 原則・コンセプト - 第11章

15.3 構造をフラットにする

継承ではいろいろなメカニズムが使用可能:

  • 改名
  • 再定義
  • 未定義化(undefinition)
  • 結合(join)
  • 選択(selection)
  • 子孫の隠蔽
@sile
sile / oosc-ch17-4-11.md
Last active August 29, 2015 14:06
オブジェクト指向入門 第2版 原則・コンセプト - 第17章

17.4 システム妥当性への最初のアプローチ

17.4.1 逆変性と無変性

両方とも使えない:

  • 逆変性: システムの妥当性エラーの理論的な問題はなくなるが非現実的
  • 無変性: 強い型付き言語では(これを採用してしまうと)型システムが使い物にならなくなってしまう
  • C++は無変性だが(型システムを壊し得る)キャストで回避