Exemplo n.º 1
0
 public static Result<String> readFile2String(String path) {
   try {
     return Result.success(new String(Files.readAllBytes(Paths.get(path))));
   } catch (IOException e) {
     return Result.failure(String.format("IO error while reading file %s", path), e);
   } catch (Exception e) {
     return Result.failure(String.format("Unexpected error while reading file %s", path), e);
   }
 }
Exemplo n.º 2
0
public class ListTest {

  @Test
  public void testFlattenResult() {
    List<Integer> list = List.list(1, 2, 3, 4, 5, 0, 6, 7, 8, 9);
    List<Double> list2 = List.flattenResult(list.map(inverse));
    assertEquals(
        "[1.0, 0.5, 0.3333333333333333, 0.25, 0.2, 0.16666666666666666, 0.14285714285714285, 0.125, 0.1111111111111111, NIL]",
        list2.toString());
  }

  private Function<Integer, Result<Double>> inverse =
      x -> x == 0 ? Result.failure("divide by 0") : Result.success((double) 1 / x);
}
Exemplo n.º 3
0
 private static Result<List<Element>> readDocument(String rootElementName, String stringDoc) {
   final SAXBuilder builder = new SAXBuilder();
   try {
     final Document document = builder.build(new StringReader(stringDoc));
     final Element rootElement = document.getRootElement();
     return Result.success(List.fromCollection(rootElement.getChildren(rootElementName)));
   } catch (IOException | JDOMException io) {
     return Result.failure(
         String.format(
             "Invalid root element name '%s' or XML data %s", rootElementName, stringDoc),
         io);
   } catch (Exception e) {
     return Result.failure(
         String.format("Unexpected error while reading XML data %s", stringDoc), e);
   }
 }
Exemplo n.º 4
0
 public static Executable readXmlFile(
     Supplier<Result<String>> sPath,
     Supplier<Result<String>> sRootName,
     Tuple<String, List<String>> format,
     Effect<List<String>> e) {
   final Result<String> path = sPath.get();
   final Result<String> rDoc = path.flatMap(ReadXmlFile::readFile2String);
   final Result<String> rRoot = sRootName.get();
   final Result<List<String>> result =
       rDoc.flatMap(
           doc ->
               rRoot
                   .flatMap(rootElementName -> readDocument(rootElementName, doc))
                   .map(list -> toStringList(list, format)));
   return () -> result.forEachOrThrow(e);
 }
Exemplo n.º 5
0
 public Result<A> lastOption() {
   return foldLeft(Result.empty(), x -> Result::success);
 }
Exemplo n.º 6
0
 public static <A, B> Result<List<B>> traverse(List<A> list, Function<A, Result<B>> f) {
   return list.foldRight(
       Result.success(List.list()), x -> y -> Result.map2(f.apply(x), y, a -> b -> b.cons(a)));
 }
Exemplo n.º 7
0
 @Override
 public Result<A> headOption() {
   return Result.success(head);
 }
Exemplo n.º 8
0
 @Override
 public Result<A> headOption() {
   return Result.empty();
 }
Exemplo n.º 9
0
 public static <A> Result<List<A>> sequence_(List<Result<A>> list) {
   return list.foldRight(
       Result.success(List.list()), x -> y -> Result.map2(x, y, a -> b -> b.cons(a)));
 }