
net.sf.staccatocommons.control.monad.AbstractMonad Maven / Gradle / Ivy
The newest version!
/**
* Copyright (c) 2010-2012, The StaccatoCommons Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package net.sf.staccatocommons.control.monad;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import net.sf.staccatocommons.control.monad.internal.AppendMonad;
import net.sf.staccatocommons.defs.Applicable;
import net.sf.staccatocommons.defs.Evaluable;
import net.sf.staccatocommons.defs.Executable;
import net.sf.staccatocommons.defs.tuple.Tuple2;
import net.sf.staccatocommons.lang.AbstractProtoMonad;
/**
* @author flbulgarelli
* @since 1.2
*/
public abstract class AbstractMonad extends AbstractProtoMonad, Monad, A> implements
Monad {
public final Monad filter(final Evaluable super A> predicate) {
return bind(Monads.filter(predicate));
}
/* fmap f xs == xs >>= return . f */
public final Monad map(final Applicable super A, ? extends B> function) {
return bind(Monads.map(function));
}
public final Monad flatMap(
final Applicable super A, ? extends Iterable extends B>> function) {
return bind(Monads.flatMap(function));
}
public Monad bind(Applicable super A, Monad> function) {
return new BoundMonad(monadicValue(), function);
}
public Monad incorporate(final Applicable super A, Monad> function) {
return bind(Monads.incorporate(function));
}
public Monad fork(ExecutorService executor) {
return bind(Monads. async(executor));
}
public final Monad each(final Executable super A> block) {
return bind(Monads.each(block));
}
public Monad discard() {
return (Monad) this;
}
public Monad> clone(Applicable super A, ? extends B> function) {
return bind(Monads.clone(function));
}
public Monad> branch(Applicable super A, ? extends B> function0,
Applicable super A, ? extends C> function1) {
return bind(Monads.branch(function0, function1));
}
public final void forEach(Executable super A> block) {
each(block).run();
}
public Monad append(Monad other) {
return new AppendMonad(this, other);
}
public void run(Executor executor) {
executor.execute(this);
}
public final Void value() {
run();
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy