There is a given function, that is fixed and must not be changed:
const validate = v => v === "fred" ? "Y" : undefined
Now, because I would like to be functional and would like to avoid null-checks I've decided to use Maybe
(ramda-fantasy
) for validation function:
const vv = val => Maybe(val).map(v=> validate(v)).getOrElse("N")
vv
should return Y
if it's called with "fred"
otherwise N
.
vv(null)
returnsN
-> OKvv("fred")
returnsY
-> OKvv("ding")
returnsundefined
-> wrong, expectedN
The problem is, that Maybe.map
always returns Just
, that I do not understand (because I'm just learning it). For me I would be beneficial if this function would behave in similar way to Maybe(val)
that returns None
or Just
.
I have two question:
- Why
Maybe.map
does not handle null/undefined? - How to rewrite
vv
that it would return expected values in all three cases?
EDIT: I would like to explain why validate should not be changed: it's just simple example of function coming from external library. I wanted to see how easy/hard is to integrate such libraries into functional programming. So is not about string operations, just about streaming values when at some point it evaluates to null.
EDIT2:
This solves my problem:
Either.ofNullable = Either.prototype.ofNullable = function (value) {
return value == null ? Either.Left("is null") : Either.Right(value);
};
EDIT3: I've implemented my own Either with missing functionality: https://github.com/maciejmiklas/functional-ts/blob/main/src/either.ts