List comprehension or lists:foldl/3 ?

Shawn Pearce spearce@REDACTED
Fri Feb 13 15:47:30 CET 2004


Doh!  I need to study my lists module API a little bit more often. I
totally forgot that lists:foreach/2 exists, if I had remembered it
was there I would have used that as my second case, rather than
lists:foldl/3.

I think Chris makes the best argument, lists:foreach/2 drives home the
"I don't care about return values, just the side effect".  While an
LC is short and sweet, it does leave one scratching their head and
saying "what about the returned references?  why isn't something done
with them?".

I'm going to go through my code now and replace all LCs where I don't
care about return values with lists:foreach.  It really makes my
intentions a little bit more clear.

Thanks for the comments, they were very much appreciated.

Chris Pressey <cpressey@REDACTED> wrote:
> On Fri, 13 Feb 2004 00:56:10 -0500
> Shawn Pearce <spearce@REDACTED> wrote:
> > 
> > 	NodeList = [...],
> > 	[erlang:monitor(process, {myserv, N}) || N <- NodeList],
> > 	ok
> > 
> > or
> > 
> > 	NodeList = [...],
> > 	lists:foldl(
> > 		fun(N, _) -> erlang:monitor(process, {myserv, N}) end,
> > 		0,
> > 		NodeList
> > 	),
> > 	ok
>
> I would probably use lists:foreach/2 instead just to drive home the fact
> that you're evaluating this for its side effects & don't care about the
> return value.  But that's just me.

-- 
Shawn.

  Son, someday a man is going to walk up to you with a deck of cards on which
  the seal is not yet broken.  And he is going to offer to bet you that he can
  make the Ace of Spades jump out of the deck and squirt cider in your ears.
  But son, do not bet this man, for you will end up with a ear full of cider.
  		-- Sky Masterson's Father



More information about the erlang-questions mailing list