Arbitrary Function - Generate return type according to input

21 views
Skip to first unread message

Tase Gula

unread,
Sep 15, 2015, 6:29:02 PM9/15/15
to Shapeless Dev
Hi,

Let's get right into the subject: giving this classes

class Step
case class Publish(p: String) extends Step
case class Receive(p: String) extends Step

class Data
case class Input(p: Int) extends Data
case class Output(p: Int) extends Data

type P
= Publish
type R
= Receive
type I
= Input
type O
= Output

I would like to define an object that takes a sequence of Step and returns a sequence of Data, according to this mapping: P -> I and R -> O.
So, if the sequence is: P, R, P, R, R, P, R, the result should be: I, O, I, O, O, I, O.

Right now I have this objects, which may clarify what I'm trying to do:

object Flow {
   
def apply[T <: HList, S <: HList](t: T)
       
(implicit mapper : Mapper[IOFlow.type, T]): S => Unit = {
        val s
= t.map(IOFlow)
        s
=> ()
   
}
}

object IOFlow extends Poly1 {
   
implicit val in = at[P]{ i => makeI(i) }
   
implicit val out = at[R]{ r => makeO(r) }

   
def makeI(p: P) = Input // so the return type is Input.type
   
def makeO(p: R) = Output // and Output.type
}

object Main extends App {
    val initialList
= HList(P("1"), R("2"), P("3"), R("4"), R("5"), P("6"), R("7"))
    val flow
= Flow(initialList)
}

Right now, this gives me an error, which actually occurs at val s = t.map(IOFlow):

Error: could not find implicit value for parameter mapper: shapeless.ops.hlist.Mapper[com.tasegula.scala.shapeless.IOFlow.type,this.Repr]
val flow
= Flow(hlist)
               
^

Trying to resolve this, I stumbled upon this topic, which have a source code here.
I can't compile and run the code because I can't find that MapperAux class (line 36).

So the questions are:

1. How can I give an implicit mapper?
2. Is there a way to change the Flow#apply method declaration to apply(t: Step*), so the call would be Flow(P("1"), R("2"), P("3"), R("4"), R("5"), P("6"), R("7"))?

Also, I posted this on StackOverflow, related to the same question.

Thanks!

Miles Sabin

unread,
Sep 16, 2015, 4:20:47 AM9/16/15
to shapel...@googlegroups.com
Hi,

This mailing list is largely deprecated now ... the best place to ask
questions is on the shapeless gitter channel
(https://gitter.im/milessabin/shapeless) or Stack Overflow.

Cheers,


Miles
> --
> You received this message because you are subscribed to the Google Groups
> "Shapeless Dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to shapeless-de...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Miles Sabin
tel: +44 7813 944 528
skype: milessabin
gtalk: mi...@milessabin.com
http://milessabin.com/blog
http://twitter.com/milessabin
Reply all
Reply to author
Forward
0 new messages