Re: Flatten a list
I always end up doing (filter identity '(:fred :mary nil :sue)) (remove nil? ...) is actually more clear. I'll try to remember that. On Tue, Feb 24, 2009 at 10:42 PM, Timothy Pratley timothyprat...@gmail.comwrote: user= (remove nil? '(:a nil nil :b :a)) (:a :b :a) On Feb 25, 2:38 pm, Sean francoisdev...@gmail.com wrote: I've got the following list (:a nil nil :b :a) I want to call a nil-killer function, and get the following list (:a :b :a) How do I go about this? Could someone post a quick example? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Flatten a list
I've got the following list (:a nil nil :b :a) I want to call a nil-killer function, and get the following list (:a :b :a) How do I go about this? Could someone post a quick example? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Flatten a list
filter, http://clojure.org/api#filter On Tue, Feb 24, 2009 at 7:38 PM, Sean francoisdev...@gmail.com wrote: I've got the following list (:a nil nil :b :a) I want to call a nil-killer function, and get the following list (:a :b :a) How do I go about this? Could someone post a quick example? -- And what is good, Phaedrus, And what is not good— Need we ask anyone to tell us these things? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Flatten a list
user= (remove nil? '(:a nil nil :b :a)) (:a :b :a) On Feb 25, 2:38 pm, Sean francoisdev...@gmail.com wrote: I've got the following list (:a nil nil :b :a) I want to call a nil-killer function, and get the following list (:a :b :a) How do I go about this? Could someone post a quick example? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Flatten a list
Awesome! Thanks guys! On Feb 24, 10:42 pm, Timothy Pratley timothyprat...@gmail.com wrote: user= (remove nil? '(:a nil nil :b :a)) (:a :b :a) On Feb 25, 2:38 pm, Sean francoisdev...@gmail.com wrote: I've got the following list (:a nil nil :b :a) I want to call a nil-killer function, and get the following list (:a :b :a) How do I go about this? Could someone post a quick example? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Flatten a list
On Feb 24, 2009, at 5:42 PM, Timothy Pratley wrote: user= (remove nil? '(:a nil nil :b :a)) (:a :b :a) C'mon! Doesn't anybody do things the old-fashioned way anymore? (defn kill-nil ([l] (kill-nil l '())) ([l result] (cond (nil? l) (reverse result) (nil? (first l)) (recur (rest l) result) true (recur (rest l) (cons (first l) result ) You young whipper snappers! Aloha, David Sletten --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Flatten a list
On Feb 24, 2009, at 6:07 PM, David Sletten wrote: (defn kill-nil ([l] (kill-nil l '())) ([l result] (cond (nil? l) (reverse result) (nil? (first l)) (recur (rest l) result) true (recur (rest l) (cons (first l) result ) I forgot to ask... In Lisp, rather than repeatedly testing for an optional argument like this: (defun kill-nil (l optional (result '())) (cond ((endp l) (nreverse result)) ((null (first l)) (kill-nil (rest l) result)) (t (kill-nil (rest l) (cons (first l) result ) I would preserve the interface to the user (i.e., single arg) but eliminate the decisions regarding the optional arg from the recursive calls: (defun kill-nil (l) (labels ((kill-nil-aux (l result) (cond ((endp l) (nreverse result)) ((null (first l)) (kill-nil-aux (rest l) result)) (t (kill-nil-aux (rest l) (cons (first l) result )) (kill-nil-aux l '( In the Clojure version above there doesn't seem to be any penalty since the recur only occurs after the correct arity has been selected. Is this correct? How about a more traditional recursion? (defn kill-nil ([l] (kill-nil l '())) ([l result] (cond (nil? l) (reverse result) (nil? (first l)) (recur (rest l) result) true (kill-nil (rest l) (cons (first l) result ) Is there a penalty deciding which arity to use on each call? Aloha, David Sletten --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Flatten a list
On Feb 24, 11:35 pm, David Sletten da...@bosatsu.net wrote: On Feb 24, 2009, at 6:07 PM, David Sletten wrote: (defn kill-nil ([l] (kill-nil l '())) ([l result] (cond (nil? l) (reverse result) (nil? (first l)) (recur (rest l) result) true (recur (rest l) (cons (first l) result ) I forgot to ask... In Lisp, rather than repeatedly testing for an optional argument like this: (defun kill-nil (l optional (result '())) (cond ((endp l) (nreverse result)) ((null (first l)) (kill-nil (rest l) result)) (t (kill-nil (rest l) (cons (first l) result ) I would preserve the interface to the user (i.e., single arg) but eliminate the decisions regarding the optional arg from the recursive calls: (defun kill-nil (l) (labels ((kill-nil-aux (l result) (cond ((endp l) (nreverse result)) ((null (first l)) (kill-nil-aux (rest l) result)) (t (kill-nil-aux (rest l) (cons (first l) result )) (kill-nil-aux l '( In the Clojure version above there doesn't seem to be any penalty since the recur only occurs after the correct arity has been selected. Is this correct? How about a more traditional recursion? (defn kill-nil ([l] (kill-nil l '())) ([l result] (cond (nil? l) (reverse result) (nil? (first l)) (recur (rest l) result) true (kill-nil (rest l) (cons (first l) result ) Is there a penalty deciding which arity to use on each call? It depends if I'm on your lawn. Aloha, David Sletten --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---