aboutsummaryrefslogtreecommitdiffstats
path: root/lib/compiler/test/pmod_SUITE_data/pmod_basic.erl
blob: 19cce452dc1b791ea6ca535a4cecb365f0ee12dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2004-2011. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
-module(pmod_basic, [Props]).

-export([lookup/1,or_props/1,prepend/1,append/1,stupid_sum/0]).
-export([bar/1,bar_bar/1]).
-export([bc1/0, bc2/0]).
-export([collapse/1]).

lookup(Key) ->
    proplists:lookup(Key, Props).

or_props(Keys) ->    
    Res = or_props_1(Keys, false),
    true = is_bool(Res),			%is_bool/1 does not use Props.
    Res.

prepend(Term) ->
    new([Term|Props]).

append(Term) ->
    pmod_basic:new(Props++[Term]).

or_props_1([K|Ks], Acc) ->
    or_props_1(Ks, proplists:get_bool(K, Props) or Acc);
or_props_1([], Acc) -> Acc.

is_bool(true) -> true;
is_bool(false) -> true;
is_bool(_) -> false.

stupid_sum() ->
    put(counter, 0),
    Res = stupid_sum_1(Props, 0),
    {Res,get(counter)}.

stupid_sum_1([H|T], Sum0) ->
    try add(Sum0, H) of
	Sum -> stupid_sum_1(T, Sum)
    catch
	error:_ -> stupid_sum_1(T, Sum0)
    after
	bump()
    end;
stupid_sum_1([], Sum) -> Sum.

bump() ->
    put(counter, get(counter)+1).

add(A, B) ->
    A+B.

-record(s, {a}).

bar(S) when S#s.a == 0 -> ok.

bar_bar(S) when is_record(S, s) -> ok;
bar_bar(_) -> error.

bc1() ->
    [A || <<A:1>> <= <<"9">> ].

bc2() ->
    << <<A:1>> || A <- [1,0,1,0] >>.

collapse(L) ->
    lists:keymap(fun lists:sort/1, 2, L).