Package org.apache.groovy.util

# Class Closures

java.lang.Object
org.apache.groovy.util.Closures

public class Closures extends Object
Helpers that return hybrid one-argument functions which are both `Closure` and the appropriate `java.util.function` SAM (`Predicate`, `Function` or `Consumer`).

For variants whose results are only the SAM type, see `Lambdas`.

Since:
  6.0.0

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `Closures.ConsumerClosure<T>`
  Hybrid one-argument function that is both a `Closure` and a `Consumer`.
  `static class`
  `Closures.FunctionClosure<T,R>`
  Hybrid one-argument function that is both a `Closure` and a `Function`.
  `static class`
  `Closures.PredicateClosure<T>`
  Hybrid one-argument function that is both a `Closure` and a `Predicate`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static <T, P> Closures.ConsumerClosure<T>`
  `curryWith(BiConsumer<? super T,? super P> bc, P p)`
  Right-partials a `BiConsumer` and lifts the result into a hybrid usable as both `Closure` and `Consumer`.
  `static <T, P, R> Closures.FunctionClosure<T,R>`
  `curryWith(BiFunction<? super T,? super P,? extends R> bf, P p)`
  Right-partials a `BiFunction` and lifts the result into a hybrid usable as both `Closure` and `Function`.
  `static <T, P> Closures.PredicateClosure<T>`
  `curryWith(BiPredicate<? super T,? super P> bp, P p)`
  Right-partials a `BiPredicate` and lifts the result into a hybrid usable as both `Closure` and `Predicate`.
  `static <T> Closures.ConsumerClosure<T>`
  `from(Consumer<T> c)`
  Lifts a `Consumer` into a hybrid that is also a `Closure`.
  `static <T, R> Closures.FunctionClosure<T,R>`
  `from(Function<T,R> f)`
  Lifts a `Function` into a hybrid that is also a `Closure`.
  `static <T> Closures.PredicateClosure<T>`
  `from(Predicate<T> p)`
  Lifts a `Predicate` into a hybrid that is also a `Closure`.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### from

    public static <T> Closures.PredicateClosure<T> from(Predicate<T> p)
    Lifts a `Predicate` into a hybrid that is also a `Closure`. If `p` is already a `Closures.PredicateClosure` it is returned unchanged.

         Predicate<Integer> isEven = n -> n % 2 == 0
         assert [1, 2, 3, 4].findAll(Closures.from(isEven)) == [2, 4]

    Since:
      6.0.0

  - ### from

    public static <T, R> Closures.FunctionClosure<T,R> from(Function<T,R> f)
    Lifts a `Function` into a hybrid that is also a `Closure`. If `f` is already a `Closures.FunctionClosure` it is returned unchanged.
    Since:
      6.0.0

  - ### from

    public static <T> Closures.ConsumerClosure<T> from(Consumer<T> c)
    Lifts a `Consumer` into a hybrid that is also a `Closure`. If `c` is already a `Closures.ConsumerClosure` it is returned unchanged.
    Since:
      6.0.0

  - ### curryWith

    public static <T, P> Closures.PredicateClosure<T> curryWith(BiPredicate<? super T,? super P> bp, P p)
    Right-partials a `BiPredicate` and lifts the result into a hybrid usable as both `Closure` and `Predicate`. Equivalent to `from(Lambdas.curryWith(bp, p))`.

         BiPredicate<Integer,Integer> divisibleBy = (n, d) -> n % d == 0
         assert [1, 2, 3, 4, 5].findAll(curryWith(divisibleBy, 2)) == [2, 4]

    Since:
      6.0.0

  - ### curryWith

    public static <T, P, R> Closures.FunctionClosure<T,R> curryWith(BiFunction<? super T,? super P,? extends R> bf, P p)
    Right-partials a `BiFunction` and lifts the result into a hybrid usable as both `Closure` and `Function`. Equivalent to `from(Lambdas.curryWith(bf, p))`.
    Since:
      6.0.0

  - ### curryWith

    public static <T, P> Closures.ConsumerClosure<T> curryWith(BiConsumer<? super T,? super P> bc, P p)
    Right-partials a `BiConsumer` and lifts the result into a hybrid usable as both `Closure` and `Consumer`. Equivalent to `from(Lambdas.curryWith(bc, p))`.
    Since:
      6.0.0