4 ms·
Optics allow us to do something like this: import optics.for as o_for o_for company.staff.pets as (.{pet}, optics.parentNode as owner) where (pet.type == "
by AnthonBerg 6y ago
Optics allow us to do something like this:
import optics.for as o_for
o_for company.staff.pets as (.{pet}, optics.parentNode as owner) where (pet.type == "cat"):
print "{.name|%s} belongs to {owner.name|%s}")
We're guaranteed to be safe against null values in company and staff and pets – we don't have to think about it – and no "for" or "if" is required for walking down the structure. Also the types of pet and owner are completely certain, and we can give verified compile-time guarantees that the string we're populating is 100% correct to match the input.
This is the very simplest use of optics. Optics also allow us to do express wild things over those structures, such as selecting nodes based on some defined statistical relationship to the statistics of defined parts of the whole structure. It's as simple and concise as the example above. And as efficient as possible.