Monads with Java 8

前端 未结 9 1074
礼貌的吻别
礼貌的吻别 2020-12-07 07:29

In the interests of helping to understand what a monad is, can someone provide an example using java ? Are they possible ?

Lambda expressions are possible using java

9条回答
  •  星月不相逢
    2020-12-07 08:21

    Here's the thing about monads which is hard to grasp: monads are a pattern, not a specific type. Monads are a shape, they are an abstract interface (not in the Java sense) more than they are a concrete data structure. As a result, any example-driven tutorial is doomed to incompleteness and failure. [...] The only way to understand monads is to see them for what they are: a mathematical construct.

    Monads are not metaphors by Daniel Spiewak


    Monads in Java SE 8

    List monad

    interface Person {
        List parents();
    
        default List greatGrandParents1() {
            List list = new ArrayList<>();
            for (Person p : parents()) {
                for (Person gp : p.parents()) {
                    for (Person ggp : p.parents()) {
    
                        list.add(ggp);
                    }
                }
            }
            return list;
        }
    
        //  Stream flatMap(Function> mapper)
        default List greatGrandParents2() {
            return Stream.of(parents())
                    .flatMap(p -> Stream.of(p.parents()))
                    .flatMap(gp -> Stream.of(gp.parents()))
                    .collect(toList());
        }
    }
    

    Maybe monad

    interface Person {
        String firstName();
        String middleName();
        String lastName();
    
        default String fullName1() {
            String fName = firstName();
            if (fName != null) {
                String mName = middleName();
                if (mName != null) {
                    String lName = lastName();
                    if (lName != null) {
                        return fName + " " + mName + " " + lName;
                    }
                }
            }
            return null;
        }
    
        //  Optional flatMap(Function> mapper)
        default Optional fullName2() {
            return Optional.ofNullable(firstName())
                    .flatMap(fName -> Optional.ofNullable(middleName())
                    .flatMap(mName -> Optional.ofNullable(lastName())
                    .flatMap(lName -> Optional.of(fName + " " + mName + " " + lName))));
        }
    }
    

    Monad is a generic pattern for nested control flow encapsulation. I.e. a way to create reusable components from nested imperative idioms.

    Important to understand that a monad is not just a generic wrapper class with a flat map operation. For example, ArrayList with a flatMap method won't be a monad. Because monad laws prohibit side effects.

    Monad is a formalism. It describes the structure, regardless of content or meaning. People struggle with relating to meaningless (abstract) things. So they come up with metaphors which are not monads.

    See also: conversation between Erik Meijer and Gilad Bracha.

提交回复
热议问题