jackfirth / lens

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

There should be a way to create a composed list lens from several lenses #15

Closed jackfirth-mj closed 9 years ago

jackfirth-mj commented 9 years ago

Given a list of lenses, there should be a way to combine them into a single lens whose view is a list of the views of the given lenses.

AlexKnauth commented 9 years ago

Is something like this what you mean?

#lang racket

(require lenses lenses/hash rackunit)

(define (wierd-list-lens . lenses)
  (define n (length lenses))
  (lambda (target)
    (values (for/list ([lens (in-list lenses)])
              (lens-view lens target))
            (lambda (new-view-list)
              (unless (= (length new-view-list) n)
                (error 'bad))
              (for/fold ([target target])
                        ([lens (in-list lenses)]
                         [v (in-list new-view-list)])
                (lens-set lens target v))))))

(define tgt (hash 'a 1 'b 2 'c 3 'd 4 'e 5 'f 6))

(define lns (wierd-list-lens (hash-ref-lens 'a)
                             (hash-ref-lens 'b)
                             (hash-ref-lens 'c)
                             (hash-ref-lens 'e)))

(check-equal? (lens-view lns tgt) '(1 2 3 5))
(check-equal? (lens-set lns tgt '(100 200 300 500)) (hash 'a 100 'b 200 'c 300 'd 4 'e 500 'f 6))
jackfirth commented 9 years ago

Yes, this is a back burner item however, there are more pressing matters I'd like to attend to