Note that there are some explanatory texts on larger screens.

plurals
  1. POInferred type of function that zips HLists
    primarykey
    data
    text
    <p>Thanks to <a href="https://github.com/milessabin/shapeless/wiki/Feature-overview:-shapeless-2.0.0" rel="nofollow">https://github.com/milessabin/shapeless/wiki/Feature-overview:-shapeless-2.0.0</a> I understand how to zip shapeless HLists:</p> <p>Import some stuff from Shapeless 2.0.0-M1:</p> <pre><code>import shapeless._ import shapeless.ops.hlist._ import syntax.std.tuple._ import Zipper._ </code></pre> <p>Create two HLists:</p> <pre><code>scala&gt; val h1 = 5 :: "a" :: HNil h1: shapeless.::[Int,shapeless.::[String,shapeless.HNil]] = 5 :: a :: HNil scala&gt; val h2 = 6 :: "b" :: HNil h2: shapeless.::[Int,shapeless.::[String,shapeless.HNil]] = 6 :: b :: HNil </code></pre> <p>Zip them:</p> <pre><code>scala&gt; (h1, h2).zip res52: ((Int, Int), (String, String)) = ((5,6),(a,b)) </code></pre> <p>Now try to define a function that does the same thing:</p> <pre><code>scala&gt; def f[HL &lt;: HList](h1: HL, h2: HL) = (h1, h2).zip f: [HL &lt;: shapeless.HList](h1: HL, h2: HL)Unit </code></pre> <p>The inferred return type is Unit, and indeed applying f to h1 and h2 does just that:</p> <pre><code>scala&gt; f(h1, h2) scala&gt; </code></pre> <p>Is there a way to define f such that I get ((5,6),(a,b)) back in this case?</p> <p>Ultimately what I'm trying to do is define a function that zips the two HLists and then maps over them, choosing either _1 or _2 based a coin toss, which would yield another HL.</p> <pre><code>object mix extends Poly1 { implicit def caseTuple[T] = at[(T, T)](t =&gt; if (util.Random.nextBoolean) t._2 else t._1) } </code></pre> <p>Which works fine in the REPL:</p> <pre><code>scala&gt; (h1, h2).zip.map(mix) res2: (Int, String) = (5,b) </code></pre> <p>But I'm getting tripped up on the above issue when trying to pull this into a function.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload