(apply interleave [[1 2]])

13 views
Skip to first unread message

Eugen Dück

unread,
May 28, 2010, 8:32:57 PM5/28/10
to Clojure
When I do

(apply interleave some-colls)

and some-colls is a sequence/collection of only one sequence/
collection, it will throw:

user=> (apply interleave [[1 2]])
java.lang.IllegalArgumentException: Wrong number of args passed to:
core$interleave (NO_SOURCE_FILE:0)

(Of course I don't need the apply to cause that exception, but calling
interleave directly with just one parameter doesn't make any sense.
But in the case you use apply, having only one sequence in a sequence
is a possible corner case that can arise "at run time")

In order to make interleave more general, I'd like to add a "one param
overload" to interleave like

(defn interleave
"Returns a lazy seq of the first item in each coll, then the second
etc."
([c] (seq c))
...

or even just

(defn interleave
([c] c)

but that would break the contract of interleave, in that it returns
whatever you pass in, which might not be a sequence, as is the case in
my example.

Any thoughts on this?

Eugen

Paul Hobbs

unread,
May 29, 2010, 6:07:15 AM5/29/10
to clo...@googlegroups.com
What code would this make simpler?  Are you constantly having to check this special case?  If not, I don't see a reason to include it.
--
Paul Hobbs


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Michael Gardner

unread,
May 29, 2010, 11:58:13 AM5/29/10
to clo...@googlegroups.com
On May 29, 2010, at 5:07 AM, Paul Hobbs wrote:

> What code would this make simpler? Are you constantly having to check this special case? If not, I don't see a reason to include it.

I haven't run across this particular issue, but I have many times written code that may end up calling a function with "degenerate" arguments in corner cases. I much prefer functions that handle such degenerate arguments gracefully when it makes sense to do so, which it absolutely does for interleave.

Eugen Dück

unread,
May 29, 2010, 6:51:45 PM5/29/10
to Clojure
Paul,

I already gave a minimal example of the code it makes simpler, i.e.
work in the first place:

(apply interleave some-colls)

I ran into this a couple of times, and wrote my own variant of
interleave that handles the one-coll case. I'd rather see this case
handled by interleave.

How often do you do:

(+ 5)

or

(* 3)

? But you might have used something like

(apply + coll)

or

(reduce + coll)

and under certain circumstances your coll might have had only one
element. Still + works just fine and returns a value that makes sense
(it even does if you call it with no argument). I'm basically asking
to get the same case that clojure handles for a lot of other functions
added to "interleave".

Eugen

On May 29, 7:07 pm, Paul Hobbs <Paul_Ho...@hmc.edu> wrote:
> What code would this make simpler?  Are you constantly having to check this

> > (apply interleave some-colls)
> > clojure+u...@googlegroups.com<clojure%2Bunsu...@googlegroups.com>

Paul Hobbs

unread,
May 31, 2010, 4:51:31 AM5/31/10
to clo...@googlegroups.com
Makes sense.
--
Paul Hobbs

Daniel Werner

unread,
May 31, 2010, 4:07:43 PM5/31/10
to Clojure
On May 30, 12:51 am, Eugen Dück <eu...@dueck.org> wrote:
> How often do you do:
> (+ 5)
> or
> (* 3)
>
> ? But you might have used something like
> (apply + coll)
> or
> (reduce + coll)
>
> and under certain circumstances your coll might have had only one
> element.

This is a good line of reasoning. Let's add an example in quantitative
logic, in which corner cases aren't that obvious at first glance:

user=> (every? pos? #{})
true

Clojure follows the mathematical definition here and concludes that
all members of the empty set are indeed positive :-) This is a useful
behaviour since the programmer probably wanted to keep out non-
positive numbers, and the empty set satisfies this restriction. No
need to check for an empty collection beforehand.

Daniel Werner

unread,
May 31, 2010, 4:12:52 PM5/31/10
to Clojure
On May 31, 10:07 pm, Daniel Werner <daniel.d.wer...@googlemail.com>
wrote:
> quantitative logic

That should have been "quantification logic".

Eugen Dück

unread,
Jun 6, 2010, 8:38:40 AM6/6/10
to Clojure
And we could actually also add an no-arg version. My own version of
interleave that I use looks like this:

(defn interleav
([] nil)
([c] (seq c))
([c1 c2] (interleave c1 c2))
([c1 c2 & colls] (apply interleave c1 c2 colls)))

I guess that's as generic as it gets.

Does Rich read all threads and will silently ignore/implement this or
is there any other good way to get his attention without nagging/
spamming him? I remember suggesting some other enhancement earlier
with rather underwhelming success...

On Jun 1, 5:12 am, Daniel Werner <daniel.d.wer...@googlemail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages