lens icon indicating copy to clipboard operation
lens copied to clipboard

There should be some way to easily convert between hashes and lists

Open jackfirth opened this issue 10 years ago • 3 comments

The naive approach of hash->list + list->hash doesn't work because no ordering is guaranteed, this breaks the lens laws. One possible approach:

  • hash to set isomorphism where set contains pairs
  • set to list isomorphism constructor that accepts an ordering procedure and promises to always return sorted lists
  • assoc-list to list isomoprhism

Hash to flat list would then be hash -> set -> assoc -> list, and would require an ordering procedure to construct the isomorphism.

jackfirth avatar Aug 21 '15 17:08 jackfirth

What would the assoc-list to list isomorphism be? Wouldn't the assoc list already be what you want?

AlexKnauth avatar Sep 05 '15 17:09 AlexKnauth

You're right. I was thinking an assoc list to list isomorphism would be something for converting '(a 1 b 2 c 3) to '((a . 1) (b . 2) (c . 3))

jackfirth avatar Sep 05 '15 20:09 jackfirth

This isn't really relevant for converting between hashes and lists, but an isomorphism for converting '(a 1 b 2 c 3) to '((a . 1) (b . 2) (c . 3)) is defined here as alternating->assoc-list-lens: https://github.com/jackfirth/lens/blob/master/lens-data/lens/private/isomorphism/data.rkt#L28 I put it there planning to use it for lens-join/assoc, but I never ended up using it as a lens.

AlexKnauth avatar Sep 05 '15 23:09 AlexKnauth