0

Is there any lens that will help me do the following transformation for a tuple of any length (say up 10-15 elements, at least):

(a, b, c) -> d -> (a, b, c, d)
Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
  • I can't see anything like that in Control.Lens.Tuple (which only supports tuples up to 9 elements anyway), so it seems unlikely. Did you need help writing your own lens for this, or were you just checking to make sure you wouldn't be duplicating something that already existed? – K. A. Buhr Dec 28 '16 at 18:54
  • You may need this hack: https://ivanmiljenovic.wordpress.com/2016/12/28/transmogrify-your-data/ – samsergey Dec 28 '16 at 21:37
  • Not writing a lens library. Need to do something like this in actual code where I'm building up a large tuple one/two elements at a time. Very surprised that there isn't a generic `concatTuple` function that works on tuples of any length. – Saurabh Nanda Dec 29 '16 at 02:51
  • And there's probably a good reason why I can't use a list. Each of my intermediate tuples is the result of an Opaleye leftJoinF. I'm not sure how Opaleye's type inference would work with lists. – Saurabh Nanda Dec 29 '16 at 02:53
  • @samgersey thanks for the hack. That might help me write my own version of concatTuple. – Saurabh Nanda Dec 29 '16 at 02:55

1 Answers1

2

To get a lens you need a getter an setter functions. Unfortunately, there is no way to obtain a fourth element of a triple, (except for Nothing, or any other unit type). So, you end up with a bunch of setters, which are trivial pattern-matching functions (one for each n-tuple), but not a lens.

Probably, you need a simple list, or some free construction, if you really need non-uniform container?

samsergey
  • 228
  • 1
  • 8
  • 1
    Well, it would not be a [simple](http://hackage.haskell.org/package/lens-4.15.1/docs/Control-Lens-Type.html#t:Simple) lens, would it? The setter would have type `(a,b,c) -> d -> (a,b,c,d)` and the getter `(a,b,c,d) -> d`, for a `Lens (a,b,c,d) (a,b,c) d d`. – leftaroundabout Dec 28 '16 at 22:21
  • As it sais in docs, _"A Simple Lens, Simple Traversal, ... can be used instead of a Lens,Traversal, ... whenever the type variables don't change upon setting a value."_ In our case it does change from `(a,b,c)` to `(a,b,c,d)`. – samsergey Dec 28 '16 at 22:56
  • I think you're both talking past each other here -- @samsergey said "simple *list*", not "simple lens", in his answer. – K. A. Buhr Dec 31 '16 at 04:09