Noob question on strings.
Hi all, I've just started looking with interest at the language, and have decided to port some of my smaller programs and scripts to Clojure as a way of getting to know the language. I am stumbling over Strings at the moment, as I am trying to read a file. I have tried the following: user=> "C:\dev\java\clojure\clj-repl.bat" ")\n" user=> java.lang.Exception: Invalid token: C: java.lang.Exception: ReaderError:(8,1) Invalid token: C: at clojure.lang.LispReader.read(LispReader.java:164) at clojure.lang.Repl.main(Repl.java:68) Caused by: java.lang.Exception: Invalid token: C: at clojure.lang.LispReader.interpretToken(LispReader.java:266) at clojure.lang.LispReader.read(LispReader.java:156) ... 1 more OK, so that didn't work. I read the docs on Strings: Strings - Enclosed in "double quotes". May span multiple lines. Standard Java escape characters are supported. So next I tried, thinking it may be the backslashes that were the problem, and needed escaping: "C:\\dev\\java\\clojure\\clj-repl.bat" "\n" user=> java.lang.Exception: Invalid token: C: java.lang.Exception: ReaderError:(10,1) Invalid token: C: at clojure.lang.LispReader.read(LispReader.java:164) at clojure.lang.Repl.main(Repl.java:68) Caused by: java.lang.Exception: Invalid token: C: at clojure.lang.LispReader.interpretToken(LispReader.java:266) at clojure.lang.LispReader.read(LispReader.java:156) ... 1 more Same error, and it seems to me that the double quoted string isn't being accepted as a string literal. I tried converting the path to forward slashes: java.lang.Exception: No such namespace: C:/dev/java/clojure clojure.lang.Compiler$CompilerException: NO_SOURCE_FILE:0: No such namespace: C:/dev/java/clojure at clojure.lang.Compiler.analyze(Compiler.java:3713) at clojure.lang.Compiler.analyze(Compiler.java:3671) at clojure.lang.Compiler.eval(Compiler.java:3895) at clojure.lang.Repl.main(Repl.java:75) Caused by: java.lang.Exception: No such namespace: C:/dev/java/clojure at clojure.lang.Compiler.resolveIn(Compiler.java:3998) at clojure.lang.Compiler.resolve(Compiler.java:3972) at clojure.lang.Compiler.analyzeSymbol(Compiler.java:3955) at clojure.lang.Compiler.analyze(Compiler.java:3686) ... 3 more So it seems that this isn't being treated as a string at all! It thinks that the slashes are denoting a namespace. What am I missing here? --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: Noob question on strings.
Hi all, > I think the reader gets confused after your initial "C:\dev\java > \clojure\clj-repl.bat" > If you start the repl again and try the string with \\ it works fine. > Also, when the reader gets confused by the above string, it seems to > miss the " at the end. If you enter a " by itself it seems to right > itself: Yes, thanks, you are all quite right. The reader had got confused somehow, and was treating the input as an unterminated string. Just putting a " on its own solved the problem. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Partitioning a list when the result of a predicate becomes a certain value.
Hi all, I am battering my head against the following problem which I'm sure is straightforward if only I knew how. I want to partition the following list: '("aa123" "x" "y" "z" "bb123" "ccq23" "3" "yg") into the following: (("aa123" "x" "y" "z") ("bb123") ("ccq23" "3" "yg")) The predicate is: #(re-matches #"^(\w)\1.*" %) partition-by doesn't work, since it splits the sequence when the result of applying the predicate changes. I want to partition when the predicate becomes a particular value. Any clues on how to accomplish this would be gratefully received! Thanks, Anthony. -- 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: Partitioning a list when the result of a predicate becomes a certain value.
After posting, I had the idea of checking out the source for partition- by, and solved the problem: (defn partition-when "Applies f to each value in coll, splitting it each time f returns the specified value. Returns a lazy seq of partitions." [f value coll] (lazy-seq (when-let [s (seq coll)] (let [run (cons (first s) (take-while #(not= value (f %)) (next s)))] (cons run (partition-when f value (seq (drop (count run) s ) ) ) ) So, I can see basically how this function works, but am not sure what the when-let gives you. Could someone explain? The documentation makes no sense to me at the moment: " (when-let bindings & body) bindings => binding-form test When test is true, evaluates body with binding-form bound to the value of test " What test? Thanks, Anthony. -- 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: Partitioning a list when the result of a predicate becomes a certain value.
Thanks for the replies guys - has given me things to mull over. On Thursday, 10 May 2012 22:11:18 UTC+1, Ant wrote: > > Hi all, > > I am battering my head against the following problem which I'm sure is > straightforward if only I knew how. I want to partition the following > list: > > '("aa123" "x" "y" "z" "bb123" "ccq23" "3" "yg") > > into the following: > > (("aa123" "x" "y" "z") ("bb123") ("ccq23" "3" "yg")) > > The predicate is: > > #(re-matches #"^(\w)\1.*" %) > > partition-by doesn't work, since it splits the sequence when the > result of applying the predicate changes. I want to partition when the > predicate becomes a particular value. > > Any clues on how to accomplish this would be gratefully received! > > Thanks, > > Anthony. -- 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