継承に関係したメカニズム、中でも暫定クラスは、ソフトウェア構築の問題(この本の はじめに示した)解決に大きな役割を果たす。
- 暫定クラス + 表明 ≒ 抽象データ型(に近い)
- 暫定クラスを使ったスタックの例: p638
- スタックの抽象データ型仕様(ADT:第六章)と(実際のプログラムコードが)かなり類似している
-module(exception_test). | |
-export([hoge/1, fuga/1]). | |
%% @doc erlang:error/1を使って例外送出 | |
hoge(_Arg) -> | |
error(badarg). | |
%% @doc erlang:error/2を使って例外送出 | |
fuga(Arg) -> |
-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()]}. |
$ 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(). |
{ | |
"id": 1, | |
"jsonrpc": "2.0", | |
"total": 1, | |
"result": [ | |
{ | |
"id": 1, | |
"avatar": "images/user_1.png", | |
"age": 38, | |
"admin": false, |
-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], |
// 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); |