maybe

Compose partial functions

寵の児 提交于 2019-12-03 12:27:32
问题 I have two PartialFunctions f and g . They have no side effects and are quick to execute. What's the best way to compose them into another partial function h such that h.isDefinedAt(x) iff f.isDefinedAt(x) && g.isDefinedAt(f(x)) ? It's also OK if h is a function returning an Option rather than a partial function. I'm disappointed that f andThen g does not do what I want: scala> val f = Map("a"->1, "b"->2) f: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2) scala> val g = Map(1

Implementing the Haskell-MaybeMonad in F# - how can we get this lazy?

坚强是说给别人听的谎言 提交于 2019-12-03 09:07:33
we are trying to build the Haskell-MaybeMonad sample from http://www.haskell.org/all_about_monads/html/maybemonad.html in F#. The idea is to search for a mailaddress in two dictionaries. If one of the both lookups returns a result we look into the third one. let bindM x k = match x with | Some value -> k value | None -> None let returnM x = Some x type MaybeBuilder() = member this.Bind(x, k) = bindM x k member this.Return(x) = returnM x member this.ReturnFrom(x) = x member this.Delay(f) = f() let maybe = MaybeBuilder() //Sample dictionaries let fullNamesDb = [("Bill Gates", "billg@microsoft

Idiomatic error handling in Clojure

南楼画角 提交于 2019-12-03 08:17:20
问题 When I put on my C hat, I think that maybe idiomatic Clojure just does the simple thing and checks return values. When I put on my Java hat (reluctantly, I must add), I think to myself that since Clojure runs on the JVM the natural way must be to use JVM exceptions. When I put on my functional hat, I'm thinking that there must be some sort of monadic construction or threading macro that can handle errors in a composable way. So what's the idiomatic way to handle errors in a Clojure program?

How to get the value of a Maybe in Haskell

怎甘沉沦 提交于 2019-12-02 17:04:09
I'm relatively new to Haskell and began to read "Real World Haskell". I Just stumbled over the type Maybe and have a question about how to receive the actual value from a Just 1 for example. I have written the following code: combine a b c = (eliminate a, eliminate b, eliminate c) where eliminate (Just a) = a eliminate Nothing = 0 This works fine if I use: combine (Just 1) Nothing (Just 2) But if I change, for example, 1 to a String it doesn't work. I think I know why: because eliminate has to give back one type, which is, in this case, an Int . But how can I change eliminate to deal at least

What's the difference between undefined in Haskell and null in Java?

强颜欢笑 提交于 2019-12-02 16:58:07
Both are terms whose type is the intersection of all types (uninhabited). Both can be passed around in code without failing until one attempts to evaluate them. The only difference I can see is that in Java, there is a loophole which allows null to be evaluated for exactly one operation, which is reference equality comparison ( == )--whereas in Haskell undefined can't be evaluated at all without throwing an exception. Is this the only difference? Edit What I'm really trying to get at with this question is, why was including null in Java such an apparently poor decision, and how does Haskell

Simply typed lambda calculus with failure, in Haskell

一笑奈何 提交于 2019-12-01 18:14:36
问题 I'm a newcomer to Haskell, so apologies if this question doesn't make too much sense. I want to be able to implement simply typed lambda expressions in Haskell in such a way that when I try to apply an expression to another of the wrong type, the result is not a type error, but rather some set value, e.g. Nothing. At first I thought using the Maybe monad would be the right approach, but I've not been able to get anything working. I wondered what, if any, would be the correct way to do this.

Right way to forcibly convert Maybe a to a in Elm, failing clearly for Nothings

随声附和 提交于 2019-12-01 15:55:32
Okay, what I really wanted to do is, I have an Array and I want to choose a random element from it. The obvious thing to do is get an integer from a random number generator between 0 and the length minus 1, which I have working already, and then applying Array.get , but that returns a Maybe a . (It appears there's also a package function that does the same thing .) Coming from Haskell, I get the type significance that it's protecting me from the case where my index was out of range, but I have control over the index and don't expect that to happen, so I'd just like to assume I got a Just

Right way to forcibly convert Maybe a to a in Elm, failing clearly for Nothings

一笑奈何 提交于 2019-12-01 14:46:33
问题 Okay, what I really wanted to do is, I have an Array and I want to choose a random element from it. The obvious thing to do is get an integer from a random number generator between 0 and the length minus 1, which I have working already, and then applying Array.get, but that returns a Maybe a . (It appears there's also a package function that does the same thing.) Coming from Haskell, I get the type significance that it's protecting me from the case where my index was out of range, but I have

The Maybe result from Map.lookup is not type checking with my Monad Transformer stack

删除回忆录丶 提交于 2019-12-01 14:39:50
问题 I am going though the following paper: Monad Transformers Step by Step. In section 2.1 "Converting to Monadic Style", a function is converted to return Value in the Eval1 monad. This part of the function doesn't make sense to me: eval1 env (Var n) = Map.lookup n env The result of that will be Maybe Value however the function's type signature is: eval1 :: Env → Exp → Eval1 Value The function is failing to type check, and the error seems obvious to me. Yet the author specifically states that

Is there a standard option workflow in F#?

时光总嘲笑我的痴心妄想 提交于 2019-12-01 02:08:39
Is there an option (maybe) wokflow (monad) in the standrd F# library? I've found a dozen of hand-made implementations ( 1 , 2 ) of this workflow, but I don't really want to introduce non-standard and not very trusted code into my project. And all imaginable queries to google and msdn gave me no clue where to find it. There's no Maybe monad in the standard F# library. You may want to look at FSharpx , a F# extension written by highly-qualified members of F# community, which has quite a number of useful monads. kvb There's no standard computation builder for options, but if you don't need things