README.org: reformat and adjust headers
This commit is contained in:
parent
c7c7c6ebeb
commit
3e4629d873
1 changed files with 369 additions and 344 deletions
139
README.org
139
README.org
|
@ -1,7 +1,4 @@
|
|||
* Mon
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: mon
|
||||
:END:
|
||||
|
||||
** TypeAlias, Maybe and Result for Java.
|
||||
|
||||
|
@ -23,10 +20,8 @@
|
|||
[[https://app.codacy.com/project/kemitix/mon/dashboard][file:https://img.shields.io/codacy/grade/d57096b0639d496aba9a7e43e7cf5b4c.svg?style=for-the-badge]]
|
||||
[[http://i.jpeek.org/net.kemitix/mon/index.html][file:http://i.jpeek.org/net.kemitix/mon/badge.svg]]
|
||||
|
||||
|
||||
** Maven
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: maven
|
||||
:END:
|
||||
|
||||
#+BEGIN_SRC xml
|
||||
<dependency>
|
||||
|
@ -41,9 +36,6 @@
|
|||
|
||||
|
||||
** TypeAlias
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: typealias
|
||||
:END:
|
||||
|
||||
In Haskell it is possible to create an alias for a Type, and to then use
|
||||
that alias with the same behaviour as the original, except that the compiler
|
||||
|
@ -127,10 +119,8 @@
|
|||
}
|
||||
#+END_SRC
|
||||
|
||||
|
||||
** Maybe
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: maybe
|
||||
:END:
|
||||
|
||||
Allows specifying that a value may or may not be present. Similar to
|
||||
=Optional=. =Maybe= provides additional methods that =Optional= doesn't:
|
||||
|
@ -180,7 +170,7 @@
|
|||
=nothing= drops straight through the map and triggers the Runnable parameter
|
||||
in the =match= call.
|
||||
|
||||
**** =Maybe= is a Monad:
|
||||
*** =Maybe= is a Monad:
|
||||
|
||||
#+BEGIN_SRC java
|
||||
package net.kemitix.mon;
|
||||
|
@ -231,9 +221,10 @@
|
|||
}
|
||||
#+END_SRC
|
||||
|
||||
**** Static Constructors
|
||||
|
||||
***** =static <T> Maybe<T> maybe(T value)=
|
||||
*** Static Constructors
|
||||
|
||||
**** =static <T> Maybe<T> maybe(T value)=
|
||||
|
||||
Create a Maybe for the value that may or may not be present.
|
||||
|
||||
|
@ -244,7 +235,8 @@
|
|||
final Maybe<Integer> nothing = Maybe.maybe(null);
|
||||
#+END_SRC
|
||||
|
||||
***** =static <T> Maybe<T> just(T value)=
|
||||
|
||||
**** =static <T> Maybe<T> just(T value)=
|
||||
|
||||
Create a Maybe for the value that is present.
|
||||
|
||||
|
@ -256,7 +248,8 @@
|
|||
final Maybe<Integer> just = Maybe.just(1);
|
||||
#+END_SRC
|
||||
|
||||
***** =static <T> Maybe<T> nothing()=
|
||||
|
||||
**** =static <T> Maybe<T> nothing()=
|
||||
|
||||
Create a Maybe for a lack of a value.
|
||||
|
||||
|
@ -264,9 +257,10 @@
|
|||
final Maybe<Integer> nothing = Maybe.nothing();
|
||||
#+END_SRC
|
||||
|
||||
**** Instance Methods
|
||||
|
||||
***** =Maybe<T> filter(Predicate<T> predicate)=
|
||||
*** Instance Methods
|
||||
|
||||
**** =Maybe<T> filter(Predicate<T> predicate)=
|
||||
|
||||
Filter a Maybe by the predicate, replacing with Nothing when it fails.
|
||||
|
||||
|
@ -275,7 +269,8 @@
|
|||
.filter(v -> v % 2 == 0);
|
||||
#+END_SRC
|
||||
|
||||
***** =<R> Maybe<R> map(Function<T,R> f)=
|
||||
|
||||
**** =<R> Maybe<R> map(Function<T,R> f)=
|
||||
|
||||
Applies the function to the value within the Maybe, returning the result within another Maybe.
|
||||
|
||||
|
@ -284,7 +279,8 @@
|
|||
.map(v -> v * 100);
|
||||
#+END_SRC
|
||||
|
||||
***** =<R> Maybe<R> flatMap(Function<T,Maybe<R>> f)=
|
||||
|
||||
**** =<R> Maybe<R> flatMap(Function<T,Maybe<R>> f)=
|
||||
|
||||
Applies the function to the value within the =Maybe=, resulting in another =Maybe=, then flattens the resulting =Maybe<Maybe<T>>= into =Maybe<T>=.
|
||||
|
||||
|
@ -295,7 +291,8 @@
|
|||
.flatMap(v -> Maybe.maybe(getValueFor(v)));
|
||||
#+END_SRC
|
||||
|
||||
***** =void match(Consumer<T> just, Runnable nothing)=
|
||||
|
||||
**** =void match(Consumer<T> just, Runnable nothing)=
|
||||
|
||||
Matches the Maybe, either just or nothing, and performs either the Consumer, for Just, or Runnable for nothing.
|
||||
|
||||
|
@ -307,7 +304,8 @@
|
|||
);
|
||||
#+END_SRC
|
||||
|
||||
***** =T orElse(T otherValue)=
|
||||
|
||||
**** =T orElse(T otherValue)=
|
||||
|
||||
A value to use when Maybe is Nothing.
|
||||
|
||||
|
@ -316,7 +314,8 @@
|
|||
.orElse(1);
|
||||
#+END_SRC
|
||||
|
||||
***** =T orElseGet(Supplier<T> otherValueSupplier)=
|
||||
|
||||
**** =T orElseGet(Supplier<T> otherValueSupplier)=
|
||||
|
||||
Provide a value to use when Maybe is Nothing.
|
||||
|
||||
|
@ -325,7 +324,8 @@
|
|||
.orElseGet(() -> getDefaultValue());
|
||||
#+END_SRC
|
||||
|
||||
***** =void orElseThrow(Supplier<Exception> error)=
|
||||
|
||||
**** =void orElseThrow(Supplier<Exception> error)=
|
||||
|
||||
Throw the exception if the Maybe is a Nothing.
|
||||
|
||||
|
@ -334,7 +334,8 @@
|
|||
.orElseThrow(() -> new RuntimeException("error"));
|
||||
#+END_SRC
|
||||
|
||||
***** =Maybe<T> peek(Consumer<T> consumer)=
|
||||
|
||||
**** =Maybe<T> peek(Consumer<T> consumer)=
|
||||
|
||||
Provide the value within the Maybe, if it exists, to the Consumer, and returns this Maybe. Conceptually equivalent to the idea of =ifPresent(...)=.
|
||||
|
||||
|
@ -343,7 +344,8 @@
|
|||
.peek(v -> v.foo());
|
||||
#+END_SRC
|
||||
|
||||
***** =void ifNothing(Runnable runnable)=
|
||||
|
||||
**** =void ifNothing(Runnable runnable)=
|
||||
|
||||
Run the runnable if the Maybe is a Nothing, otherwise do nothing.
|
||||
|
||||
|
@ -352,7 +354,8 @@
|
|||
.ifNothing(() -> doSomething());
|
||||
#+END_SRC
|
||||
|
||||
***** =Stream<T> stream()=
|
||||
|
||||
**** =Stream<T> stream()=
|
||||
|
||||
Converts the Maybe into either a single value stream or an empty stream.
|
||||
|
||||
|
@ -361,7 +364,8 @@
|
|||
.stream();
|
||||
#+END_SRC
|
||||
|
||||
***** =boolean isJust()=
|
||||
|
||||
**** =boolean isJust()=
|
||||
|
||||
Checks if the Maybe is a Just.
|
||||
|
||||
|
@ -370,7 +374,8 @@
|
|||
.isJust();
|
||||
#+END_SRC
|
||||
|
||||
***** =boolean isNothing()=
|
||||
|
||||
**** =boolean isNothing()=
|
||||
|
||||
Checks if the Maybe is Nothing.
|
||||
|
||||
|
@ -379,7 +384,8 @@
|
|||
.isNothing();
|
||||
#+END_SRC
|
||||
|
||||
***** =Optional<T> toOptional()=
|
||||
|
||||
**** =Optional<T> toOptional()=
|
||||
|
||||
Convert the Maybe to an Optional.
|
||||
|
||||
|
@ -388,10 +394,8 @@
|
|||
.toOptional();
|
||||
#+END_SRC
|
||||
|
||||
|
||||
** Result
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: result
|
||||
:END:
|
||||
|
||||
Allows handling error conditions without the need to catch exceptions.
|
||||
|
||||
|
@ -443,7 +447,7 @@
|
|||
Result would have ignored the =flatMap= and skipped to the =match()= when it
|
||||
would have called the error =Consumer=.
|
||||
|
||||
**** =Result= is a Monad
|
||||
*** =Result= is a Monad
|
||||
|
||||
#+BEGIN_SRC java
|
||||
package net.kemitix.mon;
|
||||
|
@ -494,9 +498,10 @@
|
|||
}
|
||||
#+END_SRC
|
||||
|
||||
**** Static Constructors
|
||||
|
||||
***** =static <T> Result<T> of(Callable<T> callable)=
|
||||
*** Static Constructors
|
||||
|
||||
**** =static <T> Result<T> of(Callable<T> callable)=
|
||||
|
||||
Create a Result for a output of the Callable.
|
||||
|
||||
|
@ -511,7 +516,8 @@
|
|||
final Result<Integer> error = Result.of(() -> {throw new RuntimeException();});
|
||||
#+END_SRC
|
||||
|
||||
***** =static <T> Result<T> ok(T value)=
|
||||
|
||||
**** =static <T> Result<T> ok(T value)=
|
||||
|
||||
Create a Result for a success.
|
||||
|
||||
|
@ -521,7 +527,8 @@
|
|||
final Result<Integer> okay = Result.ok(1);
|
||||
#+END_SRC
|
||||
|
||||
***** =static <T> Result<T> error(Throwable error)=
|
||||
|
||||
**** =static <T> Result<T> error(Throwable error)=
|
||||
|
||||
Create a Result for an error.
|
||||
|
||||
|
@ -529,14 +536,15 @@
|
|||
final Result<Integer> error = Result.error(new RuntimeException());
|
||||
#+END_SRC
|
||||
|
||||
**** Static Methods
|
||||
|
||||
*** Static Methods
|
||||
|
||||
These static methods provide integration with the =Maybe= class.
|
||||
|
||||
#+BEGIN_SRC java
|
||||
#+END_SRC
|
||||
|
||||
***** =static <T> Maybe<T> toMaybe(Result<T> result)=
|
||||
**** =static <T> Maybe<T> toMaybe(Result<T> result)=
|
||||
|
||||
Creates a =Maybe= from the =Result=, where the =Result= is a success, then
|
||||
the =Maybe= will contain the value. However, if the =Result= is an error
|
||||
|
@ -547,7 +555,8 @@
|
|||
final Maybe<Integer> maybe = Result.toMaybe(result);
|
||||
#+END_SRC
|
||||
|
||||
***** =static <T> Result<T> fromMaybe(Maybe<T> maybe, Supplier<Throwable> error)=
|
||||
|
||||
**** =static <T> Result<T> fromMaybe(Maybe<T> maybe, Supplier<Throwable> error)=
|
||||
|
||||
Creates a =Result= from the =Maybe=, where the =Result= will be an error
|
||||
if the =Maybe= is nothing. Where the =Maybe= is nothing, then the
|
||||
|
@ -558,7 +567,8 @@
|
|||
final Result<Integer> result = Result.fromMaybe(maybe, () -> new NoSuchFileException("filename"));
|
||||
#+END_SRC
|
||||
|
||||
***** =static <T> Result<Maybe<T>> invert(Maybe<Result<T>> maybeResult)=
|
||||
|
||||
**** =static <T> Result<Maybe<T>> invert(Maybe<Result<T>> maybeResult)=
|
||||
|
||||
Swaps the =Result= within a =Maybe=, so that =Result= contains a =Maybe=.
|
||||
|
||||
|
@ -567,7 +577,8 @@
|
|||
final Result<Maybe<Integer>> result = Result.invert(maybe);
|
||||
#+END_SRC
|
||||
|
||||
***** =static <T,R> Result<Maybe<R>> flatMapMaybe(Result<Maybe<T>> maybeResult, Function<Maybe<T>,Result<Maybe<R>>> f)=
|
||||
|
||||
**** =static <T,R> Result<Maybe<R>> flatMapMaybe(Result<Maybe<T>> maybeResult, Function<Maybe<T>,Result<Maybe<R>>> f)=
|
||||
|
||||
Applies the function to the contents of a Maybe within the Result.
|
||||
|
||||
|
@ -576,9 +587,10 @@
|
|||
final Result<Maybe<Integer>> maybeResult = Result.flatMapMaybe(result, maybe -> Result.of(() -> maybe.map(v -> v * 2)));
|
||||
#+END_SRC
|
||||
|
||||
**** Instance Methods
|
||||
|
||||
***** <R> Result<R> map(Function<T,R> f)
|
||||
*** Instance Methods
|
||||
|
||||
**** =<R> Result<R> map(Function<T,R> f)=
|
||||
|
||||
Applies the function to the value within the Functor, returning the result
|
||||
within a Functor.
|
||||
|
@ -588,7 +600,8 @@
|
|||
.map(v -> String.valueOf(v));
|
||||
#+END_SRC
|
||||
|
||||
***** <R> Result<R> flatMap(Function<T,Result<R>> f)
|
||||
|
||||
**** =<R> Result<R> flatMap(Function<T,Result<R>> f)=
|
||||
|
||||
Returns a new Result consisting of the result of applying the function to
|
||||
the contents of the Result.
|
||||
|
@ -598,7 +611,8 @@
|
|||
.flatMap(v -> Result.of(() -> String.valueOf(v)));
|
||||
#+END_SRC
|
||||
|
||||
***** <R> Result<R> andThen(Function<T,Callable<R>> f)
|
||||
|
||||
**** =<R> Result<R> andThen(Function<T,Callable<R>> f)=
|
||||
|
||||
Maps a Success Result to another Result using a Callable that is able to
|
||||
throw a checked exception.
|
||||
|
@ -608,7 +622,8 @@
|
|||
.andThen(v -> () -> {throw new IOException();});
|
||||
#+END_SRC
|
||||
|
||||
***** void match(Consumer<T> onSuccess, Consumer<Throwable> onError)
|
||||
|
||||
**** =void match(Consumer<T> onSuccess, Consumer<Throwable> onError)=
|
||||
|
||||
Matches the Result, either success or error, and supplies the appropriate
|
||||
Consumer with the value or error.
|
||||
|
@ -621,7 +636,8 @@
|
|||
);
|
||||
#+END_SRC
|
||||
|
||||
***** Result<T> recover(Function<Throwable,Result<T>> f)
|
||||
|
||||
**** =Result<T> recover(Function<Throwable,Result<T>> f)=
|
||||
|
||||
Provide a way to attempt to recover from an error state.
|
||||
|
||||
|
@ -630,7 +646,8 @@
|
|||
.recover(e -> Result.of(() -> getSafeValue(e)));
|
||||
#+END_SRC
|
||||
|
||||
***** Result<T> peek(Consumer<T> consumer)
|
||||
|
||||
**** =Result<T> peek(Consumer<T> consumer)=
|
||||
|
||||
Provide the value within the Result, if it is a success, to the Consumer,
|
||||
and returns this Result.
|
||||
|
@ -640,7 +657,8 @@
|
|||
.peek(v -> System.out.println(v));
|
||||
#+END_SRC
|
||||
|
||||
***** Result<T> thenWith(Function<T,WithResultContinuation<T>> f)
|
||||
|
||||
**** =Result<T> thenWith(Function<T,WithResultContinuation<T>> f)=
|
||||
|
||||
Perform the continuation with the current Result value then return the
|
||||
current Result, assuming there was no error in the continuation.
|
||||
|
@ -651,7 +669,8 @@
|
|||
.thenWith(v -> () -> {throw new IOException();});
|
||||
#+END_SRC
|
||||
|
||||
***** Result<Maybe<T>> maybe(Predicate<T> predicate)
|
||||
|
||||
**** =Result<Maybe<T>> maybe(Predicate<T> predicate)=
|
||||
|
||||
Wraps the value within the Result in a Maybe, either a Just if the
|
||||
predicate is true, or Nothing.
|
||||
|
@ -661,7 +680,8 @@
|
|||
.maybe(v -> v % 2 == 0);
|
||||
#+END_SRC
|
||||
|
||||
***** T orElseThrow()
|
||||
|
||||
**** =T orElseThrow()=
|
||||
|
||||
Extracts the successful value from the result, or throws the error
|
||||
Throwable.
|
||||
|
@ -671,7 +691,8 @@
|
|||
.orElseThrow();
|
||||
#+END_SRC
|
||||
|
||||
***** void onError(Consumer<Throwable> errorConsumer)
|
||||
|
||||
**** =void onError(Consumer<Throwable> errorConsumer)=
|
||||
|
||||
A handler for error states.
|
||||
|
||||
|
@ -680,7 +701,8 @@
|
|||
.onError(e -> handleError(e));
|
||||
#+END_SRC
|
||||
|
||||
***** boolean isOkay()
|
||||
|
||||
**** =boolean isOkay()=
|
||||
|
||||
Checks if the Result is a success.
|
||||
|
||||
|
@ -689,7 +711,8 @@
|
|||
.isOkay();
|
||||
#+END_SRC
|
||||
|
||||
***** boolean isError()
|
||||
|
||||
**** =boolean isError()=
|
||||
|
||||
Checks if the Result is an error.
|
||||
|
||||
|
@ -697,3 +720,5 @@
|
|||
final boolean isError = Result.of(() -> getValue())
|
||||
.isError();
|
||||
#+END_SRC
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue