Re: Function Design: sequence or argument?

2010-11-19 Thread Jarl Haggerty
I always write a function to take varargs because it can also take a
list using apply.

(+ 1 2 3 4 5)
(apply + [1 2 3 4 5])

On Nov 15, 9:52 am, Chris christopher.ma...@gmail.com wrote:
 If you have a function that needs to treat multiple arguments as a
 group, what forces drive you to represent this as a single sequence
 argument vs. an  argument?  To give a concrete example, why does
 + work like

 (+ 1 2 3 4)

 instead of

 (+ [1 2 3 4])

 Is it performance?  Aesthetics?  Composability concerns?  Not having
 to call apply all the time?

 Thanks,
 Chris

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Function Design: sequence or argument?

2010-11-19 Thread Alan
I always write a function to take a single seq argument because it can
also take varargs if I wrap them in a seq.

(defn add [nums]
  (reduce + nums))

(add some-seq)
(add [1 2 3 4 5])

On Nov 19, 4:19 pm, Jarl Haggerty fictivela...@gmail.com wrote:
 I always write a function to take varargs because it can also take a
 list using apply.

 (+ 1 2 3 4 5)
 (apply + [1 2 3 4 5])

 On Nov 15, 9:52 am, Chris christopher.ma...@gmail.com wrote:

  If you have a function that needs to treat multiple arguments as a
  group, what forces drive you to represent this as a single sequence
  argument vs. an  argument?  To give a concrete example, why does
  + work like

  (+ 1 2 3 4)

  instead of

  (+ [1 2 3 4])

  Is it performance?  Aesthetics?  Composability concerns?  Not having
  to call apply all the time?

  Thanks,
  Chris



-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Function Design: sequence or argument?

2010-11-19 Thread Eric Schulte
I generally prefer to pass in a sequence rather than use a variable
number of arguments.  The only time variable arguments are really useful
is in functions like map (or maybe +) in which you rarely use more than
one (or two) arguments and it would be a pain to wrap the last argument
in a list.

e.g.
#+begin_src clojure
  (map (partial + 1) (list (range 10)))
#+end_src
would be very awkward but required without rest

finally, explicitly wrapped sequences are more composable

just my opinions... -- Eric

Alan a...@malloys.org writes:

 I always write a function to take a single seq argument because it can
 also take varargs if I wrap them in a seq.

 (defn add [nums]
   (reduce + nums))

 (add some-seq)
 (add [1 2 3 4 5])

 On Nov 19, 4:19 pm, Jarl Haggerty fictivela...@gmail.com wrote:
 I always write a function to take varargs because it can also take a
 list using apply.

 (+ 1 2 3 4 5)
 (apply + [1 2 3 4 5])

 On Nov 15, 9:52 am, Chris christopher.ma...@gmail.com wrote:

  If you have a function that needs to treat multiple arguments as a
  group, what forces drive you to represent this as a single sequence
  argument vs. an  argument?  To give a concrete example, why does
  + work like

  (+ 1 2 3 4)

  instead of

  (+ [1 2 3 4])

  Is it performance?  Aesthetics?  Composability concerns?  Not having
  to call apply all the time?

  Thanks,
  Chris



-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Function Design: sequence or argument?

2010-11-16 Thread Chris Maier
That makes sense... thanks, Meikel.

Maybe my example of + wasn't the best, given its mathematical nature.

Here's my situation: I'm writing some software to analyze some protein
datasets, part of which entails generating a Venn diagram of their
intersections.  Each dataset has a unique name, and my function looks
up the data by this name in a database.  Right now the function takes
a single  parameter of datasets; you give it as many dataset
names as you want, and it gives you the intersection breakdown.
Internally, this function just treats datasets like the sequence it
is.

So I guess a better way to state my question would be this: if you
only have a function that operates on a sequence of items, and it
could be written either as

(def foo [ stuff] ... )

or

(def foo [stuff] ... ) ;; where 'stuff' is a sequence

are there any concerns besides (for lack of a better term) the
ergonomics of calling such functions that should influence which
signature to use?

Does that make sense?

Thanks,
Chris

On Mon, Nov 15, 2010 at 2:08 PM, Meikel Brandmeyer m...@kotka.de wrote:
 Hi,

 Am 15.11.2010 um 17:52 schrieb Chris:

 If you have a function that needs to treat multiple arguments as a
 group, what forces drive you to represent this as a single sequence
 argument vs. an  argument?  To give a concrete example, why does
 + work like

 (+ 1 2 3 4)

 instead of

 (+ [1 2 3 4])

 Is it performance?  Aesthetics?  Composability concerns?  Not having
 to call apply all the time?

 Semantics?

 + is a binary operator. Allowing more arguments is a convenience I guess. The 
 mathematically honest definition would have arity 2.

 Eg. filter on the other hand acts on a sequence. That's why it is not defined 
 as (filter even? 1 2 3 4). (See also map for example where apply wouldn't 
 help.)

 Does this make sense?

 Sincerely
 Meikel

 --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with your 
 first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Function Design: sequence or argument?

2010-11-16 Thread Christophe Grand
Hi,

On Tue, Nov 16, 2010 at 5:21 PM, Chris Maier christopher.ma...@gmail.comwrote:

 That makes sense... thanks, Meikel.

 Maybe my example of + wasn't the best, given its mathematical nature.

 Here's my situation: I'm writing some software to analyze some protein
 datasets, part of which entails generating a Venn diagram of their
 intersections.  Each dataset has a unique name, and my function looks
 up the data by this name in a database.  Right now the function takes
 a single  parameter of datasets; you give it as many dataset
 names as you want, and it gives you the intersection breakdown.
 Internally, this function just treats datasets like the sequence it
 is.

 So I guess a better way to state my question would be this: if you
 only have a function that operates on a sequence of items, and it
 could be written either as

 (def foo [ stuff] ... )

 or

 (def foo [stuff] ... ) ;; where 'stuff' is a sequence

 are there any concerns besides (for lack of a better term) the
 ergonomics of calling such functions that should influence which
 signature to use?


If you really want to treat your sequence lazily pass it explicitely and not
as varargs.

You can also provide both: clojure.core has #'conj and #'into for example.

hth,

  Christophe

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Function Design: sequence or argument?

2010-11-16 Thread Vagif Verdi
On Nov 15, 8:52 am, Chris christopher.ma...@gmail.com wrote:
 If you have a function that needs to treat multiple arguments as a
 group, what forces drive you to represent this as a single sequence
 argument vs. an  argument?  To give a concrete example, why does
 + work like

 (+ 1 2 3 4)

 instead of

 (+ [1 2 3 4])

 Is it performance?  Aesthetics?  Composability concerns?  Not having
 to call apply all the time?

 Thanks,
 Chris

The  form is a syntax helper. In other words it is intended to
change the source code, not to change the way you program. Thus you
have to look at the intended use of the function.
If you are going to type different number arguments to a function
right in the code yourself, then use . If you are going to get the
arguments for that function at runtime, then use a single argument.
Examples: map, filter, reduce.

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Function Design: sequence or argument?

2010-11-15 Thread Ken Wesson
On Mon, Nov 15, 2010 at 11:52 AM, Chris christopher.ma...@gmail.com wrote:
 If you have a function that needs to treat multiple arguments as a
 group, what forces drive you to represent this as a single sequence
 argument vs. an  argument?  To give a concrete example, why does
 + work like

 (+ 1 2 3 4)

 instead of

 (+ [1 2 3 4])

 Is it performance?  Aesthetics?  Composability concerns?  Not having
 to call apply all the time?

Not having to use extra brackets all the time gets my vote. Yeah,
yeah, I know, a strange statement coming from a lisp-head.

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Function Design: sequence or argument?

2010-11-15 Thread Meikel Brandmeyer
Hi,

Am 15.11.2010 um 17:52 schrieb Chris:

 If you have a function that needs to treat multiple arguments as a
 group, what forces drive you to represent this as a single sequence
 argument vs. an  argument?  To give a concrete example, why does
 + work like
 
 (+ 1 2 3 4)
 
 instead of
 
 (+ [1 2 3 4])
 
 Is it performance?  Aesthetics?  Composability concerns?  Not having
 to call apply all the time?

Semantics?

+ is a binary operator. Allowing more arguments is a convenience I guess. The 
mathematically honest definition would have arity 2.

Eg. filter on the other hand acts on a sequence. That's why it is not defined 
as (filter even? 1 2 3 4). (See also map for example where apply wouldn't help.)

Does this make sense?

Sincerely
Meikel

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Function Design: sequence or argument?

2010-11-15 Thread Alyssa Kwan
Performance is part of it too.  Allowing dispatch on arity leads to
faster code.

Many of the functions that operate on sequences are lazy so dispatch
on arity doesn't apply.

On Nov 15, 11:52 am, Chris christopher.ma...@gmail.com wrote:
 If you have a function that needs to treat multiple arguments as a
 group, what forces drive you to represent this as a single sequence
 argument vs. an  argument?  To give a concrete example, why does
 + work like

 (+ 1 2 3 4)

 instead of

 (+ [1 2 3 4])

 Is it performance?  Aesthetics?  Composability concerns?  Not having
 to call apply all the time?

 Thanks,
 Chris

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en