Created
August 30, 2014 18:02
-
-
Save echosa/06c51ee6b985464220fe to your computer and use it in GitHub Desktop.
Clojure core.typed comparator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Trying to annotate a comparator that sorts a list of namespaces alphabetically | |
zork-fortress.core=> (clojure.core.typed/cf (comp (t/fn [ns1 :- clojure.lang.Namespace ns2 :- clojure.lang.Namespace] :- t/Num (compare (str ns1) (str ns2))))) | |
Type Error (/private/var/folders/s1/t0k7x43122ggbclq88zgjl400000gn/T/form-init8319721319789341749.clj:1:24) Polymorphic function comp could not be applied to arguments: | |
Polymorphic Variables: | |
x | |
y | |
b | |
Domains: | |
(t/IFn [x -> y]) (t/IFn [b ... b -> x]) | |
Arguments: | |
(t/IFn [clojure.lang.Namespace clojure.lang.Namespace -> java.lang.Number]) | |
Ranges: | |
(t/IFn [b ... b -> y]) | |
in: (comp (do :clojure.core.typed.special-form/special-form :clojure.core.typed/fn {:ann (quote ({:ret-type {:type t/Num}, :dom ({:type clojure.lang.Namespace} {:type clojure.lang.Namespace})}))} (fn* ([ns1 ns2] (clojure.lang.Util/compare (str ns1) (str ns2)))))) | |
ExceptionInfo Type Checker: Found 1 error clojure.core/ex-info (core.clj:4403) |
But sort's going to want something that's actually an instance of java.util.Comparator.
Try this:
(ann compare-ns
(t/I java.util.Comparator
(t/IFn [clojure.lang.Namespace clojure.lang.Namespace -> t/Num])))
(def compare-ns
(comparator str))
I thought (comp) was (comparator). Didn't know that latter existed. Simply switching from the former to the latter makes it validate. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
At first glance, using
comp
is odd.comp
is for composing functions - I think you want(partial sort (fn ...))
.