Re: bug in partition?
Thanks for the hint. And don't worry about the meaning of this function. :) Name parameter has no use. And the regex stuff's for the url. Razvan -- 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
Aw: Re: bug in partition?
Hi, it does if you use next instead of rest. rest returns something you have to call seq on before knowing whether it is nil or not. This is necessary to allow full laziness. When looping as in your case, you'll almost always want next instead of rest. Otherwise you usually want rest instead of next. Sincerely Meikel -- 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
Re: bug in partition?
Thanks. But still I don't get something. Shouldn't partition return a sequence? Shouldn't every sequence end with nil? Razvan -- 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
Re: bug in partition?
On Mon, Jun 13, 2011 at 5:47 PM, Mark Rathwell wrote: > > This, or wrap user-pass with seq in the conditional, which will return nil > for an empty list: > (defn authenticate? > [uri name pass] > (loop [user-pass (seq (partition 2 (.getStringArray *conf* > "authentication")))] > (if (seq user-pass) > (if (re-matches (re-pattern (ffirst user-pass)) uri) > true > (recur (rest user-pass))) > false))) Or use HOFs instead of loop/recur: (defn authenticate? [uri name pass] (some #(re-matches (re-pattern (first %)) uri) (partition 2 (.getStringArray *conf* "authentication" Of course, once you flesh this out to check the password as well, you need both parts and destructuring becomes preferable: (defn authenticate? [uri name pass] (some (fn [[conf-uri conf-pass]] (and (re-matches (re-pattern conf-uri) uri) (= conf-pass (apply-some-sort-of-hash pass (partition 2 (.getStringArray *conf* "authentication" You'd probably also want to hoist the password hashing out of the loop and the reading of the conf file, unless you expect to have to accommodate changes on the fly, out of the function entirely into a def. Not sure what you had in mind with the "name" parameter though, or why you're using regexp stuff to compare usernames. :) -- Protege: What is this seething mass of parentheses?! Master: Your father's Lisp REPL. This is the language of a true hacker. Not as clumsy or random as C++; a language for a more civilized age. -- 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
Re: bug in partition?
This, or wrap user-pass with seq in the conditional, which will return nil for an empty list: (defn authenticate? [uri name pass] (loop [user-pass (seq (partition 2 (.getStringArray *conf* "authentication")))] (if (seq user-pass) (if (re-matches (re-pattern (ffirst user-pass)) uri) true (recur (rest user-pass))) false))) In Clojure, the only values that evaluate to false are nil and false. The empty list ('(), which rest returns on your last iteration, does not evaluate to false as in some languages). - Mark On Mon, Jun 13, 2011 at 5:35 PM, Meikel Brandmeyer wrote: > Hi, > > Am 13.06.2011 um 22:04 schrieb Razvan Rotaru: > > > (defn authenticate? [uri name pass] > > (loop [user-pass (partition 2 (.getStringArray *conf* > > "authentication"))] > > (if user-pass > > (if (re-matches (re-pattern (ffirst user-pass)) > uri ) > > true > > (recur (rest user-pass))) > > false) > >)) > > (defn authenticate? > [uri name pass] > (loop [user-pass (seq (partition 2 (.getStringArray *conf* > "authentication")))] > (if user-pass > (if (re-matches (re-pattern (ffirst user-pass)) uri) >true > (recur (next user-pass))) > false))) > > You probably want something more like this. Note the tactically placed seq > and the use of next instead of rest. Does that solve your problem? (I > suspect, that you get a nil from the ffirst. rest doesn't give you a nil > (next does), so your loop doesn't stop correctly. But I haven't tested > this.) > > Sincerely > Meikel > > -- > 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 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
Re: bug in partition?
Hi, Am 13.06.2011 um 22:04 schrieb Razvan Rotaru: > (defn authenticate? [uri name pass] > (loop [user-pass (partition 2 (.getStringArray *conf* > "authentication"))] > (if user-pass > (if (re-matches (re-pattern (ffirst user-pass)) uri ) > true > (recur (rest user-pass))) > false) >)) (defn authenticate? [uri name pass] (loop [user-pass (seq (partition 2 (.getStringArray *conf* "authentication")))] (if user-pass (if (re-matches (re-pattern (ffirst user-pass)) uri) true (recur (next user-pass))) false))) You probably want something more like this. Note the tactically placed seq and the use of next instead of rest. Does that solve your problem? (I suspect, that you get a nil from the ffirst. rest doesn't give you a nil (next does), so your loop doesn't stop correctly. But I haven't tested this.) Sincerely Meikel -- 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