how to hold lists
Yani Dzhurov
yani.dzhurov@REDACTED
Fri May 12 13:50:16 CEST 2006
Yeah, this is the way it's done now...or some kind of ...
But now I need to add some more values to these enums...
And imagine I add new_Monday at first position in my enum...
Then I have to rewrite all:
day(new_monday)-> {weekday, 1};
day(monday) -> {weekday, 2};
day(tuesday) -> {weekday, 3};
and I need to manage an enum with about 100 values ... and if I need to
change these funs every time a new value is added /a value may be added at
any position, not just the first one.../ .. it would become a hell:)
So I need a better way to manage these enums.
10x,
yani
-----Original Message-----
From: Serge Aleynikov [mailto:serge@REDACTED]
Sent: Friday, May 12, 2006 2:45 PM
To: Yani Dzhurov
Cc: erlang-questions@REDACTED
Subject: Re: how to hold lists
Looks like Thomas' advice was well suited for this problem:
day(monday) -> {weekday, 1};
day(tuesday) -> {weekday, 2};
...
day(saturday) -> {weekend, 6}; % or is it 20 in your case?
day(sunday) -> {weekend, 7}.
is_weekday(D) -> element(1, day(D)) == weekend.
day_of_week(D) -> element(2, day(D)).
This should indeed be faster than working with lists.
Serge
Yani Dzhurov wrote:
> You are right :)
>
> I need these lists to simulate some kind of enumarations.
> I have two big enums /100 fields each/ which I want to split in 5 lists
and
> simulate them as enums in Erlang.
> For instance
> enum WeekDays {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday=20,
> Sunday}
> spilt them to lists
> days1=[monday, tuesday, wednesday, thursday, friday]
> And days2=[saturday, sunday].
> So when I have
> WeekDay 1;
> I can easily get it from the first list;
> list:nth(1,days1).
> If I have WeekDay 22;
> lists:nth(22 - Offset, days2).
>
> Also I need easily to get the value of WeekDays as ints.
> So from thursday -> 3;
> So I would get the index of thursday in the list days1.
>
> This procedure looks pretty stupid in this example.
> But when I have large enums and need to insert new values in it /{Monday,
> Tuesday, Wednesday,SOME_OTHER_WEEK_DAY, Thursday, Friday, Saturday=20,
> Sunday}/ it would be easy to maintain.
>
> If someone else has a better idea, pls advise me:)
>
> 10x,
>
> yani
>
>
> -----Original Message-----
> From: owner-erlang-questions@REDACTED
> [mailto:owner-erlang-questions@REDACTED] On Behalf Of Richard Carlsson
> Sent: Friday, May 12, 2006 1:18 PM
> To: Yani Dzhurov
> Cc: erlang-questions@REDACTED
> Subject: Re: how to hold lists
>
> Yani Dzhurov wrote:
>
>>I need to have some kind of global variables where to hold a couple of
>>lists. Also need them to process them pretty often by thousand of
>
> processes.
>
>>[...]
>>I have about 5-6 lists with about 50 atoms each.
>>
>>Is there any better approach?
>
>
> I think that if you describe in more detail what these lists contain
> and how you want to use them (e.g., what does "process them" mean,
> more exactly?), people will be able to give you much better advice.
>
> /Richard
>
>
>
>
More information about the erlang-questions
mailing list