Using HashSets in polymorphic functions that take functions as arguments

12 views
Skip to first unread message

Jesse

unread,
Dec 13, 2014, 1:55:56 PM12/13/14
to clojure-c...@googlegroups.com
Hi everyone, I'm having trouble trying to annotate a function. The thing is that I am using the HashSet in the position of a where some expects a function, which is a common idiom in clojure to check for membership in a collection. The error tells me that polymorphic function 'some' can't be applied to args. 

i.e
Domains:
[x -> y] (Option (clojure.lang.Seqable x))
 
Arguments:
(IPersistentSet (Option Kw)) (ISeq (Option Kw))

 
The code itself looks like this:
;; external function
(defn find-availables-fn [] :- (Seq (Option Kw))...) 

;; evil function 
(defn seq-of-keywords [my-ref :- (Ref1 (Set (Option Kw)))] :- (Option (Seq Kw))
  (let [my-set :- (Set Kw), #{:a :b :c}
        my-map  :- (Map Kw Kw) {:key :val}
        kw-inputs :- (Set (Option Kw)), (s/intersection @my-ref my-set)
        candidate-kw-outputs :- (Seq (Option Kw)), (for [kw :- (Option Kw), kw-inputs] :- (Option Kw)
                                                                                  (my-map kw))]
    (for [kw :- (Option Kw), candidate-kw-outputs] :- (Seq (Option Kw))
      (some #{kw} (find-availables-fn)))))

 
I have tried placing the args to 'some' in type-declared let bindings, defining 'candidate-kw-outputs' at the top level and even instantiating 'some' and using the instance in place of the actual function, perhaps I am not doing this correctly but none of these avenues have solved my problem and some of them have caused new errors to spring up. Heuristic advice on how to troubleshoot when developing in core.typed would really be appreciated. I am learning a lot but am spending hours each time I hit a problem like this. Thanks in advance!

Ambrose Bonnaire-Sergeant

unread,
Dec 13, 2014, 2:03:29 PM12/13/14
to core.typed
I suspect sets don't work very well as functions yet. I recommend calling `some` like this for now:

(some (fn [a] (#{kw} a)) (find-availables-fn))

Thanks
Ambrose

Jesse

unread,
Dec 13, 2014, 3:29:57 PM12/13/14
to clojure-c...@googlegroups.com
This was the right way to go. I also had to annotate the 'a' param to take care of the nil case. I like that I can annotate anonymous functions inline in the body and use the :- syntax almost everywhere.

Cheers,
Jesse
Reply all
Reply to author
Forward
0 new messages