Erlang/OTP Forums

Author Message

<  Erlang questions mailing list  ~  Getting the position of a list item

gar1t
Posted: Wed Dec 02, 2009 5:11 pm Reply with quote
User Joined: 11 Aug 2009 Posts: 55
I'm missing something basic here :\

The lists module has keyfind/3 but no find/2, where you want to return
the position of a particular list item. This is surprising.

Is there a commonly used pattern for this?

Garrett

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Post received from mailinglist
View user's profile Send private message
Guest
Posted: Wed Dec 02, 2009 5:36 pm Reply with quote
Guest
On Wed, Dec 2, 2009 at 5:10 PM, Garrett Smith <g@rre.tt> wrote:

> I'm missing something basic here :\
>
> The lists module has keyfind/3 but no find/2, where you want to return
> the position of a particular list item. This is surprising.
>
> Is there a commonly used pattern for this?
>
>
>
Hmm, I fail to come up with a good usage scenario for wanting this. What are
you needing the position of an item in a list for?

Robby


Post received from mailinglist
Guest
Posted: Wed Dec 02, 2009 5:52 pm Reply with quote
Guest
On Wed, Dec 02, 2009 at 11:10:25AM -0600, Garrett Smith wrote:
> I'm missing something basic here :\
>
> The lists module has keyfind/3 but no find/2, where you want to return
> the position of a particular list item. This is surprising.
>
> Is there a commonly used pattern for this?
>

gen_leader has a compact little function I've 'borrowed' a couple times:

pos(_, []) ->
100000;
pos(N1,[N1|_]) ->
1;
pos(N1,[_|Ns]) ->
1+pos(N1,Ns).

I agree that something like this would be handy to have in the stdlib
(I've also written a version that finds the index of the first element
that a predicate fun returns true for).

Andrew

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Post received from mailinglist
Guest
Posted: Wed Dec 02, 2009 5:54 pm Reply with quote
Guest
I happened to need that once and ended up implementing my own, so I
think you won't find it in the standard library.

Later, I realized there was a more elegant way of doing what I wanted,
so I should agree with Robert in that you probably don't need that
functionality (but we could be wrong, of course...).

find_first(Element, List) when is_list(List) ->
find_first(Element, List, 0).

find_first(_Element, [], Inc) when is_integer(Inc) ->
Inc + 1;
find_first(Element, [Element | _Tail], Inc) when is_integer(Inc) ->
Inc + 1;
find_first(Element, [_ | Tail], Inc) when is_integer(Inc) ->
find_first(Element, Tail, Inc + 1).


Igor.

On Wed, Dec 2, 2009 at 3:10 PM, Garrett Smith <g@rre.tt> wrote:
> I'm missing something basic here :\
>
> The lists module has keyfind/3 but no find/2, where you want to return
> the position of a particular list item. This is surprising.
>
> Is there a commonly used pattern for this?
>
> Garrett

--
"The secret of joy in work is contained in one word - excellence. To
know how to do something well is to enjoy it." - Pearl S. Buck.

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Post received from mailinglist
gar1t
Posted: Wed Dec 02, 2009 5:57 pm Reply with quote
User Joined: 11 Aug 2009 Posts: 55
On Wed, Dec 2, 2009 at 11:34 AM, Robert Raschke <rtrlists@googlemail.com> wrote:
> On Wed, Dec 2, 2009 at 5:10 PM, Garrett Smith <g@rre.tt> wrote:
>
>> I'm missing something basic here :\
>>
>> The lists module has keyfind/3 but no find/2, where you want to return
>> the position of a particular list item. This is surprising.
>>
>> Is there a commonly used pattern for this?
>>
> Hmm, I fail to come up with a good usage scenario for wanting this. What are
> you needing the position of an item in a list for?

I'm stitching together record field values and want to get an
insertion position using a field token.

So, this goes in hard with an "insert_at(Value, N, List_Or_Tuple)"
type of function.

In general though, there are several functions in the lists module
that accept a positional argument (N) -- having some easy ways of
getting N seems reasonable. Functions like l/find and rfind are pretty
common in the list APIs I'm familiar with.

Garrett

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Post received from mailinglist
View user's profile Send private message
gar1t
Posted: Wed Dec 02, 2009 6:02 pm Reply with quote
User Joined: 11 Aug 2009 Posts: 55
On Wed, Dec 2, 2009 at 11:53 AM, Igor Ribeiro Sucupira <igorrs@gmail.com> wrote:
> I happened to need that once and ended up implementing my own, so I
> think you won't find it in the standard library.
>
> Later, I realized there was a more elegant way of doing what I wanted,
> so I should agree with Robert in that you probably don't need that
> functionality (but we could be wrong, of course...).
>
> find_first(Element, List) when is_list(List) ->
>
View user's profile Send private message
Guest
Posted: Wed Dec 02, 2009 6:05 pm Reply with quote
Guest
On Wed, Dec 2, 2009 at 5:53 PM, Garrett Smith <g@rre.tt> wrote:

> On Wed, Dec 2, 2009 at 11:34 AM, Robert Raschke <rtrlists@googlemail.com>
> wrote:
> > On Wed, Dec 2, 2009 at 5:10 PM, Garrett Smith <g@rre.tt> wrote:
> >
> >> I'm missing something basic here :\
> >>
> >> The lists module has keyfind/3 but no find/2, where you want to return
> >> the position of a particular list item. This is surprising.
> >>
> >> Is there a commonly used pattern for this?
> >>
> > Hmm, I fail to come up with a good usage scenario for wanting this. What
> are
> > you needing the position of an item in a list for?
>
> I'm stitching together record field values and want to get an
> insertion position using a field token.
>
> So, this goes in hard with an "insert_at(Value, N, List_Or_Tuple)"
> type of function.
>
> In general though, there are several functions in the lists module
> that accept a positional argument (N) -- having some easy ways of
> getting N seems reasonable. Functions like l/find and rfind are pretty
> common in the list APIs I'm familiar with.
>
> Garrett
>

Not 100% sure what it is exactly you're doing, but it sounds like it could
be a bit easier using tuple_to_list/1 and list comprehensions?

Robby


Post received from mailinglist
Guest
Posted: Wed Dec 02, 2009 6:12 pm Reply with quote
Guest
Robert Raschke wrote:
> On Wed, Dec 2, 2009 at 5:10 PM, Garrett Smith <g@rre.tt> wrote:
>
>
>> I'm missing something basic here :\
>>
>> The lists module has keyfind/3 but no find/2, where you want to return
>> the position of a particular list item. This is surprising.
>>
>> Is there a commonly used pattern for this?
>>
>>
>>
>>
> Hmm, I fail to come up with a good usage scenario for wanting this. What are
> you needing the position of an item in a list for?
>
> Robby
>
>
Find a good usage for lists:nth/2 and lists:nthtail/2 functions, and you
will find a good one for this Smile.


________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Post received from mailinglist
Guest
Posted: Wed Dec 02, 2009 6:31 pm Reply with quote
Guest
2009/12/2, Garrett Smith <g@rre.tt>:
> I'm missing something basic here :\
>
> The lists module has keyfind/3 but no find/2, where you want to return
> the position of a particular list item. This is surprising.
>
> Is there a commonly used pattern for this?

string:str(List, [Element]).

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Post received from mailinglist
Guest
Posted: Wed Dec 02, 2009 6:46 pm Reply with quote
Guest
2009/12/2, Garrett Smith <g@rre.tt>:
[...]
> But if a 20 year old language can get by without these, then by
> definition *I'm* wrong Smile

Well, actually I think in this case Erlang is wrong. There was a
thread here a couple of months ago that some (most?) libraries on OTP
were developed more in an "evolutionary" way, not "intelligently
designed". For example the string module has substr and sub_string,
because the module was merged from two previous string modules. Lookup
functions have different interfaces:

ets:lookup(Table, Key) -> [Objects]
dict:find(Key, Dict) -> {ok, Value} | error
gb_tree:lookup(Key, Tree) -> {value, Val} | none
lists:keysearch(Key, N, TupleList) -> {value, Tuple} | false
proplists:lookup(Key, List) -> none | tuple()

Our local proplist:lookup-clone function returns no_value where
proplists would return none. Expect the unexpected, when you work with
Erlang libraries.

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Post received from mailinglist
Guest
Posted: Wed Dec 02, 2009 7:45 pm Reply with quote
Guest
> Hmm, I fail to come up with a good usage scenario for wanting this. What are
> you needing the position of an item in a list for?

FWIW, here's something i wrote recently:

base32_decode(Char) ->
list_index(Char, base32_alphabet()).

base32_alphabet() ->
"0123456789bcdefghjkmnpqrstuvwxyz".


The corresponding encode function uses lists:nth/2.

Can't remember ever needing it before though (in erlang).

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Post received from mailinglist
Guest
Posted: Thu Dec 03, 2009 1:00 am Reply with quote
Guest
Hum... when you talked about records, I realized I still have one
function that needs to find the position of an item on a list. The
function is used to get the value of a field F on a record, when F is
defined at runtime.

I don't understand exactly what you need, but I guess it's basically
the same thing, isn't it?

On Wed, Dec 2, 2009 at 3:53 PM, Garrett Smith <g@rre.tt> wrote:
> On Wed, Dec 2, 2009 at 11:34 AM, Robert Raschke <rtrlists@googlemail.com> wrote:
>> On Wed, Dec 2, 2009 at 5:10 PM, Garrett Smith <g@rre.tt> wrote:
>>
>>> I'm missing something basic here :\
>>>
>>> The lists module has keyfind/3 but no find/2, where you want to return
>>> the position of a particular list item. This is surprising.
>>>
>>> Is there a commonly used pattern for this?
>>>
>> Hmm, I fail to come up with a good usage scenario for wanting this. What are
>> you needing the position of an item in a list for?
>
> I'm stitching together record field values and want to get an
> insertion position using a field token.
>
> So, this goes in hard with an "insert_at(Value, N, List_Or_Tuple)"
> type of function.
>
> In general though, there are several functions in the lists module
> that accept a positional argument (N) -- having some easy ways of
> getting N seems reasonable. Functions like l/find and rfind are pretty
> common in the list APIs I'm familiar with.
>
> Garrett
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>



--
"The secret of joy in work is contained in one word - excellence. To
know how to do something well is to enjoy it." - Pearl S. Buck.

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Post received from mailinglist
Guest
Posted: Thu Dec 03, 2009 1:18 am Reply with quote
Guest
By the way, this is the function (with some details like module names
removed/changed):

field_value(R, F) when is_record(R, test), is_atom(F) ->
P = find_first(F, record_info(fields, test)),
lists:nth(P + 1, tuple_to_list(R)).

I don't think there would be something easier without the function to
get the position of the list item (find_first, in my example).

Igor.

On Wed, Dec 2, 2009 at 10:58 PM, Igor Ribeiro Sucupira <igorrs@gmail.com> wrote:
> Hum... when you talked about records, I realized I still have one
> function that needs to find the position of an item on a list. The
> function is used to get the value of a field F on a record, when F is
> defined at runtime.
>
> I don't understand exactly what you need, but I guess it's basically
> the same thing, isn't it?
>
> On Wed, Dec 2, 2009 at 3:53 PM, Garrett Smith <g@rre.tt> wrote:
>> On Wed, Dec 2, 2009 at 11:34 AM, Robert Raschke <rtrlists@googlemail.com> wrote:
>>> On Wed, Dec 2, 2009 at 5:10 PM, Garrett Smith <g@rre.tt> wrote:
>>>
>>>> I'm missing something basic here :\
>>>>
>>>> The lists module has keyfind/3 but no find/2, where you want to return
>>>> the position of a particular list item. This is surprising.
>>>>
>>>> Is there a commonly used pattern for this?
>>>>
>>> Hmm, I fail to come up with a good usage scenario for wanting this. What are
>>> you needing the position of an item in a list for?
>>
>> I'm stitching together record field values and want to get an
>> insertion position using a field token.
>>
>> So, this goes in hard with an "insert_at(Value, N, List_Or_Tuple)"
>> type of function.
>>
>> In general though, there are several functions in the lists module
>> that accept a positional argument (N) -- having some easy ways of
>> getting N seems reasonable. Functions like l/find and rfind are pretty
>> common in the list APIs I'm familiar with.
>>
>> Garrett
>>
>> ________________________________________________________________
>> erlang-questions mailing list. See http://www.erlang.org/faq.html
>> erlang-questions (at) erlang.org
>>
>>
>
>
>
> --
> "The secret of joy in work is contained in one word - excellence. To
> know how to do something well is to enjoy it." - Pearl S. Buck.
>



--
"The secret of joy in work is contained in one word - excellence. To
know how to do something well is to enjoy it." - Pearl S. Buck.

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Post received from mailinglist
ngocdaothanh
Posted: Thu Dec 03, 2009 1:24 am Reply with quote
User Joined: 06 Dec 2008 Posts: 43
> string:str(List, [Element]).

What a surprise! Thank you.

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Post received from mailinglist
View user's profile Send private message Send e-mail
gar1t
Posted: Thu Dec 03, 2009 1:52 am Reply with quote
User Joined: 11 Aug 2009 Posts: 55
On Wed, Dec 2, 2009 at 7:23 PM, Ngoc Dao <ngocdaothanh@gmail.com> wrote:
>> string:str(List, [Element]).
>
> What a surprise! Thank you.

A good example of "most astonishment" Smile

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Post received from mailinglist
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 2
Goto page 1, 2  Next
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum