[erlang-questions] Getting the position of a list item

Robert Raschke rtrlists@REDACTED
Wed Dec 2 19:00:39 CET 2009


On Wed, Dec 2, 2009 at 5:53 PM, Garrett Smith <g@REDACTED> wrote:

> On Wed, Dec 2, 2009 at 11:34 AM, Robert Raschke <rtrlists@REDACTED>
> wrote:
> > On Wed, Dec 2, 2009 at 5:10 PM, Garrett Smith <g@REDACTED> 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


More information about the erlang-questions mailing list