question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Maybe.map not functioning as documented.

See original GitHub issue

Maybe.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:closed
  • Created 7 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
davidchamberscommented, Jan 27, 2017

Hello, @zfoxdev. Welcome to Sanctuary!

While I am encountering the following functionality:

Maybe#map :: Maybe a ~> (a -> b) -> Just b

The first thing to recognize is that Maybe is a unary type constructor. Its type is as follows:

Maybe :: Type -> Type

So Maybe is not a type, but Maybe String is a type, as is Maybe a.

Just, on the other hand, is not a type constructor but a data constructor. It’s a function for creating values of type Maybe a. Its type is as follows:

Just :: a -> Maybe a

Let’s consider the types involved in S.toMaybe('hello').map(str => undefined):

toMaybe :: a? -> Maybe a
toMaybe("hello") :: Maybe String
toMaybe("hello").map :: Maybe String ~> (String -> b) -> Maybe b
toMaybe("hello").map(str => undefined) :: Maybe Undefined

The type of the expression is Maybe Undefined. This type has two members: Nothing() and Just(undefined). It’s not a particularly useful type, as we have Boolean 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 with null or undefined by choice). This being the case, I suggest defining a wrapper function of type String -> Maybe Foo rather than String -> Foo? (the ? indicates the possibility of undefined). You could then use chain rather than map.

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 with Just(undefined) rather than with fromMaybe. Avoid getting into that position and the fromMaybe 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. 😃

2reactions
safarelicommented, Jan 27, 2017

Behaviour of map is correct. I think you want to do something like this you can do this:

// return Nothing instead of undefined
m.chain(str => Nothing)// Nothing

or

// return undefined
const f = str => undefined
// but use toMaybe to convert to maybe
m.chain(a => S.toMaybe(f(a))) //Nothing

What’s happening in your case is that, you have value of type Maybe String and it’s map takes function from String to b and returns Maybe b

:: Maybe String -> (String -> b) -> Maybe b

b for you is substituted by Nullable a or ?a which means that it is either null/undefined or some value a, and result is Maybe (Nullable a)

:: Maybe String -> (String -> Nullable a) -> Maybe (Nullable a)

so Just(undefined) is valid value.


@davidchambers I was writing answer at the same time :d

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found