Skip to content

Instantly share code, notes, and snippets.

@wang-zhijun
Last active March 16, 2016 13:48
Show Gist options
  • Save wang-zhijun/d125d5641b00fc7213ec to your computer and use it in GitHub Desktop.
Save wang-zhijun/d125d5641b00fc7213ec to your computer and use it in GitHub Desktop.
Erlangでフィアルをバイナリコードで開く
-module(open_binary_file).
-compile(export_all).
% read_file(Filename) -> {ok, Binary} | {error, Reason}
% 4> c(open_binary_file).
% {ok,open_binary_file}
% 5> open_binary_file:read_file("prime.erl").
% [<<"#!/usr/bin/env escript">>,<<>>,<<"main(_) ->">>,
% <<" Res = prime_list(1000),">>,
% <<" io:format(\"~p~n\", [length(Res)]).">>,<<>>,<<"prime_list(Num)->">>,
% <<" prime_list(Num, 3, [2]).">>,<<>>,
% <<"prime_list(Num, PP, PS) when PP >= Num ->">>,<<" lists:reverse(PS);">>,
% <<"prime_list(Num, PP, PS) when PP < Num ->">>,
% <<" IsPrime = lists:any(fun(X) -> PP rem X =:= 0 end, PS),">>,
% <<" case IsPrime of">>,<<" true -> prime_list(Num, PP+2, PS);">>,
% <<" false -> prime_list(Num, PP+2, [PP|PS])">>,<<" end.">>,
% <<" ">>,<<>>,<<>>,<<>>,<<>>,<<>>]
% ok
% 6>
read_file(Filename) ->
{ok, Data}= file:read_file(Filename),
L = binary:split(Data, [<<"\n">>], [global]),
Lc = [Item || Item <- L],
io:format("~p~n", [Lc]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment