I never use pure for Maybe, but sometimes for Either. I guess the latter has more of a monadic state character than a data character to me, compared to Maybe. depends on what the function does, how abstract it is
espcially for a Maybe do-block, because I don't often see Maybe do-blocks, so it takes a couple of seconds to figure out, whereas a Just immediately makes it clear
foo should be of type String to Int. The embedding into Maybe happens at the Maybe-mandating use-site of foo’s return value. A smart type system will do that automatically for you.
(I am assuming that there are no branches of foo that return Nothing and that 0 is a placeholder of significant processing of the input String. Otherwise foo could be merely replaced with const 0.)
For example given these contrived examples:
Which do your prefer?
Where does that "shepherd" you and others if anywhere?
This post explains language shepherding:
https://nibblestew.blogspot.com/2020/03/its-not-what-programming-languages-do.html?m=1
I never use
pure
forMaybe
, but sometimes forEither
. I guess the latter has more of a monadic state character than a data character to me, compared toMaybe
. depends on what the function does, how abstract it isI'd use
Just
in those exmples, but I'd usepure
in ado
block, even it if were explicitlyMaybe
.in that case, I think I would prefer splitting into functions and using
=<<
I always prefer a specialised versions for lists, maybe, either - makes type inference in code reviews way easier
espcially for a
Maybe
do-block, because I don't often seeMaybe
do-blocks, so it takes a couple of seconds to figure out, whereas a Just immediately makes it clear- I tend to generalize once it makes sense - it's fully mechanical change and keeps used "jargon" close to actual problem
I also don't see maybe do blocks much. Is that a good or bad thing?
pure
if indo
block, otherwiseJust
.I just found a maybe do block I had written earlier and thought how clever I had been
foo should be of type String to Int. The embedding into Maybe happens at the Maybe-mandating use-site of foo’s return value. A smart type system will do that automatically for you.
This in particular is simply wrong.
(I am assuming that there are no branches of foo that return Nothing and that 0 is a placeholder of significant processing of the input String. Otherwise foo could be merely replaced with const 0.)
these examples are surely just stripped down for brevity.
Is the query merely about Just vs pure? Or does it encompass design of the type signatures? Because I am pointing out an error in the latter.