Sven Richter <[email protected]> writes:
> HI,
>
> I just posted a question to stackoverflows code review
> page:http://codereview.stackexchange.com/questions/90809/remove-lines-from-a-2d-vec-in-clojure
>
> As there is not much traffic regarding clojure I also double post to this
> list in the hope to get some good answers. You might respond here or there,
> whatever you like best.
>
> I have some code here that I am very unhappy with. The task I am trying to
> accomplish is this.
>
> Given a 2d vec like this:
>
> [[0 2 0] [1 3 5] [3 3 0]]
> which can contain positive ints including zero I want to remove all _lines_
> that are greater zero.
> Whereas the definition of _line_ is the following:
> A _line_ is represented by the position n in every vec inside the 2d vec.
> So my example above has three lines:
>
> [0 1 3], [2 3 3] and [0 5 0].
>
> The line that I want to remove from it according to my algortihm is **[2 3
> 3]** because every element is greater than zero.
>
> So my 2d vec would now look like this:
>
> [[0 0] [1 5] [3 0]]
>
> And finally I want to pad the vecs to their original size filling them with
> zero for every removed line, so that it looks finally like this:
>
> [[0 0 0] [0 1 5] [0 3 0]]
>
You want to transpose the matrix before working with it.
The following code should do what you want:
(def v [[0 2 0] [1 3 5] [3 3 0]])
(defn transpose
[v]
(apply map vector v))
(defn all-positive?
[line]
(every? pos? line))
(defn remove-non-zero-lines
[lines]
(let [removed (remove all-positive? lines)
zero-line (vec (repeat (count (first lines)) 0))]
(concat
(repeat (- (count lines) (count removed)) zero-line)
removed)))
(defn doit
[v]
(-> v transpose remove-non-zero-lines transpose))
--
Cheers
Ralf
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.