jackfirth / lens

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

Prefab struct -> string isomorphism lens #172

Open jackfirth opened 8 years ago

jackfirth commented 8 years ago
> (struct posn (x y) #:prefab)
> (define posn->string-lens (prefab-struct-isomorphism-lens posn))
> (lens-view posn->string-lens (posn 1 2))
"#s(posn 1 2)"
> (lens-set posn->string-lens (posn 1 2) "#s(posn 0 0)")
#s(posn 0 0)

Not sure on naming, but this is the general idea. A more general read/write isomorphism would also be worth looking into.

AlexKnauth commented 8 years ago

But this would have to recur into the fields, so I think it would require a more general read/write lens anyway, right? Or would prefab-struct-isomorphism-lens take arguments for handling that, for instance like:

(prefab-struct-isomorphism-lens posn number->string-lens number->string-lens)
jackfirth commented 8 years ago

You're right. I think maybe this should accept lenses for the fields, and a more general read-write isomorphism would be a separate thing altogether. That seems the most natural way to go about it.