[erlang-questions] How would you do 100 ifs?

David Mercer <>
Tue Nov 15 18:00:44 CET 2011


Is this what you want?

 

3> Index = fun(X) -> Ranges = [200, 600, 1000, infinity],
catch(lists:foldl(fun(R, N) when X < R -> throw(N); (_, N) -> N + 1 end, 1,
Ranges)) end.

#Fun<erl_eval.6.80247286>

4> Index(100).

1

5> Index(200).

2

6> Index(300).

2

7> Index(900).

3

8> Index(1000).

4

9> Index(111000).

4

 

From: 
[mailto:] On Behalf Of Max Bourinov
Sent: Tuesday, November 15, 2011 7:27 AM
To: 
Subject: [erlang-questions] How would you do 100 ifs?

 

Hi guys,

 

I have a value X which is integer, and I have a list of ranges
[0....200),[200....600),[600...1000)....etc....[100000, infinity] (this is
just an example). We can assume that the list is static.

 

I have frequently check the index of the range X belongs to.

 

How would you implement it in Erlang?




Best regards,

Max

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111115/209b39c2/attachment.html>


More information about the erlang-questions mailing list