I tried the two options:
(.replace my-string "string-to-replace" "new-string"))
and:
(.clojure.string/replace my-string "string-to-replace" "new-string"))
I can use the both, but I don't know what is the difference, can you explain?
(.replace my-string) - this is JavaScript method String.prototype.replace()
(clojure.string/replace) is a wrapper for the same js method
Related
I am trying to use https://github.com/json-path/JsonPath in Clojure
It looks straightforward from the README, especially since it uses static functions. So I replicated the example from the README:
(import '[com.jayway.jsonpath JsonPath Criteria Filter])
(JsonPath/parse "{\"a\":\"1\"}") ;; returns a com.jayway.jsonpath.internal.JsonContext
(JsonPath/read (JsonPath/parse "{\"a\":\"1\"}") "$.a")
;; (.read (JsonPath/parse "{\"a\":\"1\"}") "$.a")
;; Exception ->
;; 1. Caused by java.lang.IllegalArgumentException
;; No matching method: read
;; I tried variations of the above line
;; for some reason this seems to want to take `this` as first parameter - I cannot
;; figure out why, and cannot seem to be able to pass a valid value
How can I call this Java static function from Clojure? Why does it want as this?
dependency: [com.jayway.jsonpath/json-path "2.4.0"]
The problem is the varargs with Predicate on read. On JVM-Level you have to pass those args as array. With Java the compiler will take care of that.
E.g. this works:
user=> (import '[com.jayway.jsonpath JsonPath Criteria Filter Predicate])
#<Class#5a114a96 com.jayway.jsonpath.Predicate>
user=> (JsonPath/read "{\"a\":\"1\"}" "$.a" (into-array Predicate []))
"1"
I have been struggling to find an answer or develop a solution. I am trying to figure out how to make code that makes code in Clojure. For my first feat, I want a function that will print to stdout the name of the symbol and its value, useful for debugging. Example:
(def mysymbol 5)
(debugging-function mysymbol)
mysymbol: 5
Does that make sense? Thanks for your help.
Post Discussion Update
Here is the answer from #amalloy:
(defmacro ?
"A useful debugging tool when you can't figure out what's going on:
wrap a form with ?, and the form will be printed alongside
its result. The result will still be passed along."
[val]
`(let [x# ~val]
(prn '~val '~'is x#)
x#))
So:
(? myvariable)
You can see a simple version of this that I wrote on github. The main point is that you can't do this with a function, but with a macro it's simple enough - you just have to get your quoting and unquoting right.
Given a string, I want to replace all links within it with the link's description. For example, given
this is a [[http://link][description]]
I would like to return
this is a description
I used re-builder to construct this regexp for a link:
\\[\\[[^\\[]+\\]\\[[^\\[]+\\]\\]
This is my function:
(defun flatten-string-with-links (string)
(replace-regexp-in-string "\\[\\[[^\\[]+\\]\\[[^\\[]+\\]\\]"
(lambda(s) (nth 2 (split-string s "[\]\[]+"))) string))
Instead of replacing the entire regexp sequence, it only replaces the trailing "]]". This is what it produces:
this is a [[http://link][descriptiondescription
I don't understand what's going wrong. Any help would be much appreciated.
UPDATE: I've improved the regex for the link. It's irrelevant to the question but if someone's gonna copy it they may as well get the better version.
Your problem is that split-string is clobbering the match data, which
replace-regexp-in-string is relying on being unchanged, since it is going to
go use that match data to decide which sections of the string to cut out. This
is arguably a doc bug in that replace-regexp-in-string does not mention that
your replacement function must preserve the match data.
You can work around by using save-match-data, which is a macro provided for
exactly this purpose:
(defun flatten-string-with-links (string)
(replace-regexp-in-string "\\[\\[[a-zA-Z:%#/\.]+\\]\\[[a-zA-Z:%#/\.]+\\]\\]"
(lambda (s) (save-match-data
(nth 2 (split-string s "[\]\[]+")))) string))
In ruby I frequently use File.expand_path(File.dirname(__FILE__)) for loading config files or files with test data. Right now I'm trying to load some html files for a test in my clojure app and I can't figure out how to do it without hard coding the full path to the file.
edit:
I'm using leinigen if that helps in any way
ref: __FILE__ is a special literal which returns the filename (including any path) given to the program when executed. see (rubydoc & perldata)
*file*
API Reference (add *file* to the url)
Here is one way to replicate that in Clojure:
(defn dirname [path]
(.getParent (java.io.File. path)))
(defn expand-path [path]
(.getCanonicalPath (java.io.File. path)))
Then your Ruby line File.expand_path(File.dirname(__FILE__)) in Clojure would be this:
(expand-path (dirname *file*))
See Java interop docs for .getParent & .getCanonicalPath.
NB. I think *file* always returns the absolute (though not canonical) pathname/filename in Clojure. Whereas __FILE__ returns the the pathname/filename provided at execution. However I don't think these difference should effect what your trying to do?
/I3az/
Neither of the 9 point solutions is correct. *file* gives you a file relative to the classpath. Using .getCanonicalPath or .getAbsolutePath on *file* will give you a nonexistant file. As pointed out in this old thread, you need to use ClassLoader to resolve *file* correctly. Here's what I use to get the parent directory of the current file:
(-> (ClassLoader/getSystemResource *file*) clojure.java.io/file .getParent)
Based on user83510's answer above, the full answer is:
(def path-to-this-file
(clojure.string/join "/" [(-> (ClassLoader/getSystemResource *file*) clojure.java.io/file .getParent) (last (clojure.string/split *file* #"/"))]))
It ain't pretty but it works :P
I've been trying out fnparse library written by Joshua Choi in Clojure and I'm having difficulties trying to work out how to call the rules on the text that I want to parse. I've been experimenting with cat which is part of the new release. Lets take the example code listed. Could anyone give me some ideas how I could call the rule on an expression?
Thank you!
thanks for trying out FnParse 3.
In general, you use the edu.arizona.fnparse/match form (as well as the complementary find, substitute, and substitute-1 forms) to use the rules that you create. Check their documentation strings.
Sorry about the confusion—I should have added an example of match in math.clj—but take a look at the bottom of the sample Clojure parser. Even though the Clojure parser uses FnParse Hound, match works the same way in both Cat and Hound.