0

I'm trying to solve this problem. I want to implement this function in Scala

def toSeq[U,T](f: U=>Seq[T]): Stream[U=>T] = ???

I guess the solution should make use of Stream, but I don't know how to solve this.

EDIT

I will try to describe the use case that lead me to this problem. I am creating a DSL to express extractions of data from different datasources. The DSL allows to define extractions like this (U is the source type and T is the extraction type)

type Mapping[-U, T] = U => T    
trait Source[T , U]
case class SingleValueSource[T  U](mappings: Seq[Mapping[U, T]]) extends Source[T, U]
case class OptionalValueSource[T , U](mappings: Seq[Mapping[U, Option[T]]]) extends Source[T, U]
case class SeqValueSource[T , U](mapping: Mapping[U, Seq[T]]) extends Source[T, U]

I want to be able to combine all this mappings to build new Source, for example

def addMapping(source: Source[U,T], mapping: Mapping[U, T]): Source[U,T]
def addMapping(source: Source[U,T], mapping: Mapping[U, Option[T]]): Source[U,T]
def addMapping(source: Source[U,T], mapping: Mapping[U, Seq[T]]): Source[U,T]

The function I am asking came up when I was trying to implement

def addMapping(source: SeqValueSource[U,T], mapping: Mapping[U, Option[T]]): Source[U,T]

But maybe there's a better way

Mikel San Vicente
  • 3,831
  • 2
  • 21
  • 39
  • This is not possible (the function `U => Seq[T]` can return different length `Seq`s, bu the result of calling `toSeq` returns one fixed-length `Seq`). Did you mean the inverse of this function? – Alec Nov 14 '16 at 18:40
  • 1
    I agree that the problem is, at best, underspecified. Can you explain how you want this `toSeq` method to actually behave...? – Seth Tisue Nov 14 '16 at 18:55
  • @Alec I agree that it's impossible with all collections except the lazy ones, but I am not sure when we use a lazy collection like Stream... but you might be right and this is just a problem with no solution – Mikel San Vicente Nov 14 '16 at 19:09
  • I have edited the signature of the function to use Stream as the return value – Mikel San Vicente Nov 14 '16 at 19:09
  • I think @Seth Tisue has a point - could you elaborate with an example? – Alec Nov 14 '16 at 19:10
  • ok, I am going to edit – Mikel San Vicente Nov 14 '16 at 19:11
  • I don't think it's generally possible. If you pass an `f` that just returns an empty `Seq`, what should be the output? An empty stream? But you don't know that `f` returns an empty `Seq` until you apply it. You should provide more context on what you are trying to achieve. – laughedelic Nov 14 '16 at 19:41
  • I have edited the question – Mikel San Vicente Nov 14 '16 at 20:18
  • @Mikel I guess you mixed up the arguments of `Source` in one snippet. Also, I don't get what `addMapping` methods are supposed to do. Can you give some examples for the cases you implemented? – laughedelic Nov 14 '16 at 22:35
  • @Mikel another question: what do all source types have in common? Shouldn't `SeqValueSource` have `mapping: Seq[Mapping[U, Seq[T]]]`? It seems more consistent. – laughedelic Nov 14 '16 at 22:46
  • I want to be able to build a Source adding Mapping to an existing one, the resulting Source will perform the new mapping plus the other mappings – Mikel San Vicente Nov 14 '16 at 23:42

0 Answers0