@ts-common/fun.ts
TypeScript icon, indicating that this package has built-in type declarations

0.0.13 • Public • Published

fun.ts

Build Status npm version

Purely functional subset of JavaScripts/TypeScript.

There are a lot of pure functional languages that can be compiled to JavaScript. Usually, the biggest problem with these libraries is interoperability. For example, if you have a big project written on JavaScript, it's very challenging to rewrite parts of this project step-by-step using another language. https://github.com/jashkenas/coffeescript/wiki/List-of-languages-that-compile-to-JS#static-typing.

The project is not another functional language that can be compiled into JavaScript. The project tries to define a subset of JavaScript that can be formally verified.

The subset can be used as a safe script or as a target platform for other programming languages.

Roadmap

Potential Targets and Applications

  • Markdown safe script that can be run in a browser (for example http://madoko.org/reference.html),
  • query languages,
  • distributed systems, like ALIQ, machine learning, AI,
  • as a target platform for other functional languages.
  • the subset can be recognized by browser and compiled into more optimal code (similar to asm.js).

Wish List

  • All data is immutable.

  • Pure functions, without side-effects.

  • Strong structural typing.

  • Type inference.

  • Compatibility with JavaScript and TypeScript.

    • write/read .d.ts files.
    • the subset should be valid JavaScript or TypeScript. So no need for additional transpilers, we only need a validator.
  • The language validator should be written on JavaScript/TypeScript so it can run in a browser.

  • no implicit type conversions. For example ?: should only accept bool type.

  • Type system should allow to describe monads. Example on pseudo-TypeScript

    type MonadStrategy = {
        type Monad<T>; // this line can't be compiled in TypeScript.
        readonly just: <T>(v: T) => Monad<T>
        readonly join: <T>(m: Monad<Monad<T>>) => Monad<T>
    }
    
    // Or
    type MonadStrategy = {
        type Monad { type T }; // this line can't be compiled in TypeScript.
        readonly just: <T>(v: T) => Monad { T }
        readonly join: <T>(m: Monad { T: Monad { T } }) => Monad { T }
    }
  • Type system should be able to reflect JSON-Schema.

Typing

Typing requires a languages extension. Several safe options are

  • embed typing in comments.
  • embed typing in a separate file.
  • typing is based on special run-time definitions, similar to Json-Schema. For example const MyType = { type: 'string', ... }.

Possible typing languages are

  • TypeScript,
  • JS docs,
  • JSON-Schema,
  • Some kind of Haskell type notations?

Proposed Typing

  • JavaScript. Starts with //: or /*:

    const myFunc
        //: (_: number) => string
        = v => v.toString()
    const myFunc /*: (_: number) => string */ = v => v.toString()

    Simplified types (incompatable with TypeScript).

    const myFunc
        //: number => string
        = v = v.toString()
    //type MyType = ...
    /*type MyType = {
    
    }*/
  • TypeScript

    const myFunc
        : (_: number) => string
        = v => v.toString()

Notes

Use hasOwnProperty() to check if we can read such properties as constructor. Incorrect code:

const m = x.constructor
// or
const { constructor } = x

Correct code:

const m = Object.prototype.hasOwnProperty.call(x, 'constructor') ? x.constructor : undefined

References

Languages

ECMAScript Proposals

Package Sidebar

Install

npm i @ts-common/fun.ts

Weekly Downloads

137

Version

0.0.13

License

Apache-2.0

Unpacked Size

106 kB

Total Files

64

Last publish

Collaborators

  • lirenhe
  • billytrend
  • vladbarosan
  • sergeyshandar