monads

Difference in capability between fmap and bind?

别来无恙 提交于 2019-11-30 08:43:07
I'm new to functional programming (coming from javascript), and I'm having a hard time telling the difference between the two, which is also messing with my understand of functors vs. monads. Functor: class Functor f where fmap :: (a -> b) -> f a -> f b Monad (simplified): class Monad m where (>>=) :: m a -> (a -> m b) -> m b fmap takes a function and a functor, and returns a functor. >>= takes a function and a monad, and returns a monad. The difference between the two is in the function parameter: fmap - (a -> b) >>= - (a -> m b) >>= takes a function parameter that returns a monad. I know

Try monad in Java 8

↘锁芯ラ 提交于 2019-11-30 08:31:13
Is there a built-in support for monad that deals with exception handling? Something similar to Scala's Try . I am asking because I don't like unchecked exceptions. The "better-java-monads" project on GitHub has a Try monad for Java 8 here . John McClean There are at least two generally available (e.g. on Maven Central) - Vavr and Cyclops both have Try implementations that take a slightly differing approach. Vavr's Try follows Scala's Try very closely. It will catch all 'non-fatal' exceptions thrown during the execution of it's combinators. Cyclops Try will only catch explicitly configured

Binding functions that take multiple arguments

我的未来我决定 提交于 2019-11-30 08:24:15
After reading some very basic haskell now I know how to "chain" monadic actions using bind , like: echo = getLine >>= putStrLn (>>=) operator is very handy in this fashion, but what if I want to chain monadic actions (or functors) that take multiple arguments? Given that (>>=) :: m a -> (a -> m b) -> m b it seems like (>>=) can supply only one argument. For example, writeFile takes two arguments (a FilePath and the contents). Suppose I have a monadic action that returns a FilePath , and another action that returns a String to write. How can I combine them with writeFile , without using the do

Is Java 8 missing an OptionalBoolean?

梦想与她 提交于 2019-11-30 07:56:18
As a primitive version of Optional *, Java 1.8 provides OptionalInt , OptionalLong and OptionalDouble . But I cannot find the equivalent OptionalBoolean class. Are there any technical reasons against having an OptionalBoolean ? * An Optional may or may not have the presence of a value, is used as an alternative to null . Eran This quote explains the considerations behind having primitive streams. I'm assuming the same applied to primitive Optionals. In short, primitive streams (and probably Optionals as well) were created for performance reasons. They didn't create them for all 8 primitive

StackOverflow in continuation monad

独自空忆成欢 提交于 2019-11-30 07:07:20
Using the following continuation monad: type ContinuationMonad() = member this.Bind (m, f) = fun c -> m (fun a -> f a c) member this.Return x = fun k -> k x let cont = ContinuationMonad() I fail to see why the following gives me a stack overflow: let map f xs = let rec map xs = cont { match xs with | [] -> return [] | x :: xs -> let! xs = map xs return f x :: xs } map xs id;; let q = [1..100000] |> map ((+) 1) While the following doesn't: let map f xs = let rec map xs = cont { match xs with | [] -> return [] | x :: xs -> let! v = fun g -> g(f x) let! xs = map xs return v :: xs } map xs id;;

Implementing monads in JavaScript

谁说胖子不能爱 提交于 2019-11-30 06:57:04
问题 Now that node.js supports ECMAScript Harmony generators we can write monadic code succinctly ala do blocks in Haskell: function monad(unit, bind) { return function (f) { return function () { var g = f.apply(this, arguments); return typeOf(g) === "Generator" ? send() : unit(g); function send(value) { var result = g.next(value); if (result.done) return unit(result.value); else return bind(result.value, send); } }; }; } function typeOf(value) { return Object.prototype.toString.call(value).slice

Monad with no wrapped value?

核能气质少年 提交于 2019-11-30 06:52:33
Most of the monad explanations use examples where the monad wraps a value. E.g. Maybe a , where the a type variable is what's wrapped. But I'm wondering about monads that never wrap anything. For a contrived example, suppose I have a real-world robot that can be controlled, but has no sensors. Maybe I'd like to control it like this: robotMovementScript :: RobotMonad () robotMovementScript = do moveLeft 10 moveForward 25 rotate 180 main :: IO () main = liftIO $ runRobot robotMovementScript connectToRobot In our imaginary API, connectToRobot returns some kind of handle to the physical device.

Folding, function composition, monads, and laziness, oh my?

笑着哭i 提交于 2019-11-30 06:39:15
I am puzzled. I can write this: import Control.Monad main = print $ head $ (foldr (.) id [f, g]) [3] where f = (1:) g = undefined and the output is 1 . That makes sense, because it reduces to: main = print $ head $ ((1:) . undefined . id) [3] main = print $ head $ (1:) ((undefined . id) [3]) main = print $ head $ 1 : ((undefined . id) [3]) main = print $ 1 But if I use a vaguely similar monadic technique, it doesn't work the same: import Control.Monad main = print $ (foldr (<=<) return [f, g]) 3 where f = const Nothing g = undefined This hits prelude.Undefined . Which is odd, because I would

What's the current status of restricted monads?

拜拜、爱过 提交于 2019-11-30 06:00:01
Going back to at least the late 1990s there have been people wishing for the integration of restricted monads into Haskell in a friendly way. For example, without restricted monads you can't make an efficient monad out of Set , Map or probability distributions . Here's a SO question from a few years ago where someone else ran afoul of this problem. There are various workarounds that people have come up with, including: Creating a new type class for every possible restriction. Using Template Haskell . Using Constraint Kinds . None of these approaches seem to be "canonical" however. I found a

Using different monads in for-comprehension

我与影子孤独终老i 提交于 2019-11-30 05:32:51
问题 Can different monads be used in for-comprehensions? Here's the code that uses map case class Post(id: Int, text: String) object PostOps { def find(id: Int) : Option[Post] = if (id == 1) Some(Post(1, "text")) else None def permitted(post: Post, userId: Int) : Try[Post] = if (userId == 1) Success(post) else Failure(new UnsupportedOperationException) def edit(id: Int, userId : Int, text: String) = find(id).map(permitted(_, userId).map(_.copy(text = text))) match { case None => println("Not found