Hi all. For Clojure 1.13 (since it’s the answer after thinking on the “Maybe not” talk), how may I say something that is optional, but if present it must have a shape? Example (from the Maybe not talk, a user may have an address, but if it does, it better have the zip code):
{:user {}} ; fine
{:user {:address {:zip "1234"}} ; fine
{:user {:address {}} ;Not ok
As far as I can see the only way would be with when-let or something like that.
(let [{:keys! [user]} my-user-map
{:keys [address]} user]
(if-let [{:keys! [zip]} address]
; Address there, with zip
; Address not there))
It depends on what you need bound, but you can say:
(let [{:req! {{{:keys! [zip]} :address} :user}} my-user-map]
...)Oh, you want the structure to be optional? Yeah, then you have to have two destructurings, sorry.
user=> (let [{:req! {{{:keys! [zip]} :address} :user}} {}] zip)
Execution error (IllegalArgumentException) at user/eval130 (REPL:1).
Missing required key: :user
user=> (let [{:req! {{{:keys! [zip]} :address} :user}} {:user {}}] zip)
Execution error (IllegalArgumentException) at user/eval138 (REPL:1).
Missing required key: :zip
user=> (let [{:req! {{{:keys! [zip]} :address} :user}} {:user {:address {}}}] zip)
Execution error (IllegalArgumentException) at user/eval146 (REPL:1).
Missing required key: :zip
user=> (let [{:req! {{{:keys! [zip]} :address} :user}} {:user {:address {:zip "12345"}}}] zip)
"12345"So, yes, in your case you need almost what you had, and you’ll get an exception from the let if :user is not present, then an exception from the if-let if :zip isn’t present in address (but it’ll be fine if address isn’t present—the if-let will eval the else branch).
Can you be a little more explicit about exactly what you’re trying to do?
If you don’t need user bound, you can short-circuit that a bit:
user=> (let [{:req! {{:keys [address]} :user}} {}] (if-let [{:keys! [zip]} address] zip :no-zip))
Execution error (IllegalArgumentException) at user/eval180 (REPL:1).
Missing required key: :user
user=> (let [{:req! {{:keys [address]} :user}} {:user {}}] (if-let [{:keys! [zip]} address] zip :no-zip))
:no-zip
user=> (let [{:req! {{:keys [address]} :user}} {:user {:address {}}}] (if-let [{:keys! [zip]} address] zip :no-zip))
Execution error (IllegalArgumentException) at user/eval198 (REPL:1).
Missing required key: :zip
user=> (let [{:req! {{:keys [address]} :user}} {:user {:address {:zip "44515"}}}] (if-let [{:keys! [zip]} address] zip :no-zip))
"44515"The idea (again, from Maybe Not talk), is the second case you showed.
(let [{:req! {{{:keys! [zip]} :address} :user}} {:user {}}] zip)
I would need a way to express this as a valid input. And the if-let solution becomes weird if I have multiple cases like that. Imagine I can have :address, and :secondary-address, same spec for both. Now I would need different validations for those.I wouldn’t expect the destructuring syntax to support “optional” tho’ for your case.
I mean, yes, you need a conditional for each optional situation that has a distinct structure, which seems to be what you’re asking.
> how may I say something that is optional, but if present it must have a shape? Yes I think logically it would be the part of the code that requires it to have a shape, that would specify it.
If you don’t need it in that part of code, you convey it along
The idea I understood from 1.13 / maybe not is you want to specify what you select from your aggregates at the point of use
“there is no such thing as a maybe sheep”
our experience from spec 2 select is that it’s a lot to cover both required/optional conditionality and nesting. the direction of the destructuring stuff is to state requirements in context rather than in one place, and you could think of this situation as having conditional context. address is optional. In the condition where it occurs, you destructure again and state the new requirements for the nested structure.
if you have multiple addresses to validate in the nested context, you could also define a selector for that and reuse it
point_up::skin-tone-2 Yes, the new selector macro can make your destructurings reusable, which is very nice!
Spec1 does this well already no? Just define a spec for address key and combine validation with spec1 ?
yes, and future work to come there in Clojure
Thanks for the answers! Yes, spec can handle this, but since Clojure 1.13 was the answer after thinking about “Maybe not”, I was curious about the answer when something is optional but has a shape.
I've been wondering if the thought process is still similar. The spec can focus on describing the shape of things when present. And now the selection/destructing can describe the optionality of things.
If you work with qualified keys, you can use destructuring for the shape and some optionality, and you can use Spec for the “types” of the attributes and ignore the shape almost completely.
If you destructure with :select, you can then ask (s/valid? (s/keys) selected-data) and it will check each attribute in the map, because Spec checks all keys against s/def attribute specs (and you don’t need to Spec the “shape” so this is more like RDF-style (I think?) and how Datomic’s schema works).
> Also I’m wondering how other Clojurist look onto the dynamic typing in Clojure. I feel like a language should translate low level abstractions into high level abstractions for you. And when we say a language is dynamically typed, we’re saying there’s some wrapper object that can wrap a lot of other objects and then how you deal with those wrapped objects can differ at runtime depending on what’s in that wrapper, right? And “non-dynamic” languages have those wrappers too, but in “dynamic languages” that wrapper object is just like the main thing. But in both types of languages you can still do both things. Really all we’re talking about is how much the language makes it easy to make that wrapper be the default interface in the language, right? Seems like most modern langs provide a bit of both anyway and you can squeeze both out of both.
And I’d rather squeeze static behavior out of a dynamic language than dynamic behavior out of a static language, just in terms of what problems I’d rather focus on.
But for lower level problems that require working with the hardware more directly that’s sometimes not an option.
OK, thank you for the response. I guess I come from another perspective. To me software delivers it’s value while it’s running. The value itself is created by delivering information to the user. For me information is language and a major capability of language is it’s ability to deal with learning. Learning implies change, in the case of software this is change of type. If the programming language is not dynamically typed, you have to decide upfront for every type if it’s dynamic or not. Otherwise you block the learning. This is just my view. It all depends on how you look at information in the context of software. Is information a language or a set of values to fill set formulas. I guess my RDF background plays a role here too.
Is there a predicate to check if a value supports contains?. I don’t see one, but just double checking.
I’m writing a DSL for specifying predicates. I want set literals to translate to predicates that require that the object contains the items. Eg.
(->pred #{:a :b :c})
will return true for {:a 42 :b 42 :c 42} and #{:a :b :c }No predicate. And I’ve seen some variations of contains?-safe out there that wrap the call in try-catch.
I just followed the pattern from seqable? to match https://github.com/clojure/clojure/blob/33fb064dee4cf8decdae71ed93f1a6262f7a4c28/src/jvm/clojure/lang/RT.java#L825
(defn containable?
"Returns true if an object supports contains?"
[x]
(or (nil? x)
(associative? x)
(set? x)
(instance? java.util.Map x)
(instance? java.util.Set x)
(string? x)
(-> x Object/.getClass .isArray)
(instance? clojure.lang.ITransientSet x)
(instance? clojure.lang.ITransientAssociative2 x)))
I’m not enamored with the name, but I can’t think of a better oneobviously, I’ll have to update it when clojure.lang.ITransientAssociative3 is released
oh. duh picard-facepalm
I think quite a few people would say that any collection is a container. But lists aren’t “containable”, at the very least.
fair point
containser?
I’d go for a non-clever supports-contains?. :) Maybe a double ? if I’d feel like it.
the worst part is that I need to call this function from a macro, so I can’t hide the shame of my poor naming skills
actually, it’s even trickier:
(contains? "a" 0)
;; true
(contains? "a" :foo)
;; java.lang.IllegalArgumentException
;; contains? not supported on type: java.lang.String
Strings and arrays can only contain numbersOhhh, right, same with (contains? (sorted-map :a 1) "b").
More and more reasons for that try-catch approach.
@smith.adriane I think the edge case there hints that the predicate name should have something to do with the fact contains? deals with checking whether there is a value that is indexable by the given key. For lists and arrays, you can only get a value in that way by the numerical index value instead of an arbitrary key value.
Perhaps indexable?, and short circuit on the arg types before calling contains? on them if there is a particular need/desire to avoid a try-catch.
Since anyone can implement the various interfaces that contains? use, there’s no alternative to try/catch that will work for an arbitrary input (which is important for my use case).