I want to check that there is a certain thing at a certain position in a nested list structure, but I don’t know the shape of the structure at the beginning. How can I check for the presence of the thing without a risk of raising an exception?
For example: :thing
is supposed to be three levels deep at the beginnings, like this:
(def st '(((:thing))))
I can reach it with (-> st first first first)
. but, if the list structure is not deep enough, it raises an exception.
How can I effectively check for its presence? My solution so far is this:
(if (list? st)
(let [in1 (first st)]
(if (list? in1)
(let [in2 (first in1)]
(if (list? in2)
(= :thing (first in2))
false))
false))
false)
It is ugly and prone to errors, so I would like to have a better solution. IMO, the biggest hurdle is that first
throws an exception if it gets not a sequence. So one way would be to have something which behaves like first
but does not throw an exception; one way would be to catch the exception like this:
(try (= :thing (-> st first first first))
(catch IllegalArgumentException _
false))
But I would rather avoid exceptions, so I would like to know if there is a good way without them.
This question falls into the general (and popular) category of “My data is a mess; how do I work around it?” There are two basic approaches:
- Figure out how to work around it
- Prevent it from becoming a mess in the first place
Both can be viable depending on your circumstances. People usually frame the question as (1), as you’ve done; but I urge you to consider (2) first. It is usually not that hard, and people don’t usually even try it because their immediate “next” problem is how to work with the data as it is now, i.e. already a mess.
To work around messy data, you need to more carefully define what kinds of messes you’re expecting, because there is no one-size-fits-all answer to “somewhere in a sea of parentheses, there might be some data”. For example, if for some reason your data is always a nested list of depth at most 3 and each list has at most 1 element and the deepest-nested non-list is a keyword, then the approach you’ve laid out will work fine, although it could be made more convenient. But if any of those assumptions are wrong, you’ll need to handle exceptions more carefully. Do you need to handle (foo (:thing))
, for example? Or ({:mode :thing})
? The permutations are endless, and you can’t solve the problem without defining it first.
Preventing your data from becoming a mess requires a different approach. How did you get here? Surely you didn’t read a text file containing the string (((:thing)))
. For example, likely you got some data from somewhere, which probably was in a format that’s at least halfway reasonable, and then you transformed it somehow, and as a result you accidentally wrapped it in too many lists. If something like this happened, go back and try to work out how not to do this. This may lead to asking a new Question about the right way to transform your data.