fantasyland / daggy

Library for creating tagged constructors.
MIT License
700 stars 29 forks source link

New implementation #9

Closed safareli closed 7 years ago

safareli commented 7 years ago

fix #7

This implementation has some breaking changes:

// creation of tagged constructor
- Point = tagged('x', 'y')
+ Point = tagged('Point', ['x', 'y'])
// creation of sum type
- List = taggedSum({ Cons: ['x', 'xs], Nil: [])
+ List = taggedSum('List', { Cons: ['x', 'xs], Nil: [])

list = List.Cons(1, List.Nil)
// checking if value is of some type or tag
- list instanceof List.Cons //true
- list instanceof List //true
+ List.Cons.is(list) //true
+ List.is(list) //true
// to string
- list.toString() // Cons(1, Nil())
+ list.toString() // List.Cons(1, List.Nil())

Also you can't call List (which before throwed) as it's not just an object representing the type List.

We also have some performance improvements, main one is that cata is about two times faster:

screen shot 2017-01-30 at 1 37 23 am

New implementation is slower in some cases:

Also we can make construction of objects much faster by not using Object.defineProperty but then @@tag and @@values will be visible in node REPL, and I think in for in as well.

Would love your comments/suggestions.

TODO:


Benchmarks are run on 2.5 GHz Intel Core i7 mac using node v7.0.0


README is updated by hand as emu is failing on node 7 maybe because of es6, but i just removed it:

$ ./node_modules/.bin/emu  < ./src/daggy.js > README.md
/Users/safareli/dev/daggy/node_modules/esprima/esprima.js:3872
            throw e;
            ^

Error: Line 1: Unexpected token {
    at throwError (/Users/safareli/dev/daggy/node_modules/esprima/esprima.js:1156:21)
    at throwUnexpected (/Users/safareli/dev/daggy/node_modules/esprima/esprima.js:1213:9)
    at parseVariableIdentifier (/Users/safareli/dev/daggy/node_modules/esprima/esprima.js:2081:13)
    at parseVariableDeclaration (/Users/safareli/dev/daggy/node_modules/esprima/esprima.js:2091:18)
    at parseVariableDeclarationList (/Users/safareli/dev/daggy/node_modules/esprima/esprima.js:2118:23)
    at parseConstLetDeclaration (/Users/safareli/dev/daggy/node_modules/esprima/esprima.js:2153:24)
    at parseSourceElement (/Users/safareli/dev/daggy/node_modules/esprima/esprima.js:3053:24)
    at parseSourceElements (/Users/safareli/dev/daggy/node_modules/esprima/esprima.js:3095:29)
    at parseProgram (/Users/safareli/dev/daggy/node_modules/esprima/esprima.js:3109:19)
    at Object.parse (/Users/safareli/dev/daggy/node_modules/esprima/esprima.js:3856:23)
davidchambers commented 7 years ago

I'd like to see List.Nil rather than List.Nil(). I made this change for union-type in paldepind/union-type#55.

safareli commented 7 years ago

@davidchambers I had the () in toString only, in code u do like this List.Cons('1', List.Nil). anyways using () in toString is not "correct" , will fix that shortly

SimonRichardson commented 7 years ago

I'm personally happy with the performance of it currently, so don't go mental going optimisation mad imo. Also we should be careful what is an optimisation now, might not be one in the near future and so push back on micro-optimisations where it harms readability (note: i'm not saying any suggests fall into this category, just we have to be aware).

safareli commented 7 years ago

Current state looks like this: screen shot 2017-01-30 at 6 48 17 pm

@Avaq thanks for review.


It would be nice if you folks, play around with this version.

safareli commented 7 years ago

btw is the is name ok?

SimonRichardson commented 7 years ago

I think is fits perfectly!

safareli commented 7 years ago

I have updated readme but i'm not sure if textual description of thouse methods are descriptive/correct enough. would <3 suggestions there. here is rendered version of readme: https://github.com/safareli/daggy/tree/is-new

SimonRichardson commented 7 years ago

Love it, I think it's good to go, we can always update it if someone spots anything. Let me know if you're done and I'll merge (tomorrow and move to FL).

On Mon, 30 Jan 2017, 19:39 Irakli Safareli, notifications@github.com wrote:

I have updated readme but i'm not sure if textual description of thouse methods are descriptive/correct enough. would <3 suggestions there. here is rendered version of readme: https://github.com/safareli/daggy/tree/is-new

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/puffnfresh/daggy/pull/9#issuecomment-276167363, or mute the thread https://github.com/notifications/unsubscribe-auth/ACcaGByhSsxpB5FVqt5dADfprLukwmBcks5rXjyGgaJpZM4Lw67U .

safareli commented 7 years ago

before toString was too fast in old version of daggy as it didn't defined one, now new version is pretty close.

@SimonRichardson ready for merge 🌮

SimonRichardson commented 7 years ago

@puffnfresh can you move this repo over to FL for me, I don't have access to the setting page :pray:

safareli commented 7 years ago

@puffnfresh ping