How can I reach into a nested list structure without raising an exception in Clojure?

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:

  1. Figure out how to work around it
  2. 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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật