Removing the 5th, 10th, 15th ... element of a list

186 views
Skip to first unread message

Cecil Westerhof

unread,
Feb 17, 2015, 2:21:20 PM2/17/15
to clo...@googlegroups.com
What is the best way to remove all elements which position (counting from 1) is a multiply of five out of a list?

So the list:
    (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21)
​becomes:
    (1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 21)​

--
Cecil Westerhof

Timothy Baldridge

unread,
Feb 17, 2015, 2:27:03 PM2/17/15
to clo...@googlegroups.com
Tweak as needed:

(keep-indexed 
              (fn [i v]
                (if (= 0 (mod i 5))
                  nil
                  v))
              (range 30))

--
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
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
“One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.”
(Robert Firth)

Ivan L

unread,
Feb 17, 2015, 2:28:26 PM2/17/15
to clo...@googlegroups.com
this works

=> (filter #(not= (mod % 5) 0) (range 22))
(1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 21)

Timothy Baldridge

unread,
Feb 17, 2015, 2:29:23 PM2/17/15
to clo...@googlegroups.com
True, that works, but he wants to remove based on element position, not element value. 

Timothy

--
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
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cecil Westerhof

unread,
Feb 17, 2015, 3:32:17 PM2/17/15
to clo...@googlegroups.com
2015-02-17 20:26 GMT+01:00 Timothy Baldridge <tbald...@gmail.com>:
Tweak as needed:

(keep-indexed 
              (fn [i v]
                (if (= 0 (mod i 5))
                  nil
                  v))
              (range 30))

​I made the following:
 
​    (defn indexed-sieve [index this-list]
      (keep-indexed
        (fn [i v]
          (if (= 0 (mod (inc i) index))
              nil
            v))
        this-list))

The first element should not be filtered (counting from 1) and because I will use it more often I created a function.​

 
On Tue, Feb 17, 2015 at 12:21 PM, Cecil Westerhof <cldwes...@gmail.com> wrote:
What is the best way to remove all elements which position (counting from 1) is a multiply of five out of a list?

So the list:
    (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21)
​becomes:
    (1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 21)​

--
Cecil Westerhof

Colin Yates

unread,
Feb 17, 2015, 3:49:43 PM2/17/15
to clo...@googlegroups.com

Style police would point out zero? and when-not :).

--

Timothy Baldridge

unread,
Feb 17, 2015, 3:51:22 PM2/17/15
to clo...@googlegroups.com
Clearly the answer is a program that runs every snippet posted to this mailing list through kibit (https://github.com/jonase/kibit). 

Colin Yates

unread,
Feb 17, 2015, 3:56:12 PM2/17/15
to clo...@googlegroups.com

+1

And don't forget Eastwood as well :).

On a more serious note, I found http://blog.mattgauger.com/blog/2014/09/15/clojure-code-quality-tools/ has some other great tips related to this.

Colin Jones

unread,
Feb 17, 2015, 4:39:25 PM2/17/15
to clo...@googlegroups.com
Sounds almost like a mirror image of `clojure.core/take-nth`, so something like this is kind of fun:

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

Fluid Dynamics

unread,
Feb 17, 2015, 6:45:02 PM2/17/15
to clo...@googlegroups.com
On Tuesday, February 17, 2015 at 2:21:20 PM UTC-5, Cecil Westerhof wrote:

The correct answer, obviously, is

(->> s
  (cons ::sentinel)
  (partition n n nil)
  (map next)
  (mapcat identity))

with, in this case, s = (range 1 22) and n = 5

Ben Wolfson

unread,
Feb 17, 2015, 6:47:59 PM2/17/15
to clo...@googlegroups.com
why not (mapcat next)?

--
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
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks, which may be sweet, aromatic, fermented or spirit-based. ... Family and social life also offer numerous other occasions to consume drinks for pleasure." [Larousse, "Drink" entry]

Fluid Dynamics

unread,
Feb 17, 2015, 7:28:20 PM2/17/15
to clo...@googlegroups.com
On Tuesday, February 17, 2015 at 6:47:59 PM UTC-5, Ben wrote:
why not (mapcat next)?

Yeah, that should work too, though conceptually trimming each part and then reflattening the partition are two separate steps of the computation. :)

Udayakumar Rayala

unread,
Feb 17, 2015, 10:24:29 PM2/17/15
to clo...@googlegroups.com
Then probably (apply concat) is better than (mapcat identity) isnt it?

What is (cons ::sentinel)? Why do you need it here?

--

Fluid Dynamics

unread,
Feb 17, 2015, 10:43:35 PM2/17/15
to clo...@googlegroups.com
On Tuesday, February 17, 2015 at 10:24:29 PM UTC-5, Udayakumar Rayala wrote:
Then probably (apply concat) is better than (mapcat identity) isnt it?

What is (cons ::sentinel)? Why do you need it here?

It'll drop the wrong elements otherwise:

=> (->> (range 1 22)
     (partition 5 5 nil)
     (mapcat next))
(2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20)

All the partitions need to be shifted by one position to the left, so the elements to drop are at their left edges, if we want to use the efficient "next" instead of the evil linear-time "butlast" in the later step. But that can be done by prepending a dummy element to the input, which will conveniently go away as one of the elided elements.

Udayakumar Rayala

unread,
Feb 17, 2015, 10:46:15 PM2/17/15
to clo...@googlegroups.com
Ah ok. Got it.

--
Reply all
Reply to author
Forward
0 new messages