具有不确定键数的映射向量的 Clojure 规范
Clojure spec for vector of map with indeterminate number of keys
我正在尝试为将以下两个映射作为参数的函数编写 clojure 规范。
{1 BL0-MD0-SU, 0 BL0-SM0-SU}
{0 [https://gateway.pinata.cloud/ipfs/QmcLjsUDHVuy8GPUPmj4ZdGa3FWTfGnsKhWZ4dxqUaGGXk/doge.jpg ], 1 [https://gateway.pinata.cloud/ipfs/QmcLjsUDHVuy8GPUPmj4ZdGa3FWTfGnsKhWZ4dxqUaGGXk/doge.jpg ]}
每张地图可以有不同的长度,这取决于用户希望购买多少件 T 恤。我不确定如何为此编写规范,允许每个映射中的键和值的数量发生变化。如果我可以断言每个映射都包含相同数量的键,那也会很有用。
编辑:
通过以下条件时,规范也应通过:
{1 BL0-MD0-SU, 0 BL0-SM0-SU, 2 BL0-LG0-SU}
{0 [https://gateway.pinata.cloud/ipfs/QmcLjsUDHVuy8GPUPmj4ZdGa3FWTfGnsKhWZ4dxqUaGGXk/doge.jpg ], 1 [https://gateway.pinata.cloud/ipfs/QmcLjsUDHVuy8GPUPmj4ZdGa3FWTfGnsKhWZ4dxqUaGGXk/doge.jpg ], 2 [ ]}
(s/def ::arg1 (s/map-of long? string?))
(s/def ::arg2 (s/map-of long? vector?))
(s/def ::args (s/and (s/cat :arg1 ::arg1 :arg2 ::arg2)
(fn [{:keys [arg1 arg2]}]
(= (count arg1) (count arg2)))))
如果您需要将此规范附加到函数,您可以在此处了解如何操作:https://clojure.org/guides/spec#_specing_functions
我正在尝试为将以下两个映射作为参数的函数编写 clojure 规范。
{1 BL0-MD0-SU, 0 BL0-SM0-SU}
{0 [https://gateway.pinata.cloud/ipfs/QmcLjsUDHVuy8GPUPmj4ZdGa3FWTfGnsKhWZ4dxqUaGGXk/doge.jpg ], 1 [https://gateway.pinata.cloud/ipfs/QmcLjsUDHVuy8GPUPmj4ZdGa3FWTfGnsKhWZ4dxqUaGGXk/doge.jpg ]}
每张地图可以有不同的长度,这取决于用户希望购买多少件 T 恤。我不确定如何为此编写规范,允许每个映射中的键和值的数量发生变化。如果我可以断言每个映射都包含相同数量的键,那也会很有用。
编辑:
通过以下条件时,规范也应通过:
{1 BL0-MD0-SU, 0 BL0-SM0-SU, 2 BL0-LG0-SU}
{0 [https://gateway.pinata.cloud/ipfs/QmcLjsUDHVuy8GPUPmj4ZdGa3FWTfGnsKhWZ4dxqUaGGXk/doge.jpg ], 1 [https://gateway.pinata.cloud/ipfs/QmcLjsUDHVuy8GPUPmj4ZdGa3FWTfGnsKhWZ4dxqUaGGXk/doge.jpg ], 2 [ ]}
(s/def ::arg1 (s/map-of long? string?))
(s/def ::arg2 (s/map-of long? vector?))
(s/def ::args (s/and (s/cat :arg1 ::arg1 :arg2 ::arg2)
(fn [{:keys [arg1 arg2]}]
(= (count arg1) (count arg2)))))
如果您需要将此规范附加到函数,您可以在此处了解如何操作:https://clojure.org/guides/spec#_specing_functions