List comprehension or lists:foldl/3 ?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

List comprehension or lists:foldl/3 ?

Shawn Pearce
Which is the more preferred form amongst you hacker types?

        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'm not concerned about the speed here, as its a startup routine
run once when the node boots.  Even for a very large cluster of
over 200+ nodes (highly unlikely in my application) this would take
no time either way.  This is a preferred syntax poll.

I find the LC easier to read by far.

--
Shawn.

  "... all the modern inconveniences ..."
  -- Mark Twain


Reply | Threaded
Open this post in threaded view
|

List comprehension or lists:foldl/3 ?

Chris Pressey
On Fri, 13 Feb 2004 00:56:10 -0500
Shawn Pearce <spearce> wrote:

> Which is the more preferred form amongst you hacker types?
>
> 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'm not concerned about the speed here, as its a startup routine
> run once when the node boots.  Even for a very large cluster of
> over 200+ nodes (highly unlikely in my application) this would take
> no time either way.  This is a preferred syntax poll.
>
> I find the LC easier to read by far.
>
> --
> Shawn.

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.

-Chris


Reply | Threaded
Open this post in threaded view
|

List comprehension or lists:foldl/3 ?

Thomas Arts-3
Hi Shawn

I was just going to write basically the same as Christ wrote.
You are not interested in the result of the monitor calls. Hence,
why would you construct a list in memory, that is garbage
directly after the call?

>From your question I also deduced that you see foldl and
list-comprehension as similar concepts. However, a
list-comprehension is more comparable to a map or
better a map with a filter application. Fold over a list
could well result in something completely different than
a list (e.g. a number).

The lists:foreach/2 function is like a map when you
are only interested in the side-effects. I prefer that
one in this case.

/Thomas

---
Dr Thomas Arts
     Program Manager
     Software Engineering and Management
IT-university in Gothenburg
Box 8718, 402 75 Gothenburg, Sweden
http://www.ituniv.se/

Tel +46 31 772 6031
Fax +46 31 772 4899


----- Original Message -----
From: "Chris Pressey" <cpressey>
To: "Shawn Pearce" <spearce>
Cc: <erlang-questions>
Sent: Friday, February 13, 2004 7:31 AM
Subject: Re: List comprehension or lists:foldl/3 ?


> On Fri, 13 Feb 2004 00:56:10 -0500
> Shawn Pearce <spearce> wrote:
>
> > Which is the more preferred form amongst you hacker types?
> >
> > 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'm not concerned about the speed here, as its a startup routine
> > run once when the node boots.  Even for a very large cluster of
> > over 200+ nodes (highly unlikely in my application) this would take
> > no time either way.  This is a preferred syntax poll.
> >
> > I find the LC easier to read by far.
> >
> > --
> > Shawn.
>
> 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.
>
> -Chris
>


Reply | Threaded
Open this post in threaded view
|

List comprehension or lists:foldl/3 ?

Shawn Pearce
In reply to this post by Chris Pressey
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> wrote:

> On Fri, 13 Feb 2004 00:56:10 -0500
> Shawn Pearce <spearce> 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