Last active
May 5, 2020 12:16
-
-
Save haskellcamargo/6274b2775821676b5f97 to your computer and use it in GitHub Desktop.
Java maybe monad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ngi.ginga.business.util.seq; | |
/** | |
* Copyright (C) 2015 - NG Informática | |
* | |
* @author Marcelo Camargo | |
* @since 08/12/2015 | |
*/ | |
public interface Function<Ret, Arg> { | |
Ret call(Arg arg); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ngi.ginga.lib.monad; | |
import br.com.ngi.ginga.business.util.seq.Function; | |
/** | |
* Copyright (C) 2015 - NG Informática | |
* | |
* @author Marcelo Camargo | |
* @since 10/02/16 | |
*/ | |
public interface IMaybe<A> { | |
<B> IMaybe<B> bind(Function<B, A> fn); | |
A fromJust(); | |
A fromMaybe(A def); | |
boolean isJust(); | |
boolean isNothing(); | |
<B> B maybe(B def, Function<B, A> fn); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ngi.ginga.lib.monad; | |
import br.com.ngi.ginga.business.util.seq.Function; | |
/** | |
* Copyright (C) 2015 - NG Informática | |
* | |
* @author Marcelo Camargo | |
* @since 10/02/16 | |
*/ | |
public class Just<A> implements IMaybe<A> { | |
private A value; | |
public Just(A value) { | |
this.value = value; | |
} | |
@Override | |
public <B> IMaybe bind(Function<B, A> fn) { | |
return Maybe.maybe(fn.call(this.value)); | |
} | |
@Override | |
public A fromJust() { | |
return this.value; | |
} | |
@Override | |
public A fromMaybe(A def) { | |
return this.value; | |
} | |
@Override | |
public boolean isJust() { | |
return true; | |
} | |
@Override | |
public boolean isNothing() { | |
return false; | |
} | |
@Override | |
public <B> B maybe(B def, Function<B, A> fn) { | |
return fn.call(null); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ngi.ginga.lib.monad; | |
/** | |
* Copyright (C) 2015 - NG Informática | |
* | |
* @author Marcelo Camargo | |
* @since 10/02/16 | |
*/ | |
public final class Maybe { | |
public static <T> IMaybe maybe(T value) { | |
if (value instanceof IMaybe) { | |
return (IMaybe) value; | |
} | |
return value == null ? new Nothing() : new Just<T>(value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ngi.ginga.lib.monad; | |
import br.com.ngi.ginga.business.util.seq.Function; | |
/** | |
* Copyright (C) 2015 - NG Informática | |
* | |
* @author Marcelo Camargo | |
* @since 10/02/16 | |
*/ | |
public class Nothing<A> implements IMaybe<A> { | |
@Override | |
public IMaybe bind(Function fn) { | |
return this; | |
} | |
@Override | |
public A fromJust() { | |
throw new RuntimeException("Cannot call fromJust() on Nothing"); | |
} | |
@Override | |
public A fromMaybe(A def) { | |
return def; | |
} | |
@Override | |
public boolean isJust() { | |
return false; | |
} | |
@Override | |
public boolean isNothing() { | |
return true; | |
} | |
@Override | |
public <B> B maybe(B def, Function<B, A> fn) { | |
return def; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment