-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Helpers for ensuring that values are wrapped in a certain type #133
Comments
Perhaps also: (maybe/to-maybe (either/right :foo))
;; => <Just :foo?> |
Definitely, although we may want to be more explicit with that one with something like |
So it would work like this? (maybe/to-maybe (either/right :foo))
;; => #<Just #<Left :foo?>>
(maybe/just nil)
;; => #<Just nil>
(maybe/to-maybe nil)
;; => #<Nothing> |
Exactly, that's what I had in mind. I don't know if is the best option though, suggestions welcome. |
I like that and maybe something like this: (either->maybe (either/right :foo))
;; => #<Just :foo>
(either->maybe (either/left :bar))
;; => #<Nothing> I'm not sure that makes sense, though.. Pertinent and interestring: -- | The 'listToMaybe' function returns 'Nothing' on an empty list
-- or @'Just' a@ where @a@ is the first element of the list.
--
-- ==== __Examples__
--
-- Basic usage:
--
-- >>> listToMaybe []
-- Nothing
--
-- >>> listToMaybe [9]
-- Just 9
--
-- >>> listToMaybe [1,2,3]
-- Just 1
--
-- Composing 'maybeToList' with 'listToMaybe' should be the identity
-- on singleton/empty lists:
--
-- >>> maybeToList $ listToMaybe [5]
-- [5]
-- >>> maybeToList $ listToMaybe []
-- []
--
-- But not on lists with more than one element:
--
-- >>> maybeToList $ listToMaybe [1,2,3]
-- [1]
--
listToMaybe :: [a] -> Maybe a
listToMaybe [] = Nothing
listToMaybe (a:_) = Just a |
Yep, converting an |
I'm just playing with the idea. With dynamic typing, things are quite different. This bothers me a bit: (bind (maybe/just nil) maybe/to-maybe)
;; => #<Nothing> I guess I would write (if (some? x)
(maybe/just x)
(maybe/nothing)) Which is essentially I guess what bothers me in the end is the ambiguity between |
Maybe we could implement this as a protocol, e.g. Something like: (defprotocol MaybeTransformable
(->maybe [mv] "Transform a given monadic value into a Maybe."))
(extend-type Left
MaybeTransformable
(->maybe [_] (maybe/nothing)))
(extend-type Right
MaybeTransformable
(->maybe [mv] (maybe/just (.-v mv)))) |
I sometimes Edit: just checked, I don't call |
The point of maybe is to get rid of the possibility of every value being null, |
That makes perfect sense. |
The counterparts of fns like
from-maybe
, take a value and ensure that they are wrapped in the correct type.An example of how it'd behave for Maybe:
The text was updated successfully, but these errors were encountered: