Lenses optimization; Cannot inline functions containing closures..

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Lenses optimization; Cannot inline functions containing closures..

sledorze

I'm currently toying with a lense implementation, willing to think we could generate lenses definition automatically from type description.

It would allow us to easily build modifier abstractions with more efficiency than handwritten code.

For those who want to know more about Lenses; there's a couple of excellent video here (Scala):
http://www.youtube.com/watch?v=efv0SQNde5Q

hence the reason I want to stress this limitation again (including JS)  because I think a lot can be fused away :)

a (very*) basic example:

    var userA : User = {
      age : 6,
      name : "georges"
    };

    var userGroup :  UserGroup = {
      users : [userA, userA],
      lead : userA,  
    };
   
    var ageOfUserGroupLead = UserGroup_.lead.andThen(User_.age);
   
    var updatedDataStructures = ageOfUserGroupLead.set(5, userGroup);
    var leadAge = ageOfUserGroupLead.get(userGroup);

Any Feedback?

St├ęphane

(* means it does not prove the whole utility which is made apparent with deeply nested data structures)
Reply | Threaded
Open this post in threaded view
|

Re: Lenses optimization; Cannot inline functions containing closures..

sledorze
Reply | Threaded
Open this post in threaded view
|

Re: Lenses optimization; Cannot inline functions containing closures..

sledorze
Forget it, that's not possible currently as functions goes through data structures anyway (I think there's a mention of an optimization around that but I think I better come back to it later..)
Reply | Threaded
Open this post in threaded view
|

Re: Lenses optimization; Cannot inline functions containing closures..

sledorze
The only possible path is to merge both functions and rely on dynamic parameters, hoping that null comparison could be fused away.
Typing could occur using another typed inlined layer.
However, this is pure speculation as the code below cannot be inlined so far.. (but it would be awesome for targets where dynamic acces is the norm!)


typedef Lense = Dynamic -> Dynamic -> Dynamic

class LenseExtensions {

  inline public static function andThen (l1 : Lense, l2 : Lense) : Lense return
    function (first, second) {
      if (second == null) { // get case
        l2(l1(first, null), null);
      } else {
        l1(first, l2(l1(first, null), second));
      }
    }
   
}