Comment

Comment by Lammy

Pattern matching gets even more fun with the rightward-assignment syntax and additional conditionals. Here's a single example case from the UUID/GUID library I'm working on where the constructor method takes arbitrary positional arguments, many combinations of which may yield a valid UUID. This particular case matches the components of a Microsoft-style GUID so it can handle the endianness appropriately:

  in [::Integer => data1, ::Integer => data2, ::Integer => data3, ::Array => data4] if (
    data1.bit_length.<=(32) and data2.bit_length.<=(16) and data3.bit_length.<=(16) and (
      data4.size.eql?(8) and data4.all?(&::Integer::method(:===)) and data4.max.bit_length.<=(8)
    )
  ) then
parent Lammy 3 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton