=> Monad m where (>>=) :: forall a b. m a -> (a -> m b) -> m b (>>) :: forall a b. m a -> m b -> m b m >> k = m >>= \_ -> k {-# INLINE (>>) #-} return :: a -> m a return = pure fail :: String -> m a fail s = errorWithoutStackTrace s
a = Optional.of(2) a ==> Optional[2] jshell> Optional<Integer> b = Optional.of(3) b ==> Optional[3] jshell> a.flatMap( x -> // with `flatMap` & `map` ...> b.map( y -> ...> x * y ...> ) ...> ) $3 ==> Optional[6]
Some(2) scala> val b = Some(3) b: Some[Int] = Some(3) scala> a.flatMap { x => // with `flatMap` & `map` | b.map { y => | x * y | } | } res0: Option[Int] = Some(6) scala> for { // with `for` expression | x <- a | y <- b | } yield x * y res1: Option[Int] = Some(6)
Just 3 > :{ -- with `>>=` & `return` | a >>= \x -> | b >>= \y -> | return $ x * y | :} Just 6 > :{ -- with `do` notation | do | x <- a | y <- b | return $ x * y | :} Just 6
here) ;; (0-arity pattern here) ([v] (cond ; blank lines omitted (not (nil? *context*)) *context* (satisfies? p/Contextual v) (p/-get-context v) :else (throw-illegal-argument (str "No context is set and it can not be automatically " "resolved from provided value"))))) nil
establishes a concrete type as a member of a contex A great example is the Maybe monad type Just. It implements this abstraction to establish that Just is part of the Maybe monad." (-get-context [_] "Get the context associated with the type.")) nil
:as xs] s] (cond (and x y (= s "*")) (conj ys (* y x)) (and x y (= s "+")) (conj ys (+ y x)) (and x y (= s "-")) (conj ys (- y x)) :else (conj xs (Double/parseDouble s)))) (defn solve-rpn [s] (as-> s v (str/split v #"\s+") (reduce folding-function () v) (first v)))
nothing lift-m for lifting conj function (defn- read-maybe [s] (try (maybe/just (Double/parseDouble s)) (catch NumberFormatException _ (maybe/nothing)))) (defn- folding-function' [[x y & ys :as xs] s] (cond (and x y (= s "*")) (maybe/just (conj ys (* y x))) (and x y (= s "+")) (maybe/just (conj ys (+ y x))) (and x y (= s "-")) (maybe/just (conj ys (- y x))) :else ((m/lift-m 1 #(conj xs %)) (read-maybe s))))
逆ポーランド記法電卓 / 14.6 安全な逆ポーランド記法電卓を作ろう / Making a Safe RPN Calculator 14.8 モナドを作る / 『すごいHaskellたのしく学ぼう!』 Learn You a Haskell for Great Good! Reverse Polish Notation Calculator Making Monads