Last active
August 22, 2018 10:06
-
-
Save irisjae/df5ad2a2b9a7223e559f7847ce9daf25 to your computer and use it in GitHub Desktop.
Polyfilling algebraic data types for js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//first attempt | |
//requirements: | |
//- must be polyfillable | |
// aka can be used as a library (though always recommended to be compiled) | |
//- syntax must be friendly | |
var board_viewer = data ({ | |
board_viewer: (board = board, questions = list (question), history = history) => board_viewer }) | |
var list = a => | |
data | |
({ nil: () => list (a) | |
, cons: (head = a, tail = list (a)) => list (a) }) | |
//various experiments | |
var board_viewer = data ({ | |
board_viewer: (board = ~ board, questions = ~ list (question), history = ~ history) => board_viewer }) | |
var board_viewer = data ({ | |
board_viewer: ({_} =- board) => (questions =- list (question)) => ({_} =- history) => ({_} =- board_viewer) }) | |
var board_viewer = data ({ | |
board_viewer: (board =- board) => (questions =- list (question)) => (history =- history) => board_viewer }) | |
var board_viewer = data ({ | |
board_viewer: ({_} =- board) => (questions =- list (question)) => ({_} =- history) => ({_} =- board_viewer) }) | |
var board_viewer = data ({ | |
board_viewer: { board :- board, questions :- list (question), history :- history } [board_viewer] }) | |
var board_viewer = data ({ | |
board_viewer: { board :- board, questions :- list (question), history :- history } |- board_viewer }) | |
var board_viewer = data ({ | |
board_viewer: { board :- board, questions :- list (question), history :- history } ** { board_viewer } }) | |
var board_viewer = data ({ | |
board_viewer: board -+ list (question) [typeof questions] -+ history [typeof history] -+ board_viewer }) | |
var board_viewer = data ({ | |
board_viewer: board .board -+ list (question) .questions -+ history .history -+ board_viewer }) | |
var board_viewer = data ({ | |
board_viewer: board, board => list (question), questions => history, history => board_viewer }) | |
var board_viewer = data ([ | |
board, board => list (question), questions => history, history => board_viewer, board_viewer ]) | |
var board_viewer = data ({ | |
board_viewer: board|( board => list (question)|( questions => history|( history => board_viewer ))) }) | |
//current draft | |
//- polyfillable; this one relies on `valueOf` coercion from types, so valueOf is hijacked for all registered types | |
//- polyfillable; order of records can be determined; defacto, records preserve order, plus, function can be inspected toString | |
//- syntax borrows from both adts and gadts | |
//- allows grouping of similar gadt return types | |
//- allows collisions of record-name and type variable name (while still being polyfillable) | |
//- allows omitting of type and omitting of tag name (while still being polyfillable) | |
//- allows omitting of singular constructor | |
//- automatically disallows repeated constructor names | |
var board_viewer = data | |
(( board_viewer = { board :- board, questions :- list (question), history :- history } ) => board_viewer ) | |
var board_viewer = data | |
(( _ = { _ :- board, questions :- list (question), _ :- history } ) => board_viewer ) | |
var board_viewer = data | |
(( _ = { board, questions, history } ) => board_viewer ) | |
var list = a => | |
data | |
(( nil | |
, cons = { head :- a, tail :- list (a) } ) => list (a) ) | |
var list = a => | |
data | |
(( nil | |
, cons = { _ :- a, _ :- list (a) } ) => list (a) ) | |
var expr = a => | |
data | |
(( bool = { _ :- bool } ) => expr (bool), | |
( int = { _ :- int } ) => expr (int) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment