Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
dotnetCarpenter
@dotnetCarpenter
hehe the return value of Right ({"$1": "2020-10-10"}) is problematic...
I'm sorry I don't have more time to work on this, right now. But I'm excited to see where this goes.
David Chambers
@davidchambers
:ok_hand:
dotnetCarpenter
@dotnetCarpenter
I found some hours to take another look at it and I came up with a validate function that seems to handle most Types, returns a Left of errors or Right with the test value. It is far from optimal and it struggle with naming properties in all cases (like when they are a nested type). But I thought I could post it here and get some feedback.
const validate = t => S.pipe ([
  x => S.unchecked.Pair (S.unchecked.append (_test (t) () (x)) ([])) (x),

  S.unchecked.pair (results => x => {
    let errors = S.unchecked.lefts (flatten (results));

    return S.size (errors) > 0
      ? S.Left (errors)
      : S.Right (x);
  }),
]);

const _test = t => name => x => {
  if (x == null || t.keys.length === 0) {
    return t._test () (x)
      ? S.Right (x)
      : x == null
        ? S.Left ({error:'MissingValue', type: t.type, name: name || t.name, value:x})
        : S.Left ({error:'WrongValue', type: t.type, name: name || t.name, value:x});
  }

  return x != null && t.keys.length > 0
    ? t.keys.map (k => _test (t.types[k]) (k) (t.extractors[k](x)[0]))
    : _test (t) () (x);
}

const flatten = array => (
  !Array.isArray(array)
    ? [array]
    : array.reduce((a,b) => a.concat(flatten(b)), [])
);
David Chambers
@davidchambers
I find this flatten function scary, @dotnetCarpenter. Could we use S.join instead?
Can you explain the reason for treating x == null specially in _test?
dotnetCarpenter
@dotnetCarpenter
This solution is characterized by not having so much time. The flatten is last-minute solution to the fact that _test sometimes returns an array and sometimes not. Always returning an array (and use S.join) would be much better.
The null stuff is about testing "validate ($.Undefined) (undefined) and make sure it succeeds.

I just notice that the last line in _test might create an infinite loop.

return x != null && t.keys.length > 0
    ? t.keys.map (k => _test (t.types[k]) (k) (t.extractors[k](x)[0]))
    : _test (t) () (x);

The false part of the ternary should never happen, I think...

The null check here is because t.extractors[k] will crash if k is null.
David Chambers
@davidchambers
I don't see how k could be null. đŸ€”
jeppe2004
@jeppe2004:matrix.org
[m]
Sorry, I meant x
Hmm no that doesn’t make sense. Sorry I’m not at my computer atm
David Chambers
@davidchambers
It is only safe to apply one of a type's extractors to x after determining that x is a member of that type.
jeppe2004
@jeppe2004:matrix.org
[m]
That might be the error I saw then. Need to investigate when that happens. Thanks for info!
David Chambers
@davidchambers
:thumbsup:
jeppe2004
@jeppe2004:matrix.org
[m]
Is the result of calling an extractor always a 1-tuple?
David Chambers
@davidchambers
An extractor's job is to expose inner values. I'll provide some examples

//    Maybe$1 :: Maybe a -> Array a
const Maybe$1 = S.maybe ([]) (a => [a]);
//    Either$1 :: Either a b -> Array a
const Either$1 = S.either (a => [a]) (b => []);

//    Either$2 :: Either a b -> Array b
const Either$2 = S.either (a => []) (b => [b]);
//    Array$1 :: Array a -> Array a
const Array$1 = as => as;
Array$1 returns all the elements of the array. ;)
MichaƂ Chruƛciel
@majkelch
Hi all. I would like to use Sanctuary with Vue 3.x (Composition API), Vite (2.6.x) and Yarn 3.x. Vite runs without problem, but when navigating to any page, I am running into error from Either/index.js: var custom = util.inspect.custom; <-- TypeError: Cannot read properties of undefined (reading 'custom'). Do you know how to resolve this issue?
My usage of Sanctuary looks like this api.utils.js:
import * as S from 'sanctuary'
export const processData = ({ data }) => S.pipe ([ S.head, S.props(['value', 'result']) ]) (data.rows)
David Chambers
@davidchambers
This issue sounds familiar, @majkelch. đŸ€”
Sanctuary currently uses this check to determine whether to use require:
typeof module === 'object' && typeof module.exports === 'object'
We assume that require ('util') will work in this context. This is not a safe assumption.
MichaƂ Chruƛciel
@majkelch
OK, so the issue is that Vite works with ESM and Sanctuary does not support that format?
David Chambers
@davidchambers
Not quite. Sanctuary is written as a CommonJS module, and CommonJS modules can be imported from ES modules. The problem is that we assume that typeof module === 'object' && typeof module.exports === 'object' implies Node.js.
The long-term fix is to update Sanctuary to use Symbol.for ('nodejs.util.inspect.custom'), obviating the need for require ('util'), circumventing this issue.
Might you be able to support require ('util') from your end in the interim?
MichaƂ Chruƛciel
@majkelch
Not sure, I will check that. Thank you.
David Chambers
@davidchambers
Please open an issue for this, if you have time. I would like to fix the underlying problem. :)
MichaƂ Chruƛciel
@majkelch
David Chambers
@davidchambers
Thank you, @majkelch!
MichaƂ Chruƛciel
@majkelch
OK, so maybe there is a way to somehow transform sanctuary or use require ('util'), but I don't know how. For now I use downloaded bundle from CDN and did const S = window.sanctuary in utils file... I also tried using importmaps, but it looks like it interferes with Vite.
David Chambers
@davidchambers
That sounds like a good workaround. I will fix the underlying issue before the next major release.
MichaƂ Chruƛciel
@majkelch
Great, thanks!
David Chambers
@davidchambers
@majkelch, I just opened sanctuary-js/sanctuary-identity#37, one of several similar pull requests that together will address your issue. :)
MichaƂ Chruƛciel
@majkelch
@davidchambers I saw that one, awesome, thanks!
David Chambers
@davidchambers
:thumbsup:
Alisson Cavalcante Agiani
@thelinuxlich
Do you use Sanctuary with a FRP lib?
kee-oth
@kee-oth
Hi! I'm new to using Sanctuary but I'm enjoying it so far (coming from Ramda). Are there any resources for learning Sanctuary for JS devs without a strong FP background? I'll admit that usage of Maybe and Either are a bit over my head right now and I'd appreciate some more hand-holding regarding learning them, ha. Also curious if anyone has made any editor/IDE plugins to use sanctuary-def and get type errors before pre-runtime (not sure if that's possible). Thanks!
David Chambers
@davidchambers

@kee-oth, welcome to the community! :wave:

Awesome FP JS has a list of learning resources. I do not have a specific recommendation.

I'm not aware of any way to leverage sanctuary-def statically. That would be sweet!

jeppe2004
@jeppe2004:matrix.org
[m]
@kee-oth in the beginning it was helpful for me to use // @ts-check in vscode. It will use the ts definition which is mostly correct. The definitions does not cover sanctuary 100% and will get confused sometimes and give you false errors. You can use the run-time type checking to check if they are really an error.
dotnetCarpenter
@dotnetCarpenter

@kee-oth It helped me a lot to read https://mostly-adequate.gitbook.io/mostly-adequate-guide/ to learn about ADTs like Maybe and Either and how to un-nest types. But I would scan the examples as many of the later ones are broken. The concepts are correct but somehow over time the examples were updated to a state where they do not actually run as advertised. The maintainer says he does not have time to go through the examples at this time..

Other than that, me and two others bought https://leanpub.com/fp-made-easier... it does not have Sanctuary.js examples though but the most popular data types should work the same with either language/framework.

Actually, I'm reading through the content of fp-made-easier and it seems to be a PureScript book.
dotnetCarpenter
@dotnetCarpenter

@kee-oth if you use vscode then turning on bracket pair colorization is a must :) https://code.visualstudio.com/updates/v1_60#_high-performance-bracket-pair-colorization

"editor.bracketPairColorization.enabled": true

image.png
Alisson Cavalcante Agiani
@thelinuxlich
Are there any other Future/Task libs good with Sanctuary besides Fluture?
1 reply
kee-oth
@kee-oth

@dotnetCarpenter and @jeppe2004:matrix.org : Thank you for the tips and resources! I appreciate it! 🙌 Checking them out now.

@davidchambers Thank you for the welcome message and resources! Curious, will the current Sanctuary docs be updated to be searchable? Thanks!