Function returns nil
Hi, I'm fairly new to Clojure and need help with a problem. The following function always returns nil, whereas it should return the value of line (which is not nil - I've tested). (defn find-line-in-output [regex] (with-open [rdr (reader belarc-output-filepath)] (doseq [line (line-seq rdr)] (if (not (nil? (re-find (re-pattern regex) line))) line ) ) ) ) -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Function returns nil
Only use 'doseq' when you don't care about the reuturn value. In other words only for side-effect-y code. Use 'for' instead... Jim On 21/06/13 11:17, Jay C wrote: Hi, I'm fairly new to Clojure and need help with a problem. The following function always returns nil, whereas it should return the value of line (which is not nil - I've tested). (defn find-line-in-output [regex] (with-open [rdr (reader belarc-output-filepath)] (doseq [line (line-seq rdr)] (if (not (nil? (re-find (re-pattern regex) line))) line ) ) ) ) -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Function returns nil
On 21 June 2013 11:17, Jay C ubuntu...@gmail.com wrote: Hi, I'm fairly new to Clojure and need help with a problem. The following function always returns nil, whereas it should return the value of line (which is not nil - I've tested). (defn find-line-in-output [regex] (with-open [rdr (reader belarc-output-filepath)] (doseq [line (line-seq rdr)] (if (not (nil? (re-find (re-pattern regex) line))) line ) ) ) ) doseq returns nil, it's for side effecting code. If you want a return value you should use loop or something else that doesn't have side effects. Alan -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Function returns nil
If you use for, which is lazy, wrap it in a doall to force it to do its work before with-open closes your reader. On Jun 21, 2013 6:52 AM, Jim jimpil1...@gmail.com wrote: Only use 'doseq' when you don't care about the reuturn value. In other words only for side-effect-y code. Use 'for' instead... Jim On 21/06/13 11:17, Jay C wrote: Hi, I'm fairly new to Clojure and need help with a problem. The following function always returns nil, whereas it should return the value of line (which is not nil - I've tested). (defn find-line-in-output [regex] (with-open [rdr (reader belarc-output-filepath)] (doseq [line (line-seq rdr)] (if (not (nil? (re-find (re-pattern regex) line))) line ) ) ) ) -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Function returns nil
I don't think that will work. for is lazy, so by the time it evals, the code will have dropped out of the scope of with-open. So: (defn read-stuff-2 [] (with-open [r (java.io.BufferedReader. (java.io.FileReader. myfile.txt))] (for [line (line-seq r)] line))) fails with an IOException. So, I think the OP needs: (defn read-stuff-3 [] (with-open [r (java.io.BufferedReader. (java.io.FileReader. myfile.txt))] (doall (for [line (line-seq r)] line or alternatively: (defn read-stuff-4 [] (with-open [r (java.io.BufferedReader. (java.io.FileReader. myfile.txt))] (for [line (doall (line-seq r))] line))) I really wish there were support for this in clojure.core: both dofor and domap would be very useful. Laziness is useful but when working with Java objecs with state, or anything with dynamic scope liberal use of doall is necessary which just adds extra brackets. Phil Jim jimpil1...@gmail.com writes: Only use 'doseq' when you don't care about the reuturn value. In other words only for side-effect-y code. Use 'for' instead... Jim On 21/06/13 11:17, Jay C wrote: Hi, I'm fairly new to Clojure and need help with a problem. The following function always returns nil, whereas it should return the value of line (which is not nil - I've tested). (defn find-line-in-output [regex] (with-open [rdr (reader belarc-output-filepath)] (doseq [line (line-seq rdr)] (if (not (nil? (re-find (re-pattern regex) line))) line ) ) ) ) -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- Phillip Lord, Phone: +44 (0) 191 222 7827 Lecturer in Bioinformatics, Email: phillip.l...@newcastle.ac.uk School of Computing Science, http://homepages.cs.ncl.ac.uk/phillip.lord Room 914 Claremont Tower, skype: russet_apples Newcastle University, twitter: phillord NE1 7RU -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Function returns nil
aaa yes, of course! :) Jim On 21/06/13 13:47, John D. Hume wrote: If you use for, which is lazy, wrap it in a doall to force it to do its work before with-open closes your reader. On Jun 21, 2013 6:52 AM, Jim jimpil1...@gmail.com mailto:jimpil1...@gmail.com wrote: Only use 'doseq' when you don't care about the reuturn value. In other words only for side-effect-y code. Use 'for' instead... Jim On 21/06/13 11:17, Jay C wrote: Hi, I'm fairly new to Clojure and need help with a problem. The following function always returns nil, whereas it should return the value of line (which is not nil - I've tested). (defn find-line-in-output [regex] (with-open [rdr (reader belarc-output-filepath)] (doseq [line (line-seq rdr)] (if (not (nil? (re-find (re-pattern regex) line))) line ) ) ) ) -- -- 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 mailto:clojure@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+unsubscr...@googlegroups.com mailto:clojure+unsubscr...@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+unsubscr...@googlegroups.com mailto:clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 mailto:clojure@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+unsubscr...@googlegroups.com mailto:clojure%2bunsubscr...@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+unsubscr...@googlegroups.com mailto:clojure%2bunsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Function returns nil
I really wish there were support for this in clojure.core: both dofor and domap would be very useful. mapv is non-lazy, which gives you the semantics of a domap as long as you don't mind a vector over a sequence -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Function returns nil
On 21/06/13 14:01, Neale Swinnerton wrote: I really wish there were support for this in clojure.core: both dofor and domap would be very useful. mapv is non-lazy, which gives you the semantics of a domap as long as you don't mind a vector over a sequence exactly! also, that's why macros are for...every time I think I wish there was X in clojure.core..., the next thought is macros :) what you wish is rather trivial to implement and it doesn't even have to be a macro... Jim -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Function returns nil
Thanks for all the input. Using for as in Phillip's suggestion seems to have gotten me somewhere, but now the function returns during every iteration. What am I missing to have it only return when a conditional statement is satisfied? -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Function returns nil
'for' accepts a :when clause which will get you even further :) Jim ps: it also accepts a :let clause if you find it useful On 21/06/13 14:06, Jay C wrote: Thanks for all the input. Using for as in Phillip's suggestion seems to have gotten me somewhere, but now the function returns during every iteration. What am I missing to have it only return when a conditional statement is satisfied? -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Function returns nil
On Friday, 21 June 2013 11:17:44 UTC+1, Jay C wrote: Hi, I'm fairly new to Clojure and need help with a problem. The following function always returns nil, whereas it should return the value of line (which is not nil - I've tested). (defn find-line-in-output [regex] (with-open [rdr (reader belarc-output-filepath)] (doseq [line (line-seq rdr)] (if (not (nil? (re-find (re-pattern regex) line))) line ) ) ) ) -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Function returns nil
Cheers Jim, my problem is now solved using :when and then doing (apply str (fn)) to the return value. On Friday, 21 June 2013 14:08:19 UTC+1, Jim foo.bar wrote: 'for' accepts a :when clause which will get you even further :) Jim ps: it also accepts a :let clause if you find it useful On 21/06/13 14:06, Jay C wrote: Thanks for all the input. Using for as in Phillip's suggestion seems to have gotten me somewhere, but now the function returns during every iteration. What am I missing to have it only return when a conditional statement is satisfied? -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clo...@googlegroups.comjavascript: 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 javascript: 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 javascript:. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: Function returns nil
Jim - FooBar(); jimpil1...@gmail.com writes: On 21/06/13 14:01, Neale Swinnerton wrote: I really wish there were support for this in clojure.core: both dofor and domap would be very useful. mapv is non-lazy, which gives you the semantics of a domap as long as you don't mind a vector over a sequence exactly! also, that's why macros are for...every time I think I wish there was X in clojure.core..., the next thought is macros :) what you wish is rather trivial to implement and it doesn't even have to be a macro... Well, indeed, yes, I have implemented it. But, then I have to do require statements everywhere, or I redo the implementation in every package, or add a dependency. Lots of functions in clojure.core are trivial to implement after all. dofor really is different from doseq because it doesn't drop the head. Didn't know about mapv though. Will use that lots! Phil -- -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.