Package ch.rfin.util

Class Pairs

java.lang.Object
ch.rfin.util.Pairs

public final class Pairs
extends java.lang.Object
Utilities for working with pairs.

Collections

  • pairFrom(Map.Entry)
  • pairFrom(Iterable)
  • pairsFrom(Map.Entry)
  • pairsFrom(Iterable)
  • toMap(Iterable)
Streams

  • stream(Iterable)
  • stream(Map)
  • streamAdjacent(Iterable)
  • zip(Iterable, Iterable)
  • pairsToMap()
Functions

  • map_1(Function)
  • map_2(Function)
  • map(Function, Function)
  • test_1(Predicate)
  • test_2(Predicate)
  • withId(Function)
  • function(BiFunction)
  • biFunction(Function)
  • predicate(biPredicate)
  • biPredicate(Predicate)
  • consumer(biConsumer)
  • biConsumer(Consumer)
Misc

  • compare(Pair, Pair)
  • comparator()

All methods that construct streams from iterables do so lazily, and so are compatible with infinite iterables.

Documentation conventions: […] represents a collection of values (such as a List, a Collection, an Iterable, a Stream). (a,b) represents a pair. (k:v) represents a map entry.

  • Method Summary

    Modifier and Type Method Description
    static <T1,​ T2> java.util.function.BiConsumer<T1,​T2> biConsumer​(java.util.function.Consumer<Pair<T1,​T2>> f)
    (f: (x,y) → _) → (g: x,y → _) - Converts a unary consumer of pairs to a binary consumer.
    static <T1,​ T2,​ R> java.util.function.BiFunction<T1,​T2,​R> biFunction​(java.util.function.Function<Pair<T1,​T2>,​? extends R> f)
    (f: (x,y) → z) → (g: x,y → z) - Converts a unary function from pairs to a binary function.
    static <T1,​ T2> java.util.function.BiPredicate<T1,​T2> biPredicate​(java.util.function.Predicate<Pair<T1,​T2>> f)
    (f: (x,y) → T/F) → (g: x,y → T/F) - Converts a unary predicate from pairs to a binary predicate.
    static <T1 extends java.lang.Comparable<? super T1>,​ T2 extends java.lang.Comparable<? super T2>>
    java.util.Comparator<Pair<T1,​T2>>
    comparator()
    Returns a Comparator that compares pairs lexicographically.
    static <T1 extends java.lang.Comparable<? super T1>,​ T2 extends java.lang.Comparable<? super T2>>
    int
    compare​(Pair<T1,​T2> p1, Pair<T1,​T2> p2)
    Compare two pairs lexicographically.
    static <T1,​ T2> java.util.function.Consumer<Pair<T1,​T2>> consumer​(java.util.function.BiConsumer<? super T1,​? super T2> f)
    (f: x,y → _) → (g: (x,y) → _) - Converts a binary consumer to a consumer that is unary for pairs.
    static <T> java.util.stream.Stream<Pair<java.lang.Integer,​T>> enumerate​(java.lang.Iterable<T> xs)
    [x₀, x₁, x₂, …] → [(0,x₀), (1,x₁), (2,x₂), …] - Converts an iterable to a stream of pairs where the first value is the index of the value.
    static <T1,​ T2,​ R> java.util.function.Function<Pair<T1,​T2>,​R> function​(java.util.function.BiFunction<? super T1,​? super T2,​? extends R> f)
    (f: x,y → z) → (g: (x,y) → z) - Converts a binary function to a function that is unary for pairs.
    static <T1,​ T2,​ R,​ U>
    java.util.function.Function<Pair<T1,​T2>,​Pair<R,​U>>
    map​(java.util.function.Function<? super T1,​? extends R> f, java.util.function.Function<? super T2,​? extends U> g)
    (f: x → y), (g: u → v) → (h: (x,u) → (f(x),g(u))) - Returns a function for mapping both elements in a pair.
    static <T1,​ T2,​ R> java.util.function.Function<Pair<T1,​T2>,​Pair<R,​T2>> map_1​(java.util.function.Function<? super T1,​? extends R> f)
    (f: x → y) → (g: (x,y) → (f(x),y)) - Returns a function for mapping the first element in a pair.
    static <T1,​ T2,​ R> java.util.function.Function<Pair<T1,​T2>,​Pair<T1,​R>> map_2​(java.util.function.Function<? super T2,​? extends R> f)
    (f: x → y) → (g: (x,y) → (x,f(y))) - Returns a function for mapping the second element in a pair.
    static <T> Pair<T,​T> pairFrom​(java.util.List<T> xs)
    [x₁, x₂, …] → (x₁, x₂) - converts a list to a pair.
    static <K,​ V> Pair<K,​V> pairFrom​(java.util.Map.Entry<K,​V> entry)
    (k:v) → (k,v) - converts a Map.Entry to a key-value pair.
    static <T> java.util.List<Pair<T,​T>> pairs​(java.util.List<T> items)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Since 1.1.0.
    static <K,​ V> java.util.List<Pair<K,​V>> pairs​(java.util.Map<K,​V> items)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Since 1.1.0.
    static <T> java.util.List<Pair<T,​T>> pairsFrom​(java.lang.Iterable<T> xs)
    Converts an iterable to a list of pairs.
    static <K,​ V> java.util.List<Pair<K,​V>> pairsFrom​(java.util.Map<K,​V> map)
    Converts a Map to a list of pairs.
    static <T1,​ T2> java.util.stream.Collector<Pair<T1,​T2>,​?,​java.util.Map<T1,​T2>> pairsToMap()
    Returns a Collector that can collect a stream of pairs into a Map.
    static <T1,​ T2> java.util.function.Function<T2,​Pair<T1,​T2>> pairWithFirst​(T1 first)
    a → (b → (a,b)) - Takes an item and returns a function that will take a second item and return a pair with the parameter to this function first and the parameter to the returned function second.
    static <T1,​ T2> java.util.function.Function<T1,​Pair<T1,​T2>> pairWithSecond​(T2 second)
    b → (a → (a,b)) - Takes an item and returns a function that will take a second item and return a pair with the parameter to this function second and the parameter to the returned function first.
    static <T1,​ T2> java.util.function.Predicate<Pair<T1,​T2>> predicate​(java.util.function.BiPredicate<? super T1,​? super T2> f)
    (f: x,y → T/F) → (g: (x,y) → T/F) - Converts a binary predicate to a predicate that is unary for pairs.
    static <T> java.util.stream.Stream<Pair<T,​T>> stream​(java.lang.Iterable<T> xs)
    [x₀, x₁, x₂, …] → [(x₀,x₁), (x₂,x₃), (x₄,x₅), …] - Converts an iterable of values to a stream of pairs, consuming two at a time.
    static <K,​ V> java.util.stream.Stream<Pair<K,​V>> stream​(java.util.Map<K,​V> map)
    [(k₀:v₀), (k₁:v₁), (k₂:v₂), …] → [(k₀,v₀), (k₁,v₁), (k₂,v₂), …] - Converts a Map to a stream of key-value pairs.
    static <T> java.util.stream.Stream<Pair<T,​T>> streamAdjacent​(java.lang.Iterable<T> xs)
    [x₀, x₁, x₂, …] → [(x₀,x₁), (x₁,x₂), (x₂,x₃), …] - Converts an iterable to a stream of pairs of adjacent values.
    static <T1,​ T2> java.util.function.Predicate<Pair<T1,​T2>> test_1​(java.util.function.Predicate<? super T1> f)
    (f: x → T/F) → (g: (x,y) → (f(x),y)) - Returns a predicate for testing the first element in a pair.
    static <T1,​ T2> java.util.function.Predicate<Pair<T1,​T2>> test_2​(java.util.function.Predicate<? super T2> f)
    (f: x → T/F) → (g: (x,y) → (x,f(y))) - Returns a predicate for testing the second element in a pair.
    static <K,​ V> java.util.Map<K,​V> toMap​(java.lang.Iterable<Pair<K,​V>> items)
    [(k₀,v₀), (k₁,v₁), (k₂,v₂), …] → [(k₀:v₀), (k₁:v₁), (k₂:v₂), …] - Converts pairs to a Map.
    static <T,​ R> java.util.function.Function<T,​Pair<T,​R>> withId​(java.util.function.Function<? super T,​? extends R> f)
    (f: x → y) → (g: x → (x,y)) - Takes a function and returns a function to pairs, where the first element is the original input and the second element is the output.
    static <T1,​ T2> java.util.stream.Stream<Pair<T1,​T2>> zip​(java.lang.Iterable<T1> xs, java.lang.Iterable<T2> ys)
    ([x₀, x₁, x₂, …], [y₀, y₁, y₂, …]) → [(x₀,y₀), (x₁,y₁), (x₂,y₂), …] - like zip(xs, ys) in Python.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • pairFrom

      public static <K,​ V> Pair<K,​V> pairFrom​(java.util.Map.Entry<K,​V> entry)
      (k:v) → (k,v) - converts a Map.Entry to a key-value pair.
      Parameters:
      entry - Map.Entry (k:v).
      Returns:
      key-value pair (k,v).
    • pairFrom

      public static <T> Pair<T,​T> pairFrom​(java.util.List<T> xs)
      [x₁, x₂, …] → (x₁, x₂) - converts a list to a pair. Ignores any elements after the first two.
      Parameters:
      xs - list of values [x₁, x₂, …].
      Returns:
      a pair (x₁, x₂).
    • pairs

      @Deprecated(forRemoval=true) public static <T> java.util.List<Pair<T,​T>> pairs​(java.util.List<T> items)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Since 1.1.0. Use pairsFrom(Iterable) instead.
      Converts a list to a list of pairs.
      Returns:
      [(x0, x1), (x2, x3), …].
      Throws:
      java.lang.IllegalArgumentException - if uneven number of items
    • pairsFrom

      public static <T> java.util.List<Pair<T,​T>> pairsFrom​(java.lang.Iterable<T> xs)
      Converts an iterable to a list of pairs.
      Returns:
      [(x₀, x₁), (x₂, x₃), …].
      Since:
      1.1.0
    • pairs

      @Deprecated(forRemoval=true) public static <K,​ V> java.util.List<Pair<K,​V>> pairs​(java.util.Map<K,​V> items)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Since 1.1.0.
      Converts a Map to a list of pairs.
      Returns:
      [(x₀, x₁), (x₂, x₃), …].
    • pairsFrom

      public static <K,​ V> java.util.List<Pair<K,​V>> pairsFrom​(java.util.Map<K,​V> map)
      Converts a Map to a list of pairs.
      Returns:
      [(k₀,v₀), (k₁,v₁), (k₂,v₂), …].
    • toMap

      public static <K,​ V> java.util.Map<K,​V> toMap​(java.lang.Iterable<Pair<K,​V>> items)
      [(k₀,v₀), (k₁,v₁), (k₂,v₂), …] → [(k₀:v₀), (k₁:v₁), (k₂:v₂), …] - Converts pairs to a Map.
      Returns:
      a map [(k₀:v₀), (k₁:v₁), (k₂:v₂), …].
    • stream

      public static <T> java.util.stream.Stream<Pair<T,​T>> stream​(java.lang.Iterable<T> xs)
      [x₀, x₁, x₂, …] → [(x₀,x₁), (x₂,x₃), (x₄,x₅), …] - Converts an iterable of values to a stream of pairs, consuming two at a time.
      Parameters:
      xs - an iterable of values [x₀, x₁, x₂, …].
      Returns:
      a stream [(x₀,x₁), (x₂,x₃), (x₄,x₅), …].
      Since:
      1.1.0
    • streamAdjacent

      public static <T> java.util.stream.Stream<Pair<T,​T>> streamAdjacent​(java.lang.Iterable<T> xs)
      [x₀, x₁, x₂, …] → [(x₀,x₁), (x₁,x₂), (x₂,x₃), …] - Converts an iterable to a stream of pairs of adjacent values. Has the same effect as zip(xs[:-1], xs[1:]) in Python.
      Parameters:
      xs - an iterable of values [x₀, x₁, x₂, …].
      Returns:
      [(x₀,x₁), (x₁,x₂), (x₂,x₃), …].
    • stream

      public static <K,​ V> java.util.stream.Stream<Pair<K,​V>> stream​(java.util.Map<K,​V> map)
      [(k₀:v₀), (k₁:v₁), (k₂:v₂), …] → [(k₀,v₀), (k₁,v₁), (k₂,v₂), …] - Converts a Map to a stream of key-value pairs. Allows you to replace map.entrySet().stream().map(Pairs::pairFrom) with stream(map).
      Returns:
      a stream of key-value pairs [(k₀,v₀), (k₁,v₁), (k₂,v₂), …]
      Since:
      1.1.0
    • enumerate

      public static <T> java.util.stream.Stream<Pair<java.lang.Integer,​T>> enumerate​(java.lang.Iterable<T> xs)
      [x₀, x₁, x₂, …] → [(0,x₀), (1,x₁), (2,x₂), …] - Converts an iterable to a stream of pairs where the first value is the index of the value. Like enumerate() in Python. Most useful with Lists so that its elements can be iterated together with their indices.

      The stream is constructed lazily. So the iterable could potentially return an infinite iterator.

      Parameters:
      xs - an iterable of values [x₀, x₁, x₂, …].
      Returns:
      a stream of index-value pairs [(0,x₀), (1,x₁), (2,x₂), …].
      Since:
      1.1.0
    • pairsToMap

      public static <T1,​ T2> java.util.stream.Collector<Pair<T1,​T2>,​?,​java.util.Map<T1,​T2>> pairsToMap()
      Returns a Collector that can collect a stream of pairs into a Map.
      Since:
      1.1.0
    • zip

      public static <T1,​ T2> java.util.stream.Stream<Pair<T1,​T2>> zip​(java.lang.Iterable<T1> xs, java.lang.Iterable<T2> ys)
      ([x₀, x₁, x₂, …], [y₀, y₁, y₂, …]) → [(x₀,y₀), (x₁,y₁), (x₂,y₂), …] - like zip(xs, ys) in Python.
      Parameters:
      xs - an Iterable of values [x₀, x₁, x₂, …].
      ys - an Iterable of values [y₀, y₁, y₂, …].
      Returns:
      a Stream of x-y pairs [(x₀,y₀), (x₁,y₁), (x₂,y₂), …].
    • function

      public static <T1,​ T2,​ R> java.util.function.Function<Pair<T1,​T2>,​R> function​(java.util.function.BiFunction<? super T1,​? super T2,​? extends R> f)
      (f: x,y → z) → (g: (x,y) → z) - Converts a binary function to a function that is unary for pairs. Allows streamOfPairs.map(function(f)) instead of streamOfPairs.map(p -> f.apply(p._1, p._2)).
      See Also:
      Pair.apply(BiFunction)
    • biFunction

      public static <T1,​ T2,​ R> java.util.function.BiFunction<T1,​T2,​R> biFunction​(java.util.function.Function<Pair<T1,​T2>,​? extends R> f)
      (f: (x,y) → z) → (g: x,y → z) - Converts a unary function from pairs to a binary function.
    • predicate

      public static <T1,​ T2> java.util.function.Predicate<Pair<T1,​T2>> predicate​(java.util.function.BiPredicate<? super T1,​? super T2> f)
      (f: x,y → T/F) → (g: (x,y) → T/F) - Converts a binary predicate to a predicate that is unary for pairs. Allows streamOfPairs.filter(predicate(f)) instead of streamOfPairs.filter(p -> f.test(p._1, p._2)).
      See Also:
      Pair.test(BiPredicate)
    • biPredicate

      public static <T1,​ T2> java.util.function.BiPredicate<T1,​T2> biPredicate​(java.util.function.Predicate<Pair<T1,​T2>> f)
      (f: (x,y) → T/F) → (g: x,y → T/F) - Converts a unary predicate from pairs to a binary predicate.
    • consumer

      public static <T1,​ T2> java.util.function.Consumer<Pair<T1,​T2>> consumer​(java.util.function.BiConsumer<? super T1,​? super T2> f)
      (f: x,y → _) → (g: (x,y) → _) - Converts a binary consumer to a consumer that is unary for pairs. Allows streamOfPairs.forEach(consumer(f)) instead of streamOfPairs.forEach(p -> f.accept(p._1, p._2)).
      See Also:
      Pair.accept(BiConsumer)
    • biConsumer

      public static <T1,​ T2> java.util.function.BiConsumer<T1,​T2> biConsumer​(java.util.function.Consumer<Pair<T1,​T2>> f)
      (f: (x,y) → _) → (g: x,y → _) - Converts a unary consumer of pairs to a binary consumer.
    • withId

      public static <T,​ R> java.util.function.Function<T,​Pair<T,​R>> withId​(java.util.function.Function<? super T,​? extends R> f)
      (f: x → y) → (g: x → (x,y)) - Takes a function and returns a function to pairs, where the first element is the original input and the second element is the output.
      Parameters:
      f - a function f: x → y
      Returns:
      a function g: x → (x,y)
    • comparator

      public static <T1 extends java.lang.Comparable<? super T1>,​ T2 extends java.lang.Comparable<? super T2>> java.util.Comparator<Pair<T1,​T2>> comparator()
      Returns a Comparator that compares pairs lexicographically.

       
       Collections.sort(pairsOfComparables); // ERROR, Pair does not implement Comparable.
       Collections.sort(pairsOfComparables, Pairs.comparator()); // OK
       
       
      Since:
      1.1.0
    • compare

      public static <T1 extends java.lang.Comparable<? super T1>,​ T2 extends java.lang.Comparable<? super T2>> int compare​(Pair<T1,​T2> p1, Pair<T1,​T2> p2)
      Compare two pairs lexicographically.
      Since:
      1.1.0
    • pairWithFirst

      public static <T1,​ T2> java.util.function.Function<T2,​Pair<T1,​T2>> pairWithFirst​(T1 first)
      a → (b → (a,b)) - Takes an item and returns a function that will take a second item and return a pair with the parameter to this function first and the parameter to the returned function second. This is essentially just the Pair factory method curried with the first item.
    • pairWithSecond

      public static <T1,​ T2> java.util.function.Function<T1,​Pair<T1,​T2>> pairWithSecond​(T2 second)
      b → (a → (a,b)) - Takes an item and returns a function that will take a second item and return a pair with the parameter to this function second and the parameter to the returned function first.
    • map_1

      public static <T1,​ T2,​ R> java.util.function.Function<Pair<T1,​T2>,​Pair<R,​T2>> map_1​(java.util.function.Function<? super T1,​? extends R> f)
      (f: x → y) → (g: (x,y) → (f(x),y)) - Returns a function for mapping the first element in a pair.
      Since:
      1.1.0
    • map_2

      public static <T1,​ T2,​ R> java.util.function.Function<Pair<T1,​T2>,​Pair<T1,​R>> map_2​(java.util.function.Function<? super T2,​? extends R> f)
      (f: x → y) → (g: (x,y) → (x,f(y))) - Returns a function for mapping the second element in a pair.
      Since:
      1.1.0
    • map

      public static <T1,​ T2,​ R,​ U> java.util.function.Function<Pair<T1,​T2>,​Pair<R,​U>> map​(java.util.function.Function<? super T1,​? extends R> f, java.util.function.Function<? super T2,​? extends U> g)
      (f: x → y), (g: u → v) → (h: (x,u) → (f(x),g(u))) - Returns a function for mapping both elements in a pair.
      Since:
      1.1.0
    • test_1

      public static <T1,​ T2> java.util.function.Predicate<Pair<T1,​T2>> test_1​(java.util.function.Predicate<? super T1> f)
      (f: x → T/F) → (g: (x,y) → (f(x),y)) - Returns a predicate for testing the first element in a pair.
      Since:
      1.1.0
    • test_2

      public static <T1,​ T2> java.util.function.Predicate<Pair<T1,​T2>> test_2​(java.util.function.Predicate<? super T2> f)
      (f: x → T/F) → (g: (x,y) → (x,f(y))) - Returns a predicate for testing the second element in a pair.
      Since:
      1.1.0