Skip to content

Is the Set instance of ix a well-behaved optic? #83

Open
@marick

Description

@marick

profunctor-lenses has the ix function to produce 0-or-1 (“affine”) Traversals. It’s nice for arrays and maps. I noticed it also has an instance for Set. Fooling around, I saw the following:

     > result = set (ix "foo") unit Set.empty
     > result
     (fromFoldable ("foo" : Nil))

    > preview (ix "foo") result
    Nothing

This is peculiar in several ways:

  1. You cannot use (ix “foo”) to insert a new value into a Map:

    > set (ix "foo") "bar" Map.empty
    (fromFoldable [])

    … and yet you can with a Set.

  2. I’m not sure what form the set-get optics law should take with ix optics. But you can’t get what you can set, which seems odd. Actually, as far as I can tell, you can’t view or preview anything in a Set:

    > preview (ix "foo") (Set.singleton "foo")
    Nothing
    
    > view (ix "foo") (Set.singleton "foo")
    unit

Is the instance for Set a well-behaved optic? Should it be?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions