-
-
Save ppamorim/90aa96fb7f1555124b52 to your computer and use it in GitHub Desktop.
Maybe monad implementation in Java 7
This file contains hidden or 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.monad; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
public class Just<T> extends Maybe<T> { | |
private T type; | |
public Just(T type) { | |
this.type = type; | |
} | |
@Override public T fromJust() { | |
return this.type; | |
} | |
@Override boolean isNothing() { | |
return false; | |
} | |
@Override public Maybe bind(Callable fn) { | |
return Monad.maybe(Executors.newFixedThreadPool(10).submit(fn)); | |
} | |
@Override public boolean isJust() { | |
return true; | |
} | |
@Override public Maybe maybe(T def, Callable<T> fn) { | |
return this.bind(fn); | |
} | |
} |
This file contains hidden or 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.refreshlayout; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.widget.ListView; | |
import java.util.concurrent.Callable; | |
import br.com.ngi.monad.Maybe; | |
import br.com.ngi.monad.Monad; | |
public class MainActivity extends AppCompatActivity { | |
@Override protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ListView x = null; | |
final Maybe list = Monad.maybe(x); | |
list.bind(new Callable() { | |
@Override public Object call() throws Exception { | |
Log.d("MainActivity", list.fromJust().toString()); | |
return null; | |
} | |
}); | |
} | |
} |
This file contains hidden or 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.monad; | |
import java.util.concurrent.Callable; | |
public abstract class Maybe<T> { | |
public abstract T fromJust(); | |
public abstract boolean isJust(); | |
public abstract boolean isNothing(); | |
public abstract Maybe<T> bind(Callable<T> runnable); | |
public abstract Maybe<T> maybe(T def, Callable<T> fn); | |
} |
This file contains hidden or 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.monad; | |
import java.util.concurrent.Callable; | |
public class Nothing<T> extends Maybe { | |
@Override public T fromJust() throws RuntimeException { | |
throw new RuntimeException("Cannot call fromJust() on Nothing"); | |
} | |
@Override public boolean isJust() { | |
return true; | |
} | |
@Override public boolean isNothing() { | |
return true; | |
} | |
@Override public Maybe<T> bind(Callable fn) { | |
return this; | |
} | |
@Override public Maybe<T> maybe(Object def, Callable fn) { | |
return (Maybe<T>) Monad.maybe(def); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment