funcool

A channel for discussing and asking questions about Funcool libraries https://github.com/funcool/
jaen 2015-12-02T15:19:26.000083Z

Is it just me or is there no convenience functions to create Maybe or Either from something potentially nullable?

niwinz 2015-12-02T15:22:16.000084Z

Can you give any example of that you are doing?

jaen 2015-12-02T15:23:33.000085Z

Basically something like this:

(let [result (something-that-might-return-null ...)]
  (if result
    (just result)
    (nothing)))

jaen 2015-12-02T15:23:54.000086Z

And that's quite verbose and I was wondering if I was missing a convenience function that could encapsulate that

jaen 2015-12-02T15:24:53.000087Z

(and something analogous for Either where you would specify the Left value if the result was nil)

niwinz 2015-12-02T15:25:32.000090Z

I agree for Maybe but not for Either

niwinz 2015-12-02T15:26:12.000092Z

That behavior for either is too much opinionated

jaen 2015-12-02T15:28:00.000093Z

Well, I partly agree that Either is more semantically rich than Maybe here and there's more reason to handle both Right and Left by hand

niwinz 2015-12-02T15:28:24.000094Z

the cats.monad.exception already provides a "Either" analogous abstraction just for succes and failure and also provides a try-on macro that allows easy wrap any result in a proper type

niwinz 2015-12-02T15:28:47.000095Z

(exc/try-on (somethird-party-fn-that-raises-exceptions))

2015-12-02T15:29:02.000096Z

note that nil already participates in the Maybe abstraction, is equivalent to Nothing

niwinz 2015-12-02T15:29:16.000097Z

Right!

niwinz 2015-12-02T15:29:22.000098Z

I forgot about it...

niwinz 2015-12-02T15:29:47.000099Z

for use that you just need to (require '[cats.builtin])

jaen 2015-12-02T15:30:25.000100Z

And then I don't have to wrap it by hand every time? I see.

niwinz 2015-12-02T15:30:58.000101Z

hmm

niwinz 2015-12-02T15:31:59.000102Z

no, I think we should create the analogous try-on macro for maybe for that...

niwinz 2015-12-02T15:32:29.000103Z

the nil is treated as nothing, but any other value is not treated as Just...

jaen 2015-12-02T15:35:17.000104Z

I see, I'll just keep doing the manual wrapping for now then

niwinz 2015-12-02T15:40:41.000105Z

Nice!

niwinz 2015-12-02T15:40:48.000106Z

We surelly add the helper for the next version

2015-12-02T15:51:46.000107Z

we could call it ensure-maybe, its behavior will be similar to core's ensure-reduced

jaen 2015-12-02T16:02:32.000108Z

Since you already have maybe/from-maybe then maybe to-maybe or from-nullable could be a good choice as well?

2015-12-02T16:43:07.000109Z

I dig to-maybe 👍, happy to merge a patch with these additions

2015-12-02T16:43:11.000110Z

I've opened an issue to not forget about it https://github.com/funcool/cats/issues/133

niwinz 2015-12-02T16:47:28.000112Z

Hmm

niwinz 2015-12-02T16:47:52.000113Z

to-maybe is ok, but I'm will be more happy with something like try-on naming

niwinz 2015-12-02T16:48:14.000114Z

to maybe is just a conversion, but I think it can be a macro

niwinz 2015-12-02T16:48:26.000115Z

just for avoid additional call stack frame

niwinz 2015-12-02T16:48:50.000116Z

or provide the two variants, macro and function

niwinz 2015-12-02T16:48:54.000117Z

that is possible in cljs...