jackfirth / lens

A Racket package for creating and composing pure functional lenses
Other
74 stars 8 forks source link

lens-fragment #206

Open jackfirth opened 9 years ago

jackfirth commented 9 years ago

This is something I implemented in my ease package:

> (lens-fragment first-lens '(1 foo bar) '(10 20 30))
'((10 foo bar) (20 foo bar) (30 foo bar))

Basically you can take a lens, a target, and a list of new views to "explode" the target into a list of possible targets, one for each view. This worked really nicely with easings, where given a start point and a stop point and a number of steps it was easy to generate the list of steps. So for instance, given a player struct with a posn field, you could easily get a list of players moving along in the x direction:

> (lens-fragment player-posn-x-lens (player (posn 0 0)) '(1 2 3))
'((player (posn 1 0)) (player (posn 2 0)) (player (posn 3 0)))
AlexKnauth commented 9 years ago

Is that equivalent to (map (lens-set lns tgt _) lst)?

jackfirth commented 9 years ago

Yup, exactly. The implementation is very simple but I've found it surprisingly helpful.

AlexKnauth commented 9 years ago

I'm not sure how that relates to "fragment" though ... What came to my mind first, which is why I was confused a bit, was a function like this:

(define (lens-fragment-like-I-imagined lens . other-lenses)
  (lens-thrush lens (apply lens-join/list other-lenses)))

Or something similar to that idea. Anyway, is fragment the best name?

jackfirth commented 9 years ago

Fragment was the first name I came up with while working in the middle of the night on a completely unrelated package, so it's very likely it's not a good name.