Sanctuary: S.of(S.Maybe)(void 0) gives Just(undefined) instead of Nothing.

Created on 2 Oct 2019  路  3Comments  路  Source: sanctuary-js/sanctuary

Hi masters!

I'm learning FP with this book mostly-adequate-guide and I'm using sanctuary as a real-world reference. I find having this well documented and nicely implemented library helps a lot! Thanks for developing such great project!

Regarding my question. Maybe I'm using it wrong, but is the value Just(undefined) by design?

What is the proper way to convert an unsafe data (maybe from a remote endpoint) to Nothing/Just that we can pipe into functions safely?

Most helpful comment

Hello, @t1ger-0527. :wave:

Maybe I'm using it wrong, but is the value Just(undefined) by design?

This is very much by design. The type of S.of is Applicative f => TypeRep f -> a -> f a. There are no constraints on a, which means we can replace it with any type we choose, even Undefined.

S.head returns a value from which we can infer whether the input was empty:

> S.head ([])
Nothing

> S.head ([undefined, undefined, undefined])
Just (undefined)

Were null and undefined to be treated specially we would lose this property. In general, special cases should be avoided. :)

What is the proper way to convert an unsafe data (maybe from a remote endpoint) to Nothing/Just that we can pipe into functions safely?

Excellent question. Consider a value xs that we assume to be either null/undefined or an array of integers. We could convert this to a value of type Maybe (Array Integer) in a straightforward manner:

xs == null ? S.Nothing : S.Just (xs)

Depending on our level of confidence in our assumption, the expression above may or may not be satisfactory. If we want to be certain that the result is of type Maybe (Array Integer), we should specify a suitably demanding predicate:

S.filter (S.is ($.Array ($.Integer))) (S.Just (xs))

In the expression above, $ refers to the sanctuary-def module.

S.get, S.gets, and S.parseJson can also be of use when dealing with untrusted inputs.

All 3 comments

Hello, @t1ger-0527. :wave:

Maybe I'm using it wrong, but is the value Just(undefined) by design?

This is very much by design. The type of S.of is Applicative f => TypeRep f -> a -> f a. There are no constraints on a, which means we can replace it with any type we choose, even Undefined.

S.head returns a value from which we can infer whether the input was empty:

> S.head ([])
Nothing

> S.head ([undefined, undefined, undefined])
Just (undefined)

Were null and undefined to be treated specially we would lose this property. In general, special cases should be avoided. :)

What is the proper way to convert an unsafe data (maybe from a remote endpoint) to Nothing/Just that we can pipe into functions safely?

Excellent question. Consider a value xs that we assume to be either null/undefined or an array of integers. We could convert this to a value of type Maybe (Array Integer) in a straightforward manner:

xs == null ? S.Nothing : S.Just (xs)

Depending on our level of confidence in our assumption, the expression above may or may not be satisfactory. If we want to be certain that the result is of type Maybe (Array Integer), we should specify a suitably demanding predicate:

S.filter (S.is ($.Array ($.Integer))) (S.Just (xs))

In the expression above, $ refers to the sanctuary-def module.

S.get, S.gets, and S.parseJson can also be of use when dealing with untrusted inputs.

@davidchambers Thanks for such quick and thoughtful reply! It solves my question perfectly! 鉂わ笍

You are welcome, @t1ger-0527. :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blackxored picture blackxored  路  10Comments

arsaniwilliam picture arsaniwilliam  路  9Comments

fnune picture fnune  路  3Comments

zhangchiqing picture zhangchiqing  路  12Comments

davidchambers picture davidchambers  路  9Comments