maybe

Getting 'a' value from 'Maybe a' return type in Haskell

a 夏天 提交于 2019-11-30 15:35:16
This is going to be hard to explain because there is a decent amount of background detail about the code as a whole that needs to be known to really know functionally what I'm talking about. But I'll try my best to just get my main point across, and hope that it's enough. Let me know if not and I'll add more information. So: I have a Haskell function eval :: WExp -> Memory -> WValue with a bunch of different instances of itself for different cases. For now, knowledge about WExp , Memory , and WValue is not relevant. My problem is that, for a specific instance of eval , I am using a lookup

Implementing boost::optional in c++11

﹥>﹥吖頭↗ 提交于 2019-11-30 14:04:42
I am experimenting with implementing boost::optional like data structure using c++11 features. Here is what I have so far : template<typename T> struct maybe { bool valid; union { T value; }; maybe() : valid(false) {} maybe(const T& _v) { valid = true; new (&value) T(_v); } maybe(const maybe& other) { if (other.valid) { valid = true; new (&value) T(other.value); } else valid = false; } ~maybe() { if (valid) value.~T(); } bool is_valid() { return valid; } operator T&() { if (valid) return value; throw std::bad_exception(); } }; I make use of the unrestricted union feature to create a properly

Why is the use of Maybe/Option not so pervasive in Clojure?

让人想犯罪 __ 提交于 2019-11-29 22:10:09
Why does Clojure, despite such an emphasis on functional paradigm, not use the Maybe / Option monad to represent optional values? The use of Option is quite pervasive in Scala, a functional programming language I use regularly. Clojure is not statically typed, so doesn't need the strict this/that/whatever type declarations that are necessary in haskell (and, I gather, Scala). If you want to return a string, you return a string; if you return nil instead, that's okay too. "Functional" does not correspond exactly to "strict compile-time typing". They are orthogonal concepts, and Clojure chooses

Getting 'a' value from 'Maybe a' return type in Haskell

天大地大妈咪最大 提交于 2019-11-29 21:47:20
问题 This is going to be hard to explain because there is a decent amount of background detail about the code as a whole that needs to be known to really know functionally what I'm talking about. But I'll try my best to just get my main point across, and hope that it's enough. Let me know if not and I'll add more information. So: I have a Haskell function eval :: WExp -> Memory -> WValue with a bunch of different instances of itself for different cases. For now, knowledge about WExp , Memory , and

What exactly makes Option a monad in Scala?

浪子不回头ぞ 提交于 2019-11-29 21:20:17
I know what the monads are and how to use them. What I don't understand is what makes, let's say, Option a monad? In Haskell a monad Maybe is a monad because it's instantiated from Monad class (which has at least 2 necessary functions return and bind that makes class Monad , indeed, a monad). But in Scala we've got this: sealed abstract class Option[+A] extends Product with Serializable { ... } trait Product extends Any with Equals { ... } Nothing related to a monad. If I create my own class in Scala, will it be a monad by default? Why not? Monad is a concept, an abstract interface if you will

Implementing boost::optional in c++11

情到浓时终转凉″ 提交于 2019-11-29 19:49:11
问题 I am experimenting with implementing boost::optional like data structure using c++11 features. Here is what I have so far : template<typename T> struct maybe { bool valid; union { T value; }; maybe() : valid(false) {} maybe(const T& _v) { valid = true; new (&value) T(_v); } maybe(const maybe& other) { if (other.valid) { valid = true; new (&value) T(other.value); } else valid = false; } ~maybe() { if (valid) value.~T(); } bool is_valid() { return valid; } operator T&() { if (valid) return

Using Haskell's “Maybe”, type declarations [beginner's question]

ⅰ亾dé卋堺 提交于 2019-11-29 10:43:51
I've started experimenting with Haskell and have a problem. qqq is a function that should print one string if called with "Nothing" and print other things if called with "Just something". The first attempt seems like working: qqq Nothing = print "There isn't anything to be printed." qqq (Just x) = print "There is something to be printed." >> print x main :: IO () main = qqq (Just 43) But: when I try to make main = qqq (Nothing) it fails ("Ambiguous type variable `a0' in the constraint: (Show a0) arising from a use of 'qqq'") When I want to add type signature if fails: qqq :: Maybe x => x -> IO

What exactly makes Option a monad in Scala?

爱⌒轻易说出口 提交于 2019-11-28 17:25:56
问题 I know what the monads are and how to use them. What I don't understand is what makes, let's say, Option a monad? In Haskell a monad Maybe is a monad because it's instantiated from Monad class (which has at least 2 necessary functions return and bind that makes class Monad , indeed, a monad). But in Scala we've got this: sealed abstract class Option[+A] extends Product with Serializable { ... } trait Product extends Any with Equals { ... } Nothing related to a monad. If I create my own class

Using Haskell's “Maybe”, type declarations [beginner's question]

故事扮演 提交于 2019-11-28 03:58:30
问题 I've started experimenting with Haskell and have a problem. qqq is a function that should print one string if called with "Nothing" and print other things if called with "Just something". The first attempt seems like working: qqq Nothing = print "There isn't anything to be printed." qqq (Just x) = print "There is something to be printed." >> print x main :: IO () main = qqq (Just 43) But: when I try to make main = qqq (Nothing) it fails ("Ambiguous type variable `a0' in the constraint: (Show

Accessing scala.None from Java

血红的双手。 提交于 2019-11-27 15:59:21
How can you access scala.None from Java? The last line causes the compiler to die with "type scala.None does not take parameters". import scala.Option; import scala.Some; import scala.None; final Option<String> object1 = new Some<String>("Hi there"); final Option<String> object2 = new None<String>(); This fails with "cannot find symbol constructor None()": final Option<String> object2 = new None(); This fails with "cannot find symbol variable None": final Option<String> object2 = None; In 2007 this used to work, but then Scala changed. The Java compiler gives error: incompatible types : final