I wish to use the clojure "get" keyword for my own function. How can I prevent clojure from using the "get" defined in the standard libraries?
As mentioned this is not necessarily a good idea, but you can do it like this:
user=> (ns your.name.space
(:refer-clojure :exclude [get]))
nil
your.name.space=> (defn get [] "something")
#'your.name.space/get
your.name.space=>
I wouldn't recommend using get for anything but getting a value out of a collection, since that's what anyone reading your code would expect it to do.
If you don't want to do that, Wodin's answer is what you want.
If you actually want to "overload" get as per your title, that is, make the standard get function work with your own collection type - just make sure your collection implements ILookup, Map, or IPersistentSet and you can provide your own get/valAt method.
Related
I'm trying to advise a number of methods in one library with utility functions from another library, where some of the methods to be advised are defined with (defn) and some are defined with (defprotocol).
Right now I'm using this library, which uses (alter-var-root). I don't care which library I use (or whether I hand-roll my own).
The problem I'm running into right now is that protocol methods sometimes can be advised, and sometimes cannot, depending on factors that are not perfectly clear to me.
If I define a protocol, then define a type and implement that protocol in-line, then advising never seems to work. I am assuming this is because the type extends the JVM interface directly and skips the vars.
If, in a single namespace, I define a protocol, then advise its methods, and then extend the protocol to a type, the advising will not work.
If, in a single namespace, I define a protocol, then extend the protocol to a type, then advise the protocol's methods, the advising will work.
What I would like to do is find a method of advising that works reliably and does not rely on undefined implementation details. Is this possible?
Clojure itself doesn't provide any possibilities to advice functions in a reliable way, even those defined via def/defn. Consider the following example:
(require '[richelieu.core :as advice])
(advice/defadvice add-one [f x] (inc (f x)))
(defn func-1 [x] x)
(def func-2 func-1)
(advice/advise-var #'func-1 add-one)
> (func-1 0)
1
> (func-2 0)
0
After evaluation of the form (def func-2 func-1), var func-2 will contain binding of var func-1 (in other words its value), so advice-var won't affect it.
Eventhough, definitions like func-2 are rare, you may have noticed or used the following:
(defn generic-function [generic-parameter x y z]
...)
(def specific-function-1 (partial generic-function <specific-arg-1>))
(def specific-function-2 (partial generic-function <specific-arg-2>))
...
If you advice generic-function, none of specific functions will work as expected due to peculiarity described above.
If advising is critical for you, as a solution that may work, I'd suppose the following: since Clojure functions are compiled to java classes, you may try to replace java method invoke with other method that had desired behaviour (however, things become more complicated when talking about replacing protocol/interface methods: seems that you'll have to replace needed method in every class that implements particular protocol/interface).
Otherwise, you'll need explicit wrapper for every function that you want to advice. Macros may help to reduce boilerplate in this case.
I'm using an external library, and passing it a function that I write. Something like this, for example:
(ext-func my-func) ...
my-func needs to be given some data to do computation. The way I see it, I have two basic choices:
1) Write my-func in such a way that it accepts my data, and returns a function, which will then have the data bound to it via closure when the external library calls it. For example:
(defn my-func
[mydata]
(fn []
(... access to mydata via closure ... )))
(ext-func (my-func somedata))
2) Do not return a function from my-func, but bind data to it when I pass it to ext-func:
(defn my-func
[mydata]
(... evaluate, use mydata, etc.))
(ext-func (partial my-func somedata))
I suppose which one to use could be answered by how I intend to use the function otherwise. If I'm going to be using it other places, I may prefer not to return a function, for example. But, all other things being equal...
...which of these is the more idiomatic approach?
partial is just sugar to create the anonymous function. Check out it's source. So, effectively they're equivalent. Take your pick. Neither is more idiomatic, just a matter of personal preference.
How to specify the possibility of two types to a variable?
(defn connect! [(or ^:String :^java.net.InetAddress) host ^:Integer port] ...)
Thanks!
From the Clojure documentation:
Clojure supports the use of type hints to assist the compiler in avoiding reflection in performance-critical areas of code. Normally, one should avoid the use of type hints until there is a known performance bottleneck
The purpose of type hints is to allow the compiler to avoid reflection. Any self-documentation aspects of type-hinted code are secondary. When you say the following:
(defn connect! [^String host])
What you're telling the compiler is to resolve all Java interop method calls on host at compile time to method calls on the String class. Allowing a form to be hinted with multiple classes would defeat this purpose - the compiler wouldn't know which class to compile a method call as. Even if it did, an object cannot be a String and an InetAddress at the same time, so any method calls compiled against the String class would be guaranteed to fail with a ClassCastException if an InetAddress happened to be passed in, and vice versa.
As far as I know the only way is to do the check yourself and add the hint inside a let:
(condp instance? host
String (let [^String s] (...))
InetAddress (let [^InetAddress a] (...)))
Is there a difference, performance or otherwise, between using a deffilterop and using a purse clojure function?
http://nathanmarz.com/blog/introducing-cascalog-a-clojure-based-query-language-for-hado.html mentions that filtering can be done with clojure functions like (< ?age2 ?age1) however looking at https://github.com/nathanmarz/cascalog/wiki/Guide-to-custom-operations it looks like you can define a function like (deffilterop is-2? [x] (= x 2)).
So my question is, is there any difference between these two approaches and if not which is the preferred syntax?
Note: It also looks like all of the defxxxop functions are being deprecated for defxxxfn instead. https://github.com/nathanmarz/cascalog/blob/develop/cascalog-core/src/clj/cascalog/logic/def.clj#L131
Turns out there is no difference from a performance perspective. The deffilterop is useful for making a parameterized function.
I'm learning Clojure. I wrote this code to recursively walk a directory.
(tree-seq #(.isDirectory %1) #(.listFiles %1) (File. "/my-directory"))
Why can't I use .isDirectory as a first-class function in Clojure? Is there a better way to rewrite this code?
Joost is spot on about Java methods not being first class functions.
As an approach to dealing with this, I usually like to wrap Java functions in a Clojure function (or find a library that does this already), then it is easy to use them in an idiomatic first-class way:
(defn directory? [^java.io.File file]
(.isDirectory file))
(defn list-files [^java.io.File file]
(.listFiles %1))
(tree-seq directory? list-files (File. "/my-directory"))
This is a few more lines of code, but has the following advantages:
You can add type hints in your functions to avoid reflection (as above)
The final code is cleaner and more idiomatic
You have abstracted away from the underlying Java interop
Java methods aren't clojure functions because you can't call a method on its own; you have to call a method on an object, and it has to be an object of the type that the method expects. In other words, in java, a method cannot be fully separated from its defining class (at least not efficiently).
An alternative to #(.foo %) would be (memfn foo), which hardly anyone uses anymore after #(...) was introduced.
You could have a look at the sourcecode of file-seq (which uses a tree-seq) to see how it works.
By the way: your code works perfectly well for me. I just have to use java.io.File instead of File in the REPL so it knows the Java class.
You've already been given the correct answers, but just to add a bit more Clojure idiomatic code, I'd also use
#(.foo %)
as Joost Diepenmaat did (yet I believed it might've been overlooked).
I would also suggest reading Listing files in a directory in Clojure.