Mike or mikera is fine :-)
If you wanted to provide this kind of functionality, I'd normally suggest
doing it with a protocol. Something like:
(defprotocol PWrappedIndex
(get-wrapped [coll index]))
(extend-protocol PWrappedIndex
clojure.lang.Indexed
(get-wrapped [coll index] (nth coll (mod index (count coll)))))
(get-wrapped [0 1 2 3 4 5 6 7 8 9] -2)
=> 8
This is incidentally the same technique that core.matrix uses to extend a
common matrix API to arbitrary matrix types.
On Thursday, 27 June 2013 23:00:48 UTC+1, Greg Slepak wrote:
>
> Thanks Mike (or do you go by "Mikera" as your email alias suggests?),
>
> I think you make very good points, so I withdraw my request.
>
> I'm curious though... Just as a learning experience, would it be possible
> to "tack on" such syntax implicit indexing and slicing using Clojure's
> extend-type function?
>
> Thanks,
> Greg
>
> On Jun 27, 2013, at 6:45 AM, Mikera <[email protected] <javascript:>>
> wrote:
>
> I agree that negative indexing (and presumably also modulo indexing for
> the upper index?) is very useful. Stuff like this comes up all the time in
> core.matrix
>
> However I don't think it makes sense as a standard feature in Clojure's
> low-level data constructs for several reasons:
> a) It's a breaking change for people who are handling out-of-bounds cases
> b) In new code it will mask out-of-bounds errors, which can conceal some
> nasty bugs
> c) Cramming more implicit features into existing constructs is a bad idea
> in general: explicit is better
> d) It probably adds a small performance overhead. Not worth making
> everyone pay a cost for one special case feature
>
> This kind of thing is IMHO better handled by either wrapping the vector in
> a function, or creating some kind of extra collection wrapper that provides
> the negative indexing functionality.
>
> On Thursday, 27 June 2013 04:19:24 UTC+1, Michael-Keith Bernard
> (SegFaultAX) wrote:
>
>> Vectors and maps are already functions of their indices and keys,
>> respectively. I don't really think it makes sense for other sequence types
>> (seqs, lists, etc.) because they aren't naturally associative in the same
>> way. Finally, there isn't a Clojure form I'm aware of that allows negative
>> indices in the same way eg Python does, but I for one would find that
>> incredibly useful.
>>
>
>> On Wednesday, June 26, 2013 8:01:02 PM UTC-7, Greg Slepak wrote:
>>>
>>> There is one feature that I really miss from newLISP and seems like it
>>> could be a natural extension to Clojure, and that is implicit indexing for
>>> lists and arrays.
>>>
>>> Clojure already has something similar in its use of keywords to act as
>>> functions that look themselves up in a map.
>>>
>>> This is basically the same concept, but using numbers instead. Implicit
>>> indexing creates a really elegant syntax for finding elements and ranges in
>>> a list or array. Here's an example:
>>>
>>> > (setf mylist '(a b c d e f g))
>>> (a b c d e f g)
>>> > (mylist 0)
>>> a
>>> > (mylist -1)
>>> g
>>> > (0 3 mylist)
>>> (a b c)
>>>
>>>
>>> Has this been considered already? Would this be something that could be
>>> added to the language syntax?
>>>
>>> Thanks for your consideration!
>>>
>>> Sincerely,
>>> Greg
>>>
>>
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected] <javascript:>
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected] <javascript:>
> 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 unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected] <javascript:>.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
>
>
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.