Fork me on GitHub
#clojure-spec
<
2023-07-12
>
Joerg Schmuecker07:07:11

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.

colinkahn13:07:32

You can replace your fspec with fn? or ifn?

Joerg Schmuecker13:07:43

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.

Joerg Schmuecker22:07:39

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.

johanatan23:07:27

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

johanatan23:07:37

Return nil when wrong input

johanatan23:07:10

Pretty sure this is how I handled this in the past