indexing only some elements of a sequence...

1 view
Skip to first unread message

Douglas Philips

unread,
Mar 20, 2010, 1:13:38 AM3/20/10
to clo...@googlegroups.com
Hello all,
I'm new to clojure, but not lisp.
I'm looking for a functional way to index/number only some items
of a list.

For example, I know I can do this (indexed is from the contrib
seq_utils library):
(using a short example to
keep it readable)
(indexed "Now is")
-> ([0 \N] [1 \o] [2 \w] [3 \space] [4 \i] [5 \s])

What I would like to do is only index those elements that satisfy
some predicate, such as:
(indexed-pred vowel? "Now is")
-> ([\N] [0 \o] [\w] [\space] [1 \i] [\s])

I want the counter to only increment when the predicate is true,
so just filtering out the index on a fully indexed list isn't what I
need, it will leave holes in the numbering.


I'd be OK with the return result being: ([nil \N] [0 \o] [nil \w]
[nil \space] [1 \i] [nil \s])
since a simple map could strip the nils out.

I can't help feeling that the solution is just on the edge of my
peripheral vision, but I can't see it.

Thanks,
-Doug


P.S. I want to use this to number the instructions in a road rally,
where the sequence of instructions are interspersed with other notes
and items. Numbering the vowels in a string is a nice simplification.

Per Vognsen

unread,
Mar 20, 2010, 9:54:52 AM3/20/10
to clo...@googlegroups.com
Learn to love scan: http://gist.github.com/338682

-Per

> --
> 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
>
> To unsubscribe from this group, send email to
> clojure+unsubscribegooglegroups.com or reply to this email with the words
> "REMOVE ME" as the subject.
>

Steve Purcell

unread,
Mar 20, 2010, 10:40:12 AM3/20/10
to clo...@googlegroups.com
Which looks the same as clojure.contrib.seq/reductions to me...

-Steve

Per Vognsen

unread,
Mar 20, 2010, 10:50:12 AM3/20/10
to clo...@googlegroups.com
Aha! I Googled for scan in seq-utils and didn't find anything. It
would be nice if people stuck to standard terminology that has a
continuous history going back to the early 60s.

-Per

Meikel Brandmeyer

unread,
Mar 20, 2010, 11:16:02 AM3/20/10
to clo...@googlegroups.com
Hi,

On Sat, Mar 20, 2010 at 09:50:12PM +0700, Per Vognsen wrote:
> Aha! I Googled for scan in seq-utils and didn't find anything. It
> would be nice if people stuck to standard terminology that has a
> continuous history going back to the early 60s.

So we use names like car and cdr? This is one of my favorite
non-technical points pro clojure: It blows of the musty smell of the
last 50 years.

(Whether scan falls in this category is a different question. But "the
others use bad names, so we should too" is not a good argument.)

Sincerely
Meikel

Per Vognsen

unread,
Mar 20, 2010, 11:22:01 AM3/20/10
to clo...@googlegroups.com
Yeah, I was being a bit too glib. One of my favorite things about
Clojure is definitely what you mention.

As for the matter at hand, the name 'reductions' is perhaps more
descriptive but the con is that it less standard and almost three
times as long as 'scan'. The importance of descriptiveness in names is
often overstated. Someone who has never seen this function before
might perhaps see the name and correctly guess a relationship of some
sort with 'reduce'. But that does not help him much in his quest to
understanding the code if he has no experience with scans. I find
recognizability and memorability is usually a more important metric.
Anyway, I don't want to derail this thread any further. :)

-Per

Per Vognsen

unread,
Mar 20, 2010, 11:43:15 AM3/20/10
to clo...@googlegroups.com
One last thing:

On Sat, Mar 20, 2010 at 10:16 PM, Meikel Brandmeyer <m...@kotka.de> wrote:

> Hi,
>
> On Sat, Mar 20, 2010 at 09:50:12PM +0700, Per Vognsen wrote:
>> Aha! I Googled for scan in seq-utils and didn't find anything. It
>> would be nice if people stuck to standard terminology that has a
>> continuous history going back to the early 60s.
>
> So we use names like car and cdr?

Perhaps if we were still programming with cons cells as fundamental
building blocks. Cons cells are decidedly not linked list nodes but
binary tree nodes that can be used to build right-leaning cons lists,
left-leaning snoc lists, a-lists and anything in between. In that
hypothetical case, yes, maybe car and cdr would not be so bad,
everything considered. They are short, symmetric (3 characters, one
middle character difference) and memorable once learned. At this point
in time, rejecting them based on their half-forgotten origins in a
computer architecture of yore is a little like rejecting a name on the
basis of its Greek etymology. Every name is a more or less
conventional sign. Convention isn't everything but neither is it
nothing.

For a sequence abstraction though, first and rest are infinitely
better choices. Fortunately that is what Clojure uses them for. In
Common Lisp, they are mere synonyms for car and cdr, though with
admittedly useful connotations.

-Per

Douglas Philips

unread,
Mar 20, 2010, 7:07:29 PM3/20/10
to clo...@googlegroups.com
On 2010 Mar 20, at 10:40 AM, Steve Purcell wrote:

> Which looks the same as clojure.contrib.seq/reductions to me...

> On 20 Mar 2010, at 13:54, Per Vognsen wrote:


>
>> Learn to love scan: http://gist.github.com/338682

Thanks Per and Steve! I knew there was catch that I wasn't seeing.
I did see reductions in seq-util (I am using 1.1), but it didn't click.

Not going to get in the middle of what names should be what.

-Doug

Reply all
Reply to author
Forward
0 new messages