Maybe Functions
91–100 of 103 posts
Re: Maybe Functions
#92Re: Maybe Functions
#93A monad needs some structure in addition to fmap, namely bind and return. These allow you to take a function T -> F(S) and a function S -> F(U) and compose them together to a function T -> F(U).
Re: Maybe Functions
#94That's not a monad, that's just a functor. And that's great, because functors are easier to grok than monads! A functor F consists of two things: - a kind of function on types which transforms any type T to some new type F(T) - a rule which associates to any function f: T -> S a function fmap(f): F(T) -> F(S) That's exactly what the author defines here, to a type T we associate Maybe , and to a function f: T -> S we…
Re: Maybe Functions
#95Agreed with this essay, and I think it rhymes with two others that I've found pretty influential over the past five years: 1. Parse, don't validate ( https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va... ) 2. Pipeline-oriented programming ( https://fsharpforfunandprofit.com/pipeline/ ) In my experience, the "best" code (defining "best" as some abstract melange of "easy to reason about", "easy to modify", "e…
Re: Maybe Functions
#96Earlier quoted context omitted.
Interestingly, Python uses exceptions for basic control flow, like end of for-loop.
Well it uses exceptions in the case your generator is at the end, not usually at the end of a for loop because a for loop by definition iterates over a list until the list is finished. The exception actually occurs when you call next() on a generator which cannot return any more values, or is finished, in which case `StopIteration` is usually raised.
>>> next(iter([]))
Traceback (most recent call last):
File "", line 1, in
StopIteration
>>> next(iter((lambda: (yield 5) if False else None)()))
Traceback (most recent call last):
File "", line 1, in
StopIterationRe: Maybe Functions
#97The real use case is when it really is maybe. (Network call, error handling). Then it's about forcing people to deal with that in a typesafe way and not hiding that it really is maybe.
Re: Maybe Functions
#98Earlier quoted context omitted.
I am for exceptions but it should not be used for basic control flow. Many techs will treat all exceptions as errors.
Interestingly, Python uses exceptions for basic control flow, like end of for-loop.
Here's a quick description from somewhere on the interwebs: An error is an issue in a program that prevents the program from completing its task. In comparison, an exception is a condition that interrupts the normal flow of the program.
Then there is StopIteration, which does not fit well into either of the two above. It's a wart I've learned to treat as a beauty mark.
Re: Maybe Functions
#99Re: Maybe Functions
#100Earlier quoted context omitted.
for a function called "getUser" it is.
Not exactly. "getUser" not having a user to get is not exceptional unless you only have logged in users. If you have logged-out users, then "getUser" should gracefully handle the case of an unknown, logged-out user (either returning null or some other sentinel value).