Class CollectionPatterns

java.lang.Object
eu.bandm.tools.paisley.CollectionPatterns

public final class CollectionPatterns extends Object
Static factory methods for creating patterns for collections.
See Also:
  • Method Details

    • anyElement

      public static <A> Pattern<Iterable<? extends A>> anyElement(Pattern<? super A> p)
      Returns a pattern that matches a given pattern for any element of an Iterable successively. Matches are effected exhaustively for each element returned by the java.lang.Iterable's Iterator before proceeding to the next element.
      Parameters:
      p - the pattern to match against the elements of the Iterable.
      See Also:
    • anyElement

      public static <A> Motif<A,Iterable<? extends A>> anyElement()
    • firstElement

      public static <A> Pattern<Iterable<? extends A>> firstElement(Pattern<? super A> p)
      Returns a pattern that matches a given pattern for the first matching element of an Iterable. Elements returned by the Iterable's java.util.Iterator are tried in order until one has at least one match; matches for that element are effected exhaustively and only.
      Parameters:
      p - the pattern to match against the elements of the Iterable.
      See Also:
    • anyArrayElement

      public static <A> Pattern<A[]> anyArrayElement(Pattern<? super A> p)
      Returns a pattern that matches a given pattern for any element of an array successively. Matches are effected exhaustively for each element of the array before proceeding to the next element.
      Parameters:
      p - the pattern to match against the elements of the array.
      See Also:
    • firstArrayElement

      public static <A> Pattern<A[]> firstArrayElement(Pattern<? super A> p)
      Returns a pattern that matches a given pattern for the first matching element of an array. Elements of the array are tried in order until one has at least one match; matches for that element are effected exhaustively and only.
      Parameters:
      p - the pattern to match against the elements of the array.
      See Also:
    • keySet

      public static <A, B> Pattern<Map<A,B>> keySet(Pattern<? super Set<? extends A>> p)
      Returns a pattern that matches a given pattern against the key set of a map.
      Parameters:
      p - the pattern to match against the key set.
      See Also:
    • values

      public static <A, B> Pattern<Map<A,B>> values(Pattern<? super Collection<? extends B>> p)
      Returns a pattern that matches a given pattern against the values of a map.
      Type Parameters:
      A - the type of the keys in the map
      B - the type of the values in the map
      Parameters:
      p - the pattern to match against the values.
      See Also:
    • size

      public static <A> Pattern<Collection<? extends A>> size(Pattern<? super Integer> body)
      Returns a pattern that matches a given pattern against the size of a Collection.
      Parameters:
      body - the pattern to match against the size.
      See Also:
    • get

      public static <A> Pattern<List<? extends A>> get(int index, Pattern<? super A> body)
      Returns a pattern that matches a given pattern against the element at a given index in a List.

      The failure behaviour of the resulting pattern reflects that of List.get(int): if the given index is invalid for a target list, an IndexOutOfBoundsException is thrown during matching.

      Parameters:
      index - the index of the element to select.
      body - the pattern to match against the selected element.
      See Also:
    • get

      public static <A, B> Pattern<Map<A,B>> get(A key, Pattern<? super B> body)
      Returns a pattern that matches a given pattern against the element for a given key in a Map.

      The failure behaviour of the resulting pattern reflects that of Map.get(java.lang.Object): if the given key is not contained in a target list, the body pattern is matched against null.

      Parameters:
      key - the key of the element to select.
      body - the pattern to match against the selected element.
      See Also:
    • length

      public static <A> Pattern<A[]> length(Pattern<? super Integer> body)
      Returns a pattern that matches a given pattern against the length of an array.
      Parameters:
      body - the pattern to match against the length.
    • getArray

      public static <A> Pattern<A[]> getArray(int index, Pattern<? super A> body)
      Returns a pattern that matches a given pattern against the element at a given index in an array.

      The failure behaviour of the resulting pattern reflects that of array selection: if the given index is invalid for a target array, an ArrayIndexOutOfBoundsException is thrown during matching.

      Parameters:
      index - the index of the element to select.
      body - the pattern to match against the selected element.
    • elementwise

      public static <A> Pattern<List<? extends A>> elementwise(List<? extends Pattern<? super A>> ps)
      Returns a pattern that matches a given list of patterns element-by-element against a list. The first pattern is matched against the first list element and so forth. A successful match requires the size of the target list to match the number of given patterns exactly. Multiple matches for individual patterns are combined as with Pattern.all(Pattern...).
      Parameters:
      ps - a list of patterns to match against list elements.
    • elementwise

      public static <A> Pattern<A[]> elementwise(Pattern<? super A>... ps)
      Returns a pattern that matches a given array of patterns element-by-element against an array. The first pattern is matched against the first array element and so forth. A successful match requires the length of the target array to match the number of given patterns exactly. Multiple matches for individual patterns are combined as with Pattern.all(Pattern...).
      Parameters:
      ps - an array of patterns to match against array elements.
    • from

      public static <A> Pattern<A> from(Set<? super A> set)
      Returns a pattern that matches against elements from a given set.

      A pattern returned by this method is deterministic and binds no variables.

      Parameters:
      set - the set of elements to match.
      See Also:
    • singleton

      public static <A> Pattern<? super Collection<? extends A>> singleton(Pattern<? super A> ps)
      Returns a pattern that matches a given pattern against the (single) element of a list. A successful match requires the target list to have size one exactly.
      Parameters:
      ps - patterns to match against the list element.
    • nil

      public static <A> Pattern<? super List<? extends A>> nil()
    • cons

      public static <A> Pattern<? super List<? extends A>> cons(Pattern<? super A> first, Pattern<? super List<? extends A>> rest)
    • snoc

      public static <A> Pattern<? super List<? extends A>> snoc(Pattern<? super List<? extends A>> front, Pattern<? super A> last)
    • map

      public static <A, B> Motif<List<? extends A>,List<? extends B>> map(Motif<A,B> m)