[erlang-questions] Additions to lists module

Serge Aleynikov <>
Thu Nov 27 00:38:04 CET 2008


That is if you know at compile time that the field your are interested 
in is #myTable.field_name.  What if you need to get the value of the 
field X = 'field_name' in Table = myTable?

Gleb Peregud wrote:
> Hi,
> 
> Isn't #myTable.field_name enough to determine the position of the
> field in record's tuple? Have I understood this correctly?
> 
> BR
> 
> On 11/26/08, Serge Aleynikov <> wrote:
>> Say you have a mnesia table with a structure being record_info(fields,
>> myTable).  The pos/2 function would help you find a position of a given
>> field in the table.
>>
>> Mazen Harake wrote:
>>> What is the idea behind pos?
>>>
>>> Curious because I have never been in a situation where I need to know
>>> the actual position of an element since I always assume that the order
>>> in a list is always undefined. Perhaps I missed something... do you have
>>> a practical example?
>>>
>>> /M
>>>
>>> Serge Aleynikov wrote:
>>>> I'd like to propose addition of these two functions to the lists
>>>> module.   Very often I find the need for this functionality and feel
>>>> that it belongs to the standard library.
>>>>
>>>> Serge
>>>>
>>>>
>>>>
>>>> %% @spec (List::list(), Ele) -> integer()
>>>> %% @doc Returns the position of `Ele' in the `List'. 0 is returned
>>>> %%      when `Ele' is not found.
>>>> %% @end
>>>> pos(List, Ele) ->
>>>>      pos(List, Ele, 1).
>>>> pos([Ele | Tail], Ele, Pos) ->
>>>>      Pos;
>>>> pos([_ | Tail], Ele, Pos) ->
>>>>      pos(Tail, Ele, Pos+1);
>>>> pos([], _Ele, _) ->
>>>>      0.
>>>>
>>>> %% @spec (Pred, Acc, List::list()) -> Acc1
>>>> %%          Pred = (Ele, Acc) -> Acc1
>>>> %% @doc A combination of foldl/3 and takewhile/2. Accumulate the result
>>>> %%      while the predicate function returns true.
>>>> %% @end
>>>> foldlwhile(Pred, Acc0, [H|T]) ->
>>>>      case Pred(H, Acc0) of
>>>>      {true, Acc} ->
>>>>          foldwhile(Fun, Acc, T);
>>>>      {false, Acc} ->
>>>>          Acc;
>>>>      end;
>>>> foldlwhile(_Pred, Acc, []) ->
>>>>      Acc.
>>>> _______________________________________________
>>>> erlang-questions mailing list
>>>> 
>>>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>>>
>>>
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
> 
> 




More information about the erlang-questions mailing list