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

  • SEARCH
  • Home
  • 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 7043049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:14:31+00:00 2026-05-28T02:14:31+00:00

Clojure’s split-with function is quite handy, but has to traverse the leading part of

  • 0

Clojure’s split-with function is quite handy, but has to traverse the leading part of the seq twice, as it is literally implemented as [(take-while pred coll) (drop-while pred coll)]. Still, it is fairly easy to write a (tail-recursive) version that traverses the leading part only once (put the leading part in an accumulating vector, etc.).

However, I would like to extract the first element of a list that satisfies a predicate and return the both the element, and the remaining list (i.e. (concat (take-while pred coll) (next (drop-while pred coll)))) — hopefully in a single pass. If I were using some imperative language, I would just traverse the list, holding onto the last cell, and, once I get the element to pop out, fiddle with the “next pointer” of the previous cell to reconstruct the modified list, but this seems out of question in a functional language.

So is there a way to do that efficiently in Clojure?

  • 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-28T02:14:32+00:00Added an answer on May 28, 2026 at 2:14 am

    For split-with (and similar tasks where you want to produce two outputs from one input), you can have any two of

    • Laziness
    • Immutability
    • Perfect efficiency.

    For example, if you don’t want laziness (of the first “dropped” portion), you can get the other two by implementing a tail-recursive version as you suggest.

    All this is not really applicable to your current question, since you only want one output sequence, and I recommend kotarak’s solution (or something else like it). However, I thought you might like an explanation for why Clojure’s built-in split-with traverses the input sequence twice.

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

Sidebar

Related Questions

Clojure has introduced me to the concept of Lisp syntax, and I'm interested, but
Clojure has some interesting features for representing types including but not limited to deftype
In clojure, I would like to write a tail-recursive function that memoizes its intermediate
------------------------- clojure.core/seq ([coll]) Returns a seq on the collection. If the collection is empty,
In clojure, I would like to create a record inside a function. I tried:
The Clojure distinct? method doesn't take a collection, but rather a list of args
From Clojure it is easy enough to use Java libraries...but what libraries does Clojure
Clojure macro noob here. I have a function with some optional parameters, e.g. (defn
Clojure has a -> macro which inserts each expression recursively as the first argument
Does Clojure specify the order of evaluation of function arguments? I.e. When I call

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.