So in your function, :ignore is not a possible values within the compared seqs :-(
2009/10/14 ataggart <[email protected]> > > How about something like this: > > (filter #(not= % :nomatch) (map #(if (= %1 %2) %1 :nomatch) [1 2 3 4] > [1 3 3 3])) > > > Or cleaned up for general use: > > (defn filter-map > "Returns the results of map, but filtered to contain only those > items whose inputs to f have satisfied pred. pred takes the same args > as f and must be side-effect free." > [pred f & colls] > (filter #(not= % :ignore) > (apply map #(if (apply pred %&) > (apply f %&) > :ignore) > colls))) > > user=> (filter-map = #(first %&) [1 2 3 4] [1 5 3 7]) > (1 3) > > > > On Oct 13, 11:06 pm, Mark Tomko <[email protected]> wrote: > > This is basically a problem of parallel iteration. I've devised 2 > > solutions, one is recursive (alas, not tail-recursive) and the other > > appears to be a more idiomatic Clojure solution. Can someone suggest > > a more efficient or cleaner solution? > > > > Here's the "literal" solution: > > > > (defn matching-elements [coll1 coll2] > > (if (or (empty? coll1) (empty? coll2)) () > > (let [e1 (first coll1) e2 (first coll2)] > > (if (= e1 e2) (cons e1 (matching-elements (rest coll1) (rest > > coll2))) > > (matching-elements (rest coll1) (rest coll2)))))) > > > > In this implementation, we first check the base case, where one of the > > sequences is empty. If one of the sequences is empty, we return an > > empty list. Otherwise, we check to see if the first elements in each > > list are equal. If so, we return the list resulting from the addition > > of the matching element to the result of the recursive call to the > > method on the remaining elements in each list. Otherwise, we simply > > return the result of the recursive call to the method on the remaining > > elements in each list. > > > > I believe that the cons prevents the use of recur to make this tail > > recursive. > > > > After a little digging and reading through a thread on this group > > about parallel iteration, I devised the following alternative > > solution, which makes use of lazy sequences: > > > > (defn matching-elements2 [coll1 coll2] > > (map first (filter #(let [[e1 e2] %] (= e1 e2)) (partition 2 > > (interleave coll1 coll2))))) > > > > Can anyone suggest an improved implementation of either > > implementation? > > > > Thanks! > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
