Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Clojure vector function results exclude the stop value? [closed]

Tags:

vector

clojure

Forgive me for the newb question and potentially incorrect terminology.

Clojure vector functions produce values that do not include the stop value. For example:

=> (subvec [:peanut :butter :and :jelly] 1 3)
[:butter :and]
=> (range 1 5)
(1 2 3 4)

The doc for range explicitly states this but doesn't give a rational: "...Returns a lazy seq of nums from start (inclusive) to end (exclusive)...".

In Ruby these operations are inclusive:

(1..5).to_a => [1, 2, 3, 4, 5] [:peanut, :butter, :and, :jelly][1,3] => [:butter, :and, :jelly]

Obviously these are very different languages, but I'm wondering if there was some underlying reason, beyond a personal preference by the language designers?

like image 892
Allyl Isocyanate Avatar asked Jan 11 '23 04:01

Allyl Isocyanate


2 Answers

Making the end exclusive allows you to do things like specify (count collection) as the endpoint without getting an NPE. That's about the biggest difference between the two approaches.

like image 53
Chuck Avatar answered Jan 18 '23 15:01

Chuck


It might be that the indexing was chosen in order to be consistent with Java libraries. java.lang.String.substring and java.util.List.subList both have exclusive-end indexes.

like image 21
Nathan Hughes Avatar answered Jan 18 '23 14:01

Nathan Hughes