Last active
July 18, 2022 06:53
-
-
Save haskellcamargo/3c93bc279bd7dba9477d 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; | |
import java.util.concurrent.Future; | |
public class Just<T> extends Maybe<T> { | |
private T mValue; | |
public Just(T data) { | |
mValue = data; | |
} | |
@Override | |
public T fromJust() { | |
return mValue; | |
} | |
@Override | |
public boolean isNothing() { | |
return false; | |
} | |
@Override | |
public Maybe bind(Callable fn) { | |
ExecutorService executorService = Executors.newFixedThreadPool(10); | |
Future<T> future = executorService.submit(fn); | |
return Monad.maybe(future); | |
} | |
@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
In
Nothing
class,isJust()
andisNothing()
are currently returntrue
. ThatisJust()
should returnfalse
.