clojure-spec 2023-07-12

How would I spec an higher order function such as map. The function parameter passed in should validate against

(s/def ::f (s/fspec :args (s/cat any?) :ret any?))
but if I try to validate (valid? ::f (fn [i] (* i 2))) then this fails because it will generate non numerical values according to the above spec.

I would put a runtime guard in the body of the function.

Return nil when wrong input

Pretty sure this is how I handled this in the past

You can replace your fspec with fn? or ifn?

yes, I can but then I don’t check that the signature/shape matches. The real signature is something like this:

(s/fspec :args (s/cat :state any?) :res (s/cat :state any :result any?))
I always want to get a vector with 2 or more elements back.

This would be easy with static typing 🙂

Spec is not a type system.

I know, it’s much more than a type system! This is the first thing I did find that would be easier with a static type system. That’s why I mentioned it.