npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hm-def-light

v0.1.4

Published

Runtime Hindley-Milner ad hoc type checking with Sanctuary

Downloads

4

Readme

Javascript runtime Hindley-Milner ad hoc type checking with Sanctuary.

Overview

It's basically a syntax sugar over sanctuary-def, inspired on hm-def.

"It facilitates the definition of curried JavaScript functions which are explicit about the number of arguments to which they may be applied and the types of those arguments." - sanctuary-def

Features

  • All Sanctuary's type constructors and type classes available out of the box.
  • Supports custom type constructors and type classes.
  • Only three dependencies: Sanctuary, Parsimmon and hm-lang-light.
  • Comprehensively tested - works with any Sanctuary type declaration.
  • Input is fairly validated.
  • Written in a -quite- functional Javascript idiom.

Getting Started

  1. Install hm-def-light with npm - or by any preferred means.
    • For testing, it's sufficient to install the dev dependencies and run the test script.
    • It's dependent on imports. So, adopt it - with e.g: esm, babel.
  2. Then you're ready to go! Please, follow the examples for further apprehension.

API

hm-def-light is reliant on:

create           : S create
env              : S env
$                : sanctuary-def
Z                : sanctuary-type-classes
checkTypes       : Boolean
typeClasses      : optional [TypeClass]
typeConstructors : optional StrMap Type'

Params 1-5 are not checked for correctness: they're supposed to be correct. Type' is a -fancy, uncanon- alias for $ Nullary, Unary and Binary types.

One by one brief:

create           : for internal S retrieval
env              : basis for `Type'`s (e.g: `Number`, `HtmlElement`, `Error` etc)
$                : for `Type`s construction
Z                : basis for default `TypeClasses` (e.g: `Functor`, `Alt`, `Traversable` etc)
checkTypes       : "The checkTypes option determines whether type checking is enabled. 
                    This allows one to only pay the performance cost of run-time type checking  
                    during development."
typeClasses      : optional custom `TypeClass`es
typeConstructors : optional custom `Type'`s; * must also be defined in the environment (i.e env)

Hands on:

First define a def function for the setting:

import {create, env}  from "sanctuary"
import SDef           from "sanctuary-def"
import Z              from "sanctuary-type-classes"

import $create        from "hm-def-light"

// checkTypes :: Boolean
const checkTypes = true

// minimal
const def = $create ({create, env, $ : SDef, Z, checkTypes})

Now you can use def in function definitions:

const add =
def ("add :: Number -> Number -> Number")
    (x => y => x+y)

add (1) (2) // ok
add (3) (4) // ok

add ("a") ("b") // error - in case of checkTypes = true
add ("c") ("d") // error ...

As a design choice, def is not memoized.
So you should be attentious to repeated def calls:

while (true) {
  // this repeats the entire, sanctuary-def `def`, 'mirroring' for every iteration
  def ("K :: a -> b -> a")
      (x => y => x)
}

In this case, the function definition could be taken out of the loop.
Alternatively, it's very easy to memoize def yourself:

// e.g
const memoDef = def => memo =>
  memo (typeDeclaration => def (typeDeclaration))

minimal def also works for more complex type signatures:

const ex1 =
def ("ex1 :: (Foldable a, Alternative a) => a -> a")
    (x => x)

ex1 (1)   // error
ex1 ([])  // ok

const ex2 =
def ("ex2 :: Maybe a -> Either String a")
    (x => maybeToEither ("dummy") (x))

ex2 (Just (1))    // Right (1)
ex2 (Nothing)     // Left  ("dummy")
ex2 ("arbitrary") // error

const ex3 =
def ("ex3 :: NonEmpty [Number] -> Number")
    (arr => arr[0])

ex3 ([1,2]) // ok
ex3 ([])    // error

// ...

And if you need custom Type's or TypeClasses, there's no secret, e.g:

const customEnv = env.concat ([MyCustomType (SDef.Date)]) // `Type` in env is mandatory
                                                          // But it can be refined as 
                                                          // required: no need to pass
                                                          // `Unknown`s if not desired,
                                                          // thus retaining `env` 
                                                          // characteristics

const S = create ({
  checkTypes, 
  env : customEnv
})

const def = $create ({
  create, 
  env              : customEnv, 
  $                : SDef, 
  Z, 
  checkTypes,
  typeClasses      : [MyCustomTypeClass],
  typeConstructors : {MyCustomType} // `Unary` and `Binary` type constructors are sensible here
})

// ...

Running tests:

For more information, I recommend a stride around the referenced resources.

License

This project is licensed under the MIT License - see the LICENSE file for details