clojure-spec

Joerg Schmuecker 2023-07-12T07:39:11.052589Z

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.

johanatan 2023-07-28T23:11:27.538869Z

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

johanatan 2023-07-28T23:11:37.755649Z

Return nil when wrong input

johanatan 2023-07-28T23:12:10.847629Z

Pretty sure this is how I handled this in the past

2023-07-12T13:01:32.798519Z

You can replace your fspec with fn? or ifn?

Joerg Schmuecker 2023-07-12T13:48:43.593519Z

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 Schmuecker 2023-07-12T13:50:51.134979Z

This would be easy with static typing 🙂

seancorfield 2023-07-12T16:43:26.009229Z

Spec is not a type system.

Joerg Schmuecker 2023-07-12T22:49:39.166329Z

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.