I'm trying to compose 2 functions in a script and getting a type mismatch I can not solve. Here is the sample code:
def generate(start: Int, end: Int): Seq[Int] = (start until end).toSeq
def restrain(seq: Seq[Int]) = seq.dropWhile(_ < 20).takeWhile(_ < 60)
val com: (Int, Int) => Seq[Int] = (restrain _ compose generate)
By loading this in the REPL with:
:load test.sc
I get the following error:
val com: (Int, Int) => Seq[Int] = (restrain _ compose generate)
^
test.sc:1: error: type mismatch;
found : (Int, Int) => Seq[Int]
required: ? => Seq[Int]
What am I doing wrong?
The types Function2[Int, Int, Seq[Int]]
and Function1[(Int, Int), Seq[Int]]
are not the same. The (generate _)
produces the former, whereas for this composition, you need the latter. Try:
restrain _ compose (generate _).tupled