Welcome! Please see the About page for a little more info on how this works.

0 votes
ago in Sequences by
edited ago by

This is Clojure 1.12.0.

user=> (doc nthrest)
-------------------------
user/nthrest
([coll n])
  Returns the nth rest of coll, coll when n is 0.
nil
user=> (rest [])
()
user=> (rest '())
()
user=> (rest nil)
()
user=> (nthrest [] 1)
[]
user=> (nthrest '() 1)
()
user=> (nthrest nil 1)
nil

Note that invoking rest on any of these arguments (empty vector, empty list, or nil) results in returning the empty list. In contrast, invoking nthrest with n = 1 results in many different types of return values. If the semantics of nthrest are as if rest is applied to the argument n times, as described in the doc string, then this is a bug.

Note that this is not related to an n of 1:

user=> (nthrest [] 2)
[]
user=> (nthrest '() 2)
()
user=> (nthrest nil 2)
nil

1 Answer

0 votes
ago by

I'm not able to repro that on Clojure 1.12.0:

$ clj
Clojure 1.12.0
user=> (rest [])
()
user=> (rest '())
()
user=> (rest nil)
()
user=> (nthrest [] 1)
()
user=> (nthrest '() 1)
()
user=> (nthrest nil 1)
()
user=> (nthrest [] 2)
()
user=> (nthrest '() 2)
()
user=> (nthrest nil 2)
()

The results you have look like an older version,- this was fixed in 1.12 (CLJ-2717).

ago by
Interesting. I must have just picked that up today. I noticed the Clojure updated via Brew this afternoon and my tests were done this morning. But it was 1.12 being tested this morning, so was this JUST updated (like this week)?
ago by
No, the update you saw today was just an update to the CLI, not Clojure itself. Clojure 1.12 has been out since Sept 2024. It's possible that you had an old version of the CLI though that defaulted to 1.11, or are in a project specifying an older version of Clojure.
ago by
You can check the version of the CLI with `clj -version` and in a REPL you can test the version of Clojure you are using with `*clojure-version*` at the REPL (also it's printed when the REPL starts). Note that any version of the CLI (4 part version) can use any version of Clojure (3 part version), the overlap in the first 3 parts just tells you what the default version of Clojure will be for the CLI you're using.
ago by
Yea, so this is from a REPL session earlier today. Note the `*clojure-version*`.

    user=> *clojure-version*
    {:major 1, :minor 12, :incremental 0, :qualifier nil}
    user=> (nthrest [] 1)
    []

Then, I restarted the REPL session from the CLI with `clj` after the update earlier today and now it works just like you showed. Hm... weird.
ago by
Ah, I think I figured it out. Two days ago, during the course of trying to debug some weirdness,  I copied the source for `nthrest` into the REPL to validate that I was seeing what I thought I was seeing. I didn't realize it was 1.11.1 code at the time, though. And IIRC I forgot to change the name of the function, so the new definition overwrote the standard one. So, I think I had a 1.11.1 version of `nthrest` running in 1.12.0 and that was tripping me up. So, I think pilot error on my part. Sorry to have troubled you. Glad the bug got fixed in 1.12.0.
...