Arrow functions as methods

Claus Reinke claus.reinke at talk21.com
Wed Jul 11 09:39:43 PDT 2012


> function map(f) {
>  return this.reduce((result, value) =>  result.concat([ f(value) ]), [])
> }
> ..
> var map = (list, f) =>
>   list.reduce((result, value) => result.concat([ f(value) ]), [])

Not sure I got your motivation, but would this help?

    function fn(f) { return f(this) } // provide 'this' as explicit arg

    let map = it => f =>
      it.list.reduce((result, value) => result.concat([ f(value) ]), [])

    let obj = { map: fn(map), list: [1,2,3] }; // wrap map as method

    obj.map(x => x+1);
    map( {list:[1,2,3]} )(x => x+1);

Claus
 


More information about the es-discuss mailing list