This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-07-04
Channels
- # announcements (10)
- # asami (6)
- # babashka (22)
- # beginners (44)
- # biff (1)
- # calva (8)
- # clj-kondo (13)
- # clojure (62)
- # clojure-art (1)
- # clojure-europe (27)
- # clojure-nl (1)
- # clojure-norway (19)
- # clojure-spec (19)
- # clojure-uk (2)
- # component (29)
- # datascript (1)
- # fulcro (9)
- # gratitude (2)
- # kaocha (6)
- # klipse (1)
- # luminus (16)
- # malli (9)
- # nbb (5)
- # off-topic (4)
- # reagent (5)
- # shadow-cljs (85)
- # spacemacs (1)
- # tools-deps (10)
- # vim (9)
- # xtdb (2)
Does malli have anything equivalent to Union
in Typed Racket?
https://docs.racket-lang.org/ts-guide/beginning.html
#lang typed/racket
(define-type Tree (U leaf node))
(struct leaf ([val : Number]))
(struct node ([left : Tree] [right : Tree]))
It is the same concept with F#'s discriminated union.
I read malli documentation carefully but couldn't find.Or :multi
which has support for discriminators via :dispatch
https://github.com/metosin/malli#multi-schemas
@UK0810AQ2 I believe you can say that.
multi
can dispatch to the correct schema which would make it discriminating. If you don't have a way of doing that, you can use or
or orn
, which is a named union
I'm still trying to spec clojure.core with malli https://github.com/clojure/core.specs.alpha/blob/master/src/main/clojure/clojure/core/specs/alpha.clj#L39
I'm trying to find a way to replace this every
which specs a map as 3 different kinds of key/values.
unfortunately Malli map-of takes only one kind of key
I'm wondering if there is a way to transform the values in the schema, so that it checks that it's a map then transforms in a repetitions of tuples.
Is it possible with Malli? going through the docs I can't find any clear way
I think I found my solution in malli source https://github.com/metosin/malli/blob/2398df55ee806e25592fabf4d0c642ee3a2b233f/src/malli/destructure.cljc#L19
Is it expected that the output of m/parse
can be lossy?
(defn -map-like? [x] (or (map? x) (and (seqable? x) (every? (fn [e] (and (vector? e) (= 2 (count e)))) x))))
(def MapLike (m/-collection-schema {:type 'MapLike, :empty {}, :pred -map-like?}))
(m/parse [MapLike [:orn
[:some-keyword [:tuple :keyword :any]]
[:some-int [:tuple :int :any]]]]
{:k1 1
:k2 2
3 4
5 :six})
{:some-keyword [:k2 2], :some-int [5 :six]}
In this snippet above one can see that because the output of parse is a map, keys of the same tuple are overwritten