Skip to content

Instantly share code, notes, and snippets.

@xaviervia
Last active February 4, 2017 00:44
Show Gist options
  • Save xaviervia/74ed142bf08afdba22e690270798baaa to your computer and use it in GitHub Desktop.
Save xaviervia/74ed142bf08afdba22e690270798baaa to your computer and use it in GitHub Desktop.
const createType = name => {
const of = x => Object.freeze({
'@@type': name,
fold: f => f(x),
map: f => of(f(x)),
inspect: () => `${name}(${x})`
})
return {of}
}
const Left = createType('Left')
const x = {a: 1}
Left.of(x).map(({a}) => a + 1)
@leostera
Copy link

leostera commented Feb 3, 2017

We're missing this:

inspect: () => `${name}(${x})`

@xaviervia
Copy link
Author

Crucial. Thanks

@xaviervia
Copy link
Author

How 'bout:

const createType = name => {
  const of = x => ({
    [Symbol.for('@@type')]: Symbol.for(name),
    [Symbol.for('@@value')]: x,
    fold: f => f(x),
    map: f => of(f(x)),
    inspect: () => `${name}(${x})`
  })
  
  return {of}
}

@leostera
Copy link

leostera commented Feb 3, 2017

Works for me πŸ‘, but maybe we want to take it a step forward and really define the property in the object as a non-enumerable, non-configurable, non-writable property? Completely frozen and unchangeable.

@leostera
Copy link

leostera commented Feb 3, 2017

@xaviervia
Copy link
Author

Sure.

@xaviervia
Copy link
Author

Well apparently that's already the case just because they are Symbols πŸ‘

screen shot 2017-02-04 at 00 19 31

https://www.keithcirkel.co.uk/metaprogramming-in-es6-symbols/

@xaviervia
Copy link
Author

Will add freeze

@leostera
Copy link

leostera commented Feb 3, 2017

πŸ‘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment