Giter VIP home page Giter VIP logo

Comments (6)

AlexKnauth avatar AlexKnauth commented on September 22, 2024 2

It would be tricky to make a lens like this follow the lens laws. What should happen in an example like this?

(lens-set list*->list-lens '(a b . c) '(1 2 (3 . 4))) ;; => '(1 2 3 . 4)
(lens-view list*->list-lens '(1 2 3 . 4)) ;; => ???

The answer would be '(1 2 3 4), except that that violates one of the lens laws.

from lens.

SuzanneSoy avatar SuzanneSoy commented on September 22, 2024

That's a very good point, I hadn't thought about this case. An obvious (poor) solution would be to make it a lens constructor, which takes the number of “main” elements as an argument:

(lens-set (list*->list-lens 2) '(a b . c) '(1 2 (3 . 4))) ;; => '(1 2 3 . 4)
(lens-view (list*->list-lens 2) '(1 2 3 . 4)) ;; => '(1 2 (3 . 4))

(lens-set (list*->list-lens 2) '(a b c . d) '(1 2 3)) ;; => '(1 2 . 3)
(lens-view (list*->list-lens 2) '(a b c . d)) ;; => '(a b (c . d))

This, however, is not very practical if the goal is to be able to use functions like map, length etc. on improper lists.

Another poor solution would be to contract the view with (not/c pair?), but that's annoyingly restrictive.

For my own use, I think an improper lens would be good enough, but I agree we need something better for the library.

Any suggestion? Otherwise I'll close this for now.

from lens.

jackfirth avatar jackfirth commented on September 22, 2024

Restricting the view to a proper list seems sensible to me, as the very name itself implies the target is (possibly) improper while the view is proper. In what scenarios would that restriction hinder the ability to use the lens?

from lens.

SuzanneSoy avatar SuzanneSoy commented on September 22, 2024

@jackfirth The problem arises when the view is any pair, including if it's a proper list. Consider the following:

(lens-set list*->list-lens '(a b . c) '(1 2 (3 4 5))) ;; => '(1 2 3 4 5)
(lens-view list*->list-lens '(1 2 3 4 5)) ;; => '(1 2 3 4 5 ())

I made a mistake, the contract should instead be (*list/c any/c (not/c pair?)), i.e. the last element of the view should be a non-pair value (so null?, vector?, hash?, struct? etc. but not pair?), to avoid ambiguities.

In the end, the list*->list-lens is the dual of an list* lens for which the view is (apply list* target), and list* is not surjective because (equal? (list* 1 2 '(3)) (list* 1 '(2 3))), hence the ambiguity.

from lens.

jackfirth avatar jackfirth commented on September 22, 2024

Ah, I see the problem now. That revised contract makes sense to me.

from lens.

jackfirth avatar jackfirth commented on September 22, 2024

As to the original question - improper list manipulation lenses in general seems like a fine addition to the library. For syntax lists specifically, ideally I'd like to see some way to express stx->list*-lens and other improper syntax list lenses in terms of list*->list-lens through some composition or combinator, to avoid a whole pile of improper list functionality that's tied up in the syntax lenses.

For naming: the a->b-lens names are generally reserved for things that are isomorphisms (which have stronger requirements than lenses) and are discussed briefly in #204 and #205. The proposed possibly-improper-list-to-proper-list lens is not an isomorphism, so some other name like list-proper-lens would be better.

from lens.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.