Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 860647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:50:49+00:00 2026-05-15T08:50:49+00:00

I have noticed that lazy sequences in Clojure seem to be represented internally as

  • 0

I have noticed that lazy sequences in Clojure seem to be represented internally as linked lists (Or at least they are being treated as a sequence with only sequential access to elements). Even after being cached into memory, access time over the lazy-seq with nth is O(n), not constant time as with vectors.

;; ...created my-lazy-seq here and used the first 50,000 items

(time (nth my-lazy-seq 10000))
"Elapsed time: 1.081325 msecs"

(time (nth my-lazy-seq 20000))
"Elapsed time: 2.554563 msecs"

How do I get a constant-time lookups or create a lazy vector incrementally in Clojure?

Imagine that during generation of the lazy vector, each element is a function of all elements previous to it, so the time spent traversing the list becomes a significant factor.

Related questions only turned up this incomplete Java snippet:
Designing a lazy vector: problem with const

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:50:50+00:00Added an answer on May 15, 2026 at 8:50 am

    Yes, sequences in Clojure are described as “logical lists” with three operations (first, next and cons).

    A sequence is essentially the Clojure version of an iterator (although clojure.org insists that sequences are not iterators, since they don’t hold iternal state), and can only move through the backing collection in a linear front-to-end fashion.

    Lazy vectors do not exist, at least not in Clojure.

    If you want constant time lookups over a range of indexes, without calculating intermediate elements you don’t need, you can use a function that calculates the result on the fly. Combined with memoization (or caching the results in an arg-to-result hash on your own) you get pretty much the same effect as I assume you want from the lazy vector.

    This obviously only works when there are algorithms that can compute f(n) more directly than going through all preceding f(0)…f(n-1). If there is no such algorithm, when the result for every element depends on the result for every previous element, you can’t do better than the sequence iterator in any case.

    Edit

    BTW, if all you want is for the result to be a vector so you get quick lookups afterwards, and you don’t mind that elements are created sequentially the first time, that’s simple enough.

    Here is a Fibonacci implementation using a vector:

    (defn vector-fib [v]
      (let [a (v (- (count v) 2)) ; next-to-last element
            b (peek v)]   ; last element
        (conj v (+ a b))))
    
    (def fib (iterate vector-fib [1 1]))
    
    (first (drop 10 fib))
      => [1 1 2 3 5 8 13 21 34 55 89 144]
    

    Here we are using a lazy sequence to postpone the function calls until asked for (iterate returns a lazy sequence), but the results are collected and returned in a vector.

    The vector grows as needed, we add only the elements up to the last one asked for, and once computed it’s a constant time lookup.

    Was it something like this you had in mind?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.