How to use sanctuary - 10 common examples

To help you get started, we’ve selected a few sanctuary examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Risto-Stevcev / lazy-either / examples / readme2.js View on Github external
//:: String -> String -> String
const join = S.curry2(path.join)

//:: String -> LazyEither (Either e [String])
const ls = path => LazyEither(resolve =>
  fs.readdir(path, (err, files) => resolve(err ? S.Left(err) : S.Right(S.map(join(path), files)))))

//:: String -> LazyEither (Either e String)
const cat = file => LazyEither(resolve =>
  fs.readFile(file, {encoding: 'utf8'}, (err, data) => resolve(err ? S.Left(err) : S.Right(data))))

//:: String -> LazyEither (Either e String)
const catDir =
S.pipe([ls,
        S.chain(S.traverse(LazyEither, cat)),
        S.map(S.unlines)])

// A LazyEither instance is executed when value gets called:
catDir(os.homedir()).value(S.either(console.error, console.log))
github Risto-Stevcev / lazy-either / examples / readme2.js View on Github external
, S  = require('sanctuary')

//:: String -> String -> String
const join = S.curry2(path.join)

//:: String -> LazyEither (Either e [String])
const ls = path => LazyEither(resolve =>
  fs.readdir(path, (err, files) => resolve(err ? S.Left(err) : S.Right(S.map(join(path), files)))))

//:: String -> LazyEither (Either e String)
const cat = file => LazyEither(resolve =>
  fs.readFile(file, {encoding: 'utf8'}, (err, data) => resolve(err ? S.Left(err) : S.Right(data))))

//:: String -> LazyEither (Either e String)
const catDir =
S.pipe([ls,
        S.chain(S.traverse(LazyEither, cat)),
        S.map(S.unlines)])

// A LazyEither instance is executed when value gets called:
catDir(os.homedir()).value(S.either(console.error, console.log))
github Risto-Stevcev / lazy-either / examples / readme2.js View on Github external
//:: String -> String -> String
const join = S.curry2(path.join)

//:: String -> LazyEither (Either e [String])
const ls = path => LazyEither(resolve =>
  fs.readdir(path, (err, files) => resolve(err ? S.Left(err) : S.Right(S.map(join(path), files)))))

//:: String -> LazyEither (Either e String)
const cat = file => LazyEither(resolve =>
  fs.readFile(file, {encoding: 'utf8'}, (err, data) => resolve(err ? S.Left(err) : S.Right(data))))

//:: String -> LazyEither (Either e String)
const catDir =
S.pipe([ls,
        S.chain(S.traverse(LazyEither, cat)),
        S.map(S.unlines)])

// A LazyEither instance is executed when value gets called:
catDir(os.homedir()).value(S.either(console.error, console.log))
github Risto-Stevcev / lazy-either / examples / readme2.js View on Github external
//:: String -> LazyEither (Either e [String])
const ls = path => LazyEither(resolve =>
  fs.readdir(path, (err, files) => resolve(err ? S.Left(err) : S.Right(S.map(join(path), files)))))

//:: String -> LazyEither (Either e String)
const cat = file => LazyEither(resolve =>
  fs.readFile(file, {encoding: 'utf8'}, (err, data) => resolve(err ? S.Left(err) : S.Right(data))))

//:: String -> LazyEither (Either e String)
const catDir =
S.pipe([ls,
        S.chain(S.traverse(LazyEither, cat)),
        S.map(S.unlines)])

// A LazyEither instance is executed when value gets called:
catDir(os.homedir()).value(S.either(console.error, console.log))
github xodio / hm-def / src / signature.js View on Github external
[typeEq ('typeConstructor'), convertTypeConstructor],
      [typeEq ('function'), convertFunction],
      [typeEq ('list'), convertList],
      [typeEq ('record'), convertRecord],
      [typeEq ('constrainedType'), convertConstrainedType],
      [typeEq ('typevar'), S.pipe ([convertTypevar, Reader.of])],
      [typeEq ('thunk'), S.K (Reader.of (Thunk))],
      [S.K (true), e => {
        throw new Error
          (`Don't know what to do with signature entry ${e.type}`);
      }],
    ]) (entry));

  //    convertTypes :: Array SignatureEntry -> Reader (TypeMap (Array Type))
  const convertTypes = memoize (S.pipe ([
      S.map (convertType),
      S.unchecked.sequence (Reader),
      S.unchecked.map (S.reject (S.equals (Thunk))),
    ]));

  //    convertTypeConstructor :: SignatureEntry -> Reader (TypeMap Type)
  const convertTypeConstructor = memoize (S.ifElse
    (hasChildren)
    (y => S.pipe ([
      children,
      convertTypes,
      x => S.unchecked.lift2 (constructType) (x) (lookupType (y)),
    ]) (y))
    (lookupType));

  //    convertList :: SignatureEntry -> Reader (TypeMap Type)
  const convertList = memoize (S.pipe ([
github xodio / hm-def / src / signature.js View on Github external
(types[types.length - 1])
      (types.slice (0, -1))),
  ]));

  //    convertRecordField :: SignatureEntry
  //                          -> Reader (TypeMap (Pair String Type))
  const convertRecordField = memoize (entry => S.pipe ([
    firstChild,
    convertType,
    S.unchecked.map (valueType => [entry.text, valueType]),
  ]) (entry));

  //    convertRecord :: SignatureEntry -> Reader (TypeMap Type)
  const convertRecord = memoize (S.pipe ([
    children,
    S.map (convertRecordField),
    S.unchecked.sequence (Reader),
    S.unchecked.map (fromPairs),
    S.unchecked.map ($.RecordType),
  ]));

  //    convertTypevar :: SignatureEntry -> Type
  const convertTypevar = memoize (x => $.TypeVariable (text (x)));

  //    unaryTypevar :: SignatureEntry -> (Type -> Type)
  const unaryTypevar = memoize (x => $.UnaryTypeVariable (text (x)));

  //    convertConstrainedType :: SignatureEntry -> Reader (TypeMap Type)
  const convertConstrainedType = memoize (entry => S.pipe ([
    firstChild,
    convertType,
    S.unchecked.map (unaryTypevar (entry)),
github xodio / hm-def / src / signature.js View on Github external
const convertType = memoize (entry => cond ([
      [typeEq ('typeConstructor'), convertTypeConstructor],
      [typeEq ('function'), convertFunction],
      [typeEq ('list'), convertList],
      [typeEq ('record'), convertRecord],
      [typeEq ('constrainedType'), convertConstrainedType],
      [typeEq ('typevar'), S.pipe ([convertTypevar, Reader.of])],
      [typeEq ('thunk'), S.K (Reader.of (Thunk))],
      [S.K (true), e => {
        throw new Error
          (`Don't know what to do with signature entry ${e.type}`);
      }],
    ]) (entry));

  //    convertTypes :: Array SignatureEntry -> Reader (TypeMap (Array Type))
  const convertTypes = memoize (S.pipe ([
      S.map (convertType),
      S.unchecked.sequence (Reader),
      S.unchecked.map (S.reject (S.equals (Thunk))),
    ]));

  //    convertTypeConstructor :: SignatureEntry -> Reader (TypeMap Type)
  const convertTypeConstructor = memoize (S.ifElse
    (hasChildren)
    (y => S.pipe ([
      children,
      convertTypes,
      x => S.unchecked.lift2 (constructType) (x) (lookupType (y)),
    ]) (y))
    (lookupType));

  //    convertList :: SignatureEntry -> Reader (TypeMap Type)
github xodio / hm-def / src / signature.js View on Github external
const cond = conds => x => {
  const c = conds.find (y => y[0] (x));
  if (c !== undefined) {
    return c[1] (x);
  }
  throw new Error (`No predicate was satisfied for ${x}`);
};

//    stripNamespace :: String -> String
const stripNamespace = memoize (xs => xs.split ('/').pop ());

//    name :: { name :: a } -> a
const name = S.prop ('name');

//    text :: { text :: a } -> a
const text = S.prop ('text');

//    spellNumber :: Number -> String
const spellNumber = x => ({
  1: 'one',
  2: 'two',
  3: 'three',
  4: 'four',
  5: 'five',
  6: 'six',
  7: 'seven',
  8: 'eight',
  9: 'nine',
}[x] || x.toString ());

// ----------------------------------------------------------------------------
//
github xodio / hm-def / src / signature.js View on Github external
(acc => curr => S.insert (curr[0]) (curr[1]) (acc))
  ({});
//    cond :: Array (Array2 (a -> Boolean) (a -> b)) -> a -> b
const cond = conds => x => {
  const c = conds.find (y => y[0] (x));
  if (c !== undefined) {
    return c[1] (x);
  }
  throw new Error (`No predicate was satisfied for ${x}`);
};

//    stripNamespace :: String -> String
const stripNamespace = memoize (xs => xs.split ('/').pop ());

//    name :: { name :: a } -> a
const name = S.prop ('name');

//    text :: { text :: a } -> a
const text = S.prop ('text');

//    spellNumber :: Number -> String
const spellNumber = x => ({
  1: 'one',
  2: 'two',
  3: 'three',
  4: 'four',
  5: 'five',
  6: 'six',
  7: 'seven',
  8: 'eight',
  9: 'nine',
}[x] || x.toString ());
github xodio / hm-def / src / signature.js View on Github external
};

//    indexTypeClasses :: Array TypeClass -> TypeClassMap
const indexTypeClasses = memoize (indexBy (S.pipe ([
  name,
  stripNamespace,
])));

// ----------------------------------------------------------------------------
//
// Types
//
// ----------------------------------------------------------------------------

//    children :: { children :: Array a } -> Array a
const children = S.prop ('children');

//    firstChild :: { children :: NonEmpty (Array a) } -> a
const firstChild = x => children (x)[0];

//    typeEq :: String -> Object -> Boolean
const typeEq = propEq ('type');

//    hasChildren :: { children :: Array a } -> Boolean
const hasChildren = x => !isEmpty (children (x));

//    assertTypeArity :: Type -> Array Type -> Undefined !
const assertTypeArity = type => argTypes => {
  const expected = type.keys.length;
  const actual = argTypes.length;
  if (expected !== actual) {
    throw new TypeError (