lens icon indicating copy to clipboard operation
lens copied to clipboard

Struct pick lens

Open jackfirth opened this issue 10 years ago • 2 comments

It may be useful to have a function that constructs a lens viewing a struct whose fields are lens views of some other structure, similar to the pick lenses for other data types.

> (struct foo (a b c) #:transparent)
> (define foo-hash-lens (struct-pick-lens foo (hash-ref-lens 'a) (hash-ref-lens 'b) (hash-ref-lens 'c))
> (lens-view foo-hash-lens (hash 'a 1 'b 2 'c 3))
(foo 1 2 3)
> (lens-set foo-hash-lens (hash 'a 1 'b 2 'c 3) (foo 10 20 30))
(hash 'a 10 'b 20 'c 30)

jackfirth avatar Aug 21 '15 19:08 jackfirth

Oh yes definitely I'll make a pull request. (but wouldn't this be lens-join/struct?)

AlexKnauth avatar Aug 21 '15 19:08 AlexKnauth

Ah you're right, this would be lens-join/struct. struct-pick-lens would be something that creates a smaller struct from a larger struct:

> (struct foo (a b c d) #:transparent
> (struct bar (n m) #:transparent)
> (define foo-bar-lens (struct-pick-lens bar foo a d))
> (lens-view foo-bar-lens (foo 1 2 3 4))
(bar 1 4)
> (lens-set foo-bar-lens (foo 1 2 3 4) (bar 10 100))
(foo 10 2 3 100)

Not sure about the argument order, but that's the general idea.

jackfirth avatar Aug 21 '15 20:08 jackfirth