Wiki

Clone wiki

fugue / 2.2

Scala interop support

New fugue-scala module which can be used from Scala to automatically convert between standard Scala classes such as scala.Option, scala.Either and scala.Tuple2, scala.Function2 to the fugue equivalents com.atlassian.fugue.Option, com.atlassian.fugue.Either, com.atlassian.fugue.Pair, com.atlassian.fugue.Function2 as well as from scala.Function to guava's com.google.common.base.Function and com.google.common.base.Predicate.

Usage is simply:

#!scala
import com.atlassian.fugue.ScalaConverters._

val java: com.guava.common.base.Function[Pair[Integer, String], Option[Integer]] = 

// infers: ((Int, String)) => scala.Option[Int]
val s = java.asScala 

// infers: Function[Pair[Integer, String], Option[Integer]]
val j = s.asJava 

Unit type

the new com.atlassian.fugue.Unit is a type which is once inhabited (it has only a single member, Unit.VALUE) that is meant as a replacement for the Void type that is not inhabited at all, which means the only valid value is null, and much of the library is null-intolerant.

The Unit and Void types usually signify that there was a side-effect – there is no useful return type, the method or function 'did something' (maybe launched missiles).

Either is now right biased.

Either has added the standard Functor and Monad methods, map and flatMap that work through the RHS type. Either also adds:

#!java
<X> Either<X, R> leftMap(Function<L, X> f);
<LL, RR> Either<LL, RR> bimap(Function<L, LL> ifLeft, Function<R, RR> ifRight);
allowing mapping through the LHS (equivalent to either.left().map(…), and bimap applies the appropriate function to whatever side it is.

Weak memoizer

The new Functions method:

#!java
static <A, B> weakMemoize(Function<A, B> f)
allows a function application to be cached such that the result is held in memory while it is in use, but is GC'd at the first available opportunity once it is no longer referenced. This can have important benefits to certain types of algorithms such as lock striping, while being as memory efficient as possible.

Iterables

Iterables adds two new methods:

#!java
static <A> Function<Iterable<A>, Option<A>> findFirst(Predicate<A> predicate);
static <A, B> Pair<Iterable<A>, Iterable<B>> unzip(Iterable<Pair<A, B>> pairs);

Options

Options adds one new method:

#!java
static <A> Predicate<Option<A>> lift(Predicate<A> pred);
for lifting a predicate to one that accepts options as input.

Suppliers

Suppliers adds one new method:

#!java
static <A, B> Supplier<B> fromFunction(Function<A, B> f, A a);
that applies the argument to the function each time the supplier is called.

Miscellaneous

Note that all signatures have had extraneous details such as public, final and appropriate variance annotations removed for clarity.

Updated