Remove Value From Vector at Given Index

1,667 views
Skip to first unread message

Don

unread,
Dec 1, 2009, 10:06:08 PM12/1/09
to Clojure
I have a vector [2 4 5 8 6 4]

And I want to remove a value based on index. Specifically, I want to
remove every third item.

So my new vector would be [2 4 8 6]

Thank you.

Wilson MacGyver

unread,
Dec 1, 2009, 10:31:32 PM12/1/09
to clo...@googlegroups.com
you can do it using partition and flatten from clojure.contrib.seq-utils

(use 'clojure.contrib.seq-utils)
(flatten (partition 2 3 [2 4 5 8 6 4]))

this yields (2 4 8 6)
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en



--
Omnem crede diem tibi diluxisse supremum.

Don

unread,
Dec 1, 2009, 10:36:46 PM12/1/09
to Clojure
Thank you Wilson.

Mark N

unread,
Dec 3, 2009, 12:42:17 PM12/3/09
to Clojure
You might want to use partition-all from seq-utils, since partition
may drop the end elements if it can't make a full size partition.

For example
user=> (flatten (partition 2 3 [2 4 5 8]))
(2 4)

But
user=> (flatten (partition-all 2 3 [2 4 5 8]))
(2 4 8)

Sean Devlin

unread,
Dec 3, 2009, 1:39:33 PM12/3/09
to Clojure
He's another (longer) way

(vec (filter identity
(map
(fn [x idx] (if (zero? (mod 3 idx)) x))
[2 4 5 8 6 4]
(iterate inc 0))))

I'd use a form of partition production, though. Some thing like this:

(apply concat (partition-all 2 3 [2 4 5 8 6 4]))

The concat should be faster than flatten, because it doesn't have to
do a seqable? check.

Sean

CuppoJava

unread,
Dec 3, 2009, 2:38:40 PM12/3/09
to Clojure
And here's yet another way:

(for [[x b] (zip coll (cycle [true true false])) :when b]
x)

where zip is defined: (def zip (partial map vector))

-Patrick

ataggart

unread,
Dec 3, 2009, 5:10:41 PM12/3/09
to Clojure
There's take-nth in core, but no drop-nth (which sounds like what you
need), so I wrote it:

(defn drop-nth [n coll]
(lazy-seq
(when-let [s (seq coll)]
(concat (take n s) (drop-nth n (next (drop n s)))))))

So, in your example:

(vec (drop-nth 2 [2 4 5 8 6 4]))

ataggart

unread,
Dec 3, 2009, 5:23:52 PM12/3/09
to Clojure


On Dec 3, 2:10 pm, ataggart <alex.tagg...@gmail.com> wrote:
> There's take-nth in core, but no drop-nth (which sounds like what you
> need), so I wrote it:
>
> (defn drop-nth [n coll]
>   (lazy-seq
>     (when-let [s (seq coll)]
>       (concat (take n s) (drop-nth n (next (drop n s)))))))
>
> So, in your example:
>
> (vec (drop-nth 2 [2 4 5 8 6 4]))
>

Just realized I wrote that zero-based, but take-nth is 1-based.
Fixed:

(defn drop-nth [n coll]
(lazy-seq
(when-let [s (seq coll)]
(concat (take (dec n) s) (drop-nth n (drop n s))))))

Note the results aren't mutually exclusive to take-nth since both
"take" the first element (which seems reasonable).


Reply all
Reply to author
Forward
0 new messages