Maybe.map not functioning as documented.
See original GitHub issueMaybe.map is documented as follows:
Maybe#map :: Maybe a ~> (a -> b) -> Maybe b
While I am encountering the following functionality:
Maybe#map :: Maybe a ~> (a -> b) -> Just b
For example the following code:
S.toMaybe('hello').map(str => undefined)
Will result in:
Just(undefined)
When the following is desired:
Nothing
This leads to the following issue with the code bellow:
let str = S.toMaybe('hello').map(str => undefined)
let result = S.fromMaybe('world', str)
TypeError: Type-variable constraint violation
fromMaybe :: a -> Maybe a -> a
^ ^
1 2
1) "world" :: String
2) undefined :: Undefined
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Maybe - core 1.0.5 - Elm Packages
Represent values that may or may not exist. It can be useful if you have a record field that is only filled in...
Read more >Maybe — returns 0.19.0 documentation - Read the Docs
Returns inner value for successful container. map(function)[source] ...
Read more >The Maybe result from Map.lookup is not type checking with ...
The function is failing to type check, and the error seems obvious to me. Yet the author specifically states that this will work:...
Read more >Maybe (RxJava Javadoc 3.1.5) - ReactiveX
Returns a Maybe based on applying a specified function to the item emitted by the current Maybe , where that function returns a...
Read more >Array.prototype.map() - JavaScript - MDN Web Docs
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello, @zfoxdev. Welcome to Sanctuary!
The first thing to recognize is that
Maybe
is a unary type constructor. Its type is as follows:So
Maybe
is not a type, butMaybe String
is a type, as isMaybe a
.Just
, on the other hand, is not a type constructor but a data constructor. It’s a function for creating values of typeMaybe a
. Its type is as follows:Let’s consider the types involved in
S.toMaybe('hello').map(str => undefined)
:The type of the expression is
Maybe Undefined
. This type has two members:Nothing()
andJust(undefined)
. It’s not a particularly useful type, as we haveBoolean
if we need a type with two members.My suggestion is to replace or wrap
str => undefined
. I assume it represents a third-party function (since a Sanctuary user would not deal withnull
orundefined
by choice). This being the case, I suggest defining a wrapper function of typeString -> Maybe Foo
rather thanString -> Foo?
(the?
indicates the possibility ofundefined
). You could then usechain
rather thanmap
.The
fromMaybe
type error is to be expected. Sanctuary is preventing you from using an expression which can evaluate to values of different types. The problem lies withJust(undefined)
rather than withfromMaybe
. Avoid getting into that position and thefromMaybe
issue will resolve itself.Let me know if this is helpful. I’m happy to clarify anything I have not done a good job of explaining. 😃
Behaviour of map is correct. I think you want to do something like this you can do this:
or
What’s happening in your case is that, you have value of type
Maybe String
and it’smap
takes function fromString
tob
and returnsMaybe b
b
for you is substituted byNullable a
or?a
which means that it is eithernull/undefined
or some valuea
, and result isMaybe (Nullable a)
so Just(undefined) is valid value.
@davidchambers I was writing answer at the same time :d