0.0.13 • Published 5 years ago

@ts-common/fun.ts v0.0.13

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
5 years ago

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

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago