Tom Lieber's Microblog

Oct 28, 2023

I’m working on what feels like my lispiest Mathematica project. One of the pieces I wanted was to be able to search a list of associations by pattern, like:

It turns out to be pretty simple to make a little API like that because pattern-matching is common in Mathematica so all the necessary pieces are right there, waiting to be assembled:

(* Plain search *)
findAll[o_, {pattern__}] := 
  Cases[o, <|OrderlessPatternSequence[PatternSequence[pattern], ___]|>];

(* Search and map *)
findAll[o_, {pattern__} :> replacement_] := 
  Cases[o, <|
     OrderlessPatternSequence[PatternSequence[pattern], ___]|> :> 
    replacement];