Git Product home page Git Product logo

Comments (11)

muhuk avatar muhuk commented on July 16, 2024

Perhaps also:

(maybe/to-maybe (either/right :foo))
;; => <Just :foo?>

from cats.

 avatar commented on July 16, 2024

Definitely, although we may want to be more explicit with that one with something like either->maybe? I think of to-maybe to something akin to Clojure core's ensure-reduced for Reduced so ensure-maybe may be a better name.

from cats.

muhuk avatar muhuk commented on July 16, 2024

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>

from cats.

 avatar commented on July 16, 2024

Exactly, that's what I had in mind. I don't know if is the best option though, suggestions welcome.

from cats.

yurrriq avatar yurrriq commented on July 16, 2024

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

from cats.

 avatar commented on July 16, 2024

Yep, converting an Either to a Maybe loses information.

from cats.

muhuk avatar muhuk commented on July 16, 2024

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 (maybe/just 3) instead of (maybe/to-maybe 3) and (maybe/nothing) instead of (maybe/to-maybe nil). And if I had some random value:

(if (some? x)
  (maybe/just x)
  (maybe/nothing))

Which is essentially to-maybe I guess, but clearer {{Citation needed}}.

I guess what bothers me in the end is the ambiguity between nil, (maybe/just nil) & (maybe/to-maybe nil).

from cats.

yurrriq avatar yurrriq commented on July 16, 2024

Maybe we could implement this as a protocol, e.g. MaybeTransformable, and then implement ->maybe for different monads.

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))))

from cats.

muhuk avatar muhuk commented on July 16, 2024

Maybe we could implement this as a protocol, e.g. MaybeTransformable and then implement ->maybe for different monads.

I sometimes mzero on either and left-branch later to attach an error message. It would be cool if it would support either as well.


Edit: just checked, I don't call mzero directly.

from cats.

 avatar commented on July 16, 2024

I guess what bothers me in the end is the ambiguity between nil, (maybe/just nil) & (maybe/to-maybe nil).

The point of maybe is to get rid of the possibility of every value being null, nil in Clojure's case, so wrapping a nil in a Just is useless and shouldn't be done. I'd go that far to add an assert to the constructor. In Maybe monad's context nil is treated like Nothing.

from cats.

muhuk avatar muhuk commented on July 16, 2024

That makes perfect sense. to-maybe would make a good addition if nil isn't allowed as a parameter to maybe/just.

from cats.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.