practicalli / practicalli/clojure

Remove the first occurance of a value in a collection

Open
#297 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

clojure-core
Dominant language
Makefile
Stars
117
Forks
36
PR merge metrics
No merged PRs in 30d

Description

How to remove only the first occurrence of a number from a collection, in this case a vector
For example, how could only the first occurrence of 2 be removed from the following collection

[8 49 2 0 7 0 -1 7 92 2 9]

The filter function can be used to remove all occurrences of any given number. It is not quite as straight forward with just the first occurance

One approach is to split the collection at the first matching value

(defn remove-first [pred coll]
  (let [[before non-pred-and-after] (split-with (complement pred) coll)]
    (concat before (rest non-pred-and-after))))

?? take and drop approach ??

Another approach based on the design of the clojure.core/filter function:

(defn remove-first [pred coll]
  (lazy-seq
   (when-let [s (seq coll)]
     (let [f (first s) r (rest s)]
       (if (pred f)
         r
         (cons f (remove-first pred r)))))))

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the issue's two remove-first examples and compare their behavior for a matching value, no match, and an empty collection. The issue names no documentation file or test, so first locate where collection guidance belongs; done would require a clear, agreed recommendation for removing only the first occurrence.

Written by the indexing model from the issue text.

Assessment

Tech stack
clojure
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.