Re: Adding implicit indexing to Clojure lists and arrays?

2013-07-03 Thread david
(defn indexed
  Returns a lazy sequence of [index, item] pairs, where items come
  from 's' and indexes count up from zero.

  (indexed '(a b c d)) = ([0 a] [1 b] [2 c] [3 d])
  [s]
  (map vector (iterate inc 0) s))

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Adding implicit indexing to Clojure lists and arrays?

2013-06-28 Thread Mikera
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 mike.r.an...@gmail.com 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 clo...@googlegroups.com javascript:
 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 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 clojure+u...@googlegroups.com 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 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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Adding implicit indexing to Clojure lists and arrays?

2013-06-27 Thread Mikera
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 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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Adding implicit indexing to Clojure lists and arrays?

2013-06-27 Thread Greg
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 mike.r.anderson...@gmail.com 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 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 unsubscribe from this group and stop receiving emails from it, send an 
 email to clojure+unsubscr...@googlegroups.com.
 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 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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Adding implicit indexing to Clojure lists and arrays?

2013-06-26 Thread Greg
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 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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Adding implicit indexing to Clojure lists and arrays?

2013-06-26 Thread Michael-Keith Bernard (SegFaultAX)
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 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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Adding implicit indexing to Clojure lists and arrays?

2013-06-26 Thread Cedric Greevey
(v 0) and the like already work when v is a vector. You can also do
something like this:

(defn proper-rem [a b]
  (if (neg? a)
(dec (- b (rem (- (inc a)) b)))
(rem a b)))

(defn at [v idx]
  (v (proper-rem idx (count v

(defn r [start end v]
  (let [c (count v)
s (proper-rem start c)
e (proper-rem end c)
s2 (min s e)
e2 (max s e)
(subvec v s2 e2)))

(at '[a b c d e f g] -1)
g
(r '[a b c d e f g] -2 2)
[c d e]


On Wed, Jun 26, 2013 at 11:01 PM, Greg g...@kinostudios.com 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 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 unsubscribe from this group and stop receiving emails from it, send an
 email to clojure+unsubscr...@googlegroups.com.
 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 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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.