4 ms·
In Clojure you generally accept any kind of map, do some operation and return 'copy' of that map, or pass it on. A simple example is a ring web stack, where re
by nickik 7y ago
In Clojure you generally accept any kind of map, do some operation and return 'copy' of that map, or pass it on.
A simple example is a ring web stack, where request flow threw different functions that transform the http request.
Each function just assumes the keys its needed are there, or to do input validation, you just validate that the incoming map has the keys that you require but does not care what else is in there. Clojure Spec will also do validation of the value in that key, even if it is an arbitrary complex map.
What this gets you is that you can pass around generic structures, that can be used with all standard Clojure functions and most libraries, validation is applied as needed on those keys that you need.
- yakshaving_jgt 7y agoThere is no reason why the same would not be possible in a statically-typed language.
- youerbt 7y agoBut I believe this is also with the spirit of the article. Your functions still assume something about input and are polymorphic over everything else in it. AFAIK this can also be done in a type safe manner in languages which support extensible records/row polymorphism.
- yakshaving_jgt 7y agoYou don't need row polymorphism to write a function which operates on a collection of key/value pairs and expects a specific key to exist at runtime. This works in Haskell, just as it does in Clojure.
- wereHamster 7y agoPlease write the Haskell type signature of such a function, as an example.
- yakshaving_jgt 7y agof :: Map String String -> Map String String