Arrow functions as methods

Irakli Gozalishvili rfobic at gmail.com
Wed Jul 11 10:23:50 PDT 2012


It is true that on could wrap arrow function into vintage one in order to reuse it as a method, but that is a poor mans workaround. As of motivation, all I really want is to have a synergy of different paradigms that are in the language.

What I really mean by that is, if one writes in a functional style: 

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

That should be reusable in OO code without any changes or wrappers:

List.prototype.map = map
List().map((x) => x + 1)

Also this complements another idea that I have posted a while ago:
https://mail.mozilla.org/pipermail/es-discuss/2012-June/023657.html

Where I proposed to make private names functions:

var method = Name()
method(object, arg) // is just a sugar for
object[method](arg)

This will enable people to write code in OO or functional style and make it usable in other style:

var map = Name();
List.prototype[map] = (list, f) =>
  reduce(list, (result, value) => result.concat([ f(value) ]), [])

Which one will be able to us in a functional style

map(list, f)

or in OO

list[map](f)

Also even if suggested changes to private names won't take of, it's still can be implemented as a library:
https://github.com/Gozala/method

There for a way to author functions that also can be used as methods is relevant. Thin arrows would probably also solve this problem.
Regards
--
Irakli Gozalishvili
Web: http://www.jeditoolkit.com/


On Wednesday, 2012-07-11 at 09:39 , Claus Reinke wrote:

> > 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
> 
> 
> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120711/bd37d8ac/attachment.html>


More information about the es-discuss mailing list