Scala really weird Type Mismatch - scala

I'm trying to implement dropWhile in Scala but I get a type mismatch, on the invocation of "f(h)" error that says it actually found the type it was expecting:
def dropWhile[A](l: XList[A])(f: A => Boolean): XList[A] = {
def dropWhile[A](toCheck: XList[A], toKeep: XList[A]) : XList[A] = toCheck match {
case XNil => toKeep
case Cons(h, t) if **f(h)** == false => dropWhile(tail(toCheck), Cons(h, toKeep))
case Cons(_, Cons(t1, t2)) => dropWhile(Cons(t1, t2), toKeep)
}
dropWhile(l, XList[A]())
}
error message:
found : h.type (with underlying type A)
[error] required: A
relevant code:
sealed trait XList[+A] {}
case object XNil extends XList[Nothing]
case class Cons[+A](head: A, tail: XList[A]) extends XList[A]
EDIT:
Here's a way to make it compile - but the winning answer is better and explains why as well.
def dropWhile[A](l: XList[A])(f: A => Boolean): XList[A] = {
#tailrec
def dropWhile[A](toCheck: XList[A], toKeep: XList[A], dropItem: A => Boolean): XList[A] = toCheck match {
case Cons(h, XNil) if !dropItem(h) => Cons(h, toKeep)
case Cons(h, XNil) if dropItem(h) => toKeep
case Cons(h, t) if !dropItem(h) => dropWhile(t, Cons(h, toKeep), dropItem)
case Cons(h, t) if dropItem(h) => dropWhile(t, toKeep, dropItem)
}
dropWhile(l, XList[A](), f)
}

You already have type parameter A on the original 'dropWhile' which is scoping the type of f. However, you then introduce a second type parameter on the inner def which shadows the outer definition of A and scopes the type of the XList. So the problem is that the A are not the same type! If you remove the shadowed type, it all works (few other changes made to get your code to compile):
def dropWhile[A](l: XList[A])(f: A => Boolean): XList[A] = {
def dropWhile(toCheck: XList[A], toKeep: XList[A]) : XList[A] = toCheck match {
case XNil => toKeep
case Cons(h, t) if f(h) == false => dropWhile(t, Cons(h, toKeep))
case Cons(_, Cons(t1, t2)) => dropWhile(Cons(t1, t2), toKeep)
}
dropWhile(l, XNil)
}

You could just use a foldRight instead (I've simplified this to use List rather than XList):
scala> def dropWhile[A](l: List[A])(f: A => Boolean): List[A] =
| l.foldRight(List[A]())((h,t) => if (f(h)) t else h :: t)
dropWhile: [A](l: List[A])(f: A => Boolean)List[A]
scala> val l = List(1,2,3,4,5,6,5,4,3,2,1)
l: List[Int] = List(1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1)
scala> l.dropWhile(_ < 4)
res1: List[Int] = List(4, 5, 6, 5, 4, 3, 2, 1)
Doesn't answer your found : h.type (with underlying type A) question though :-)

Related

Is it possible to pattern match on a by-name parameter without evaluating it?

Was playing with Lazy Structure Stream as below
import Stream._
sealed trait Stream[+A] {
..
def toList: List[A] = this match {
case Empty => Nil
case Cons(h, t) => println(s"${h()}::t().toList"); h()::t().toList
}
def foldRight[B](z: B) (f: ( A, => B) => B) : B = this match {
case Empty => println(s"foldRight of Empty return $z"); z
case Cons(h, t) => println(s"f(${h()}, t().foldRight(z)(f))"); f(h(), t().foldRight(z)(f))
}
..
}
case object Empty extends Stream[Nothing]
case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A]
object Stream {
def cons[A](h: => A, t: => Stream[A]): Stream[A] = {
lazy val hd = h
lazy val tl = t
Cons[A](() => hd, () => tl)
}
def empty[A]: Stream[A] = Empty
def apply[A](la: A*): Stream[A] = la match {
case list if list.isEmpty => empty[A]
case _ => cons(la.head, apply(la.tail:_*))
}
}
For a function takeWhile via foldRight i initially wrote:
def takeWhileFoldRight_0(p: A => Boolean) : Stream[A] = {
foldRight(empty[A]) {
case (a, b) if p(a) => println(s"takeWhileFoldRight cons($a, b) with p(a) returns: cons($a, b)"); cons(a, b)
case (a, b) if !p(a) => println(s"takeWhileFoldRight cons($a, b) with !p(a) returns: empty[A]"); empty[A]
}
}
Which when called as:
Stream(4,5,6).takeWhileFoldRight_0(_%2 == 0).toList
result in the following trace:
f(4, t().foldRight(z)(f))
f(5, t().foldRight(z)(f))
f(6, t().foldRight(z)(f))
foldRight of Empty return Empty
takeWhileFoldRight cons(6, b) with p(a) returns: cons(6, b)
takeWhileFoldRight cons(5, b) with !p(a) returns: empty[A]
takeWhileFoldRight cons(4, b) with p(a) returns: cons(4, b)
4::t().toList
res2: List[Int] = List(4)
Then questioning and questioning i figured that it might have been the unapply method in the pattern match that evaluate eagerly.
So i changed to
def takeWhileFoldRight(p: A => Boolean) : Stream[A] = {
foldRight(empty[A]) { (a, b) =>
if (p(a)) cons(a, b) else empty[A]
}
}
which when called as
Stream(4,5,6).takeWhileFoldRight(_%2 == 0).toList
result in the following trace:
f(4, t().foldRight(z)(f))
4::t().toList
f(5, t().foldRight(z)(f))
res1: List[Int] = List(4)
Hence my question:
Is there a way to recover the power of pattern match when working with by-name parameter ?
Said differently case i match parameter that are by-name without evaluating them eagerly ?
Or i have to go to a set of ugly nested "if" :p in that kind of scenario
Take a closer look at this fragment:
def toList: List[A] = this match {
case Empty => Nil
case Cons(h, t) => println(s"${h()}::t().toList"); h()::t().toList
}
def foldRight[B](z: B) (f: ( A, => B) => B) : B = this match {
case Empty => println(s"foldRight of Empty return $z"); z
case Cons(h, t) => println(s"f(${h()}, t().foldRight(z)(f))"); f(h(), t().foldRight(z)(f))
}
..
}
Here h and t in Cons aren't evaluated by unapply - after all unapply returns () => X functions without calling them. But you do. Twice for each match - once for printing and once for passing the result on. And you aren't remembering the result, so any future fold, map, etc would evaluate the function anew.
Depending on what behavior you want to have you should either:
Calculate the results once, right after matching them:
case Cons(h, t) =>
val hResult = h()
val tResult = t()
println(s"${hResult}::tail.toList")
hResult :: tResult.toList
or
not use case class because it cannot memoize the result and you might need to memoize it:
class Cons[A](fHead: () => A, fTail: () => Stream[A]) extends Stream[A] {
lazy val head: A = fHead()
lazy val tail: Stream[A] = fTail()
// also override: toString, equals, hashCode, ...
}
object Cons {
def apply[A](head: => A, tail: => Stream[A]): Stream[A] =
new Cons(() => head, () => tail)
def unapply[A](stream: Stream[A]): Option[(A, Stream[A])] = stream match {
case cons: Cons[A] => Some((cons.head, cons.tail)) // matches on type, doesn't use unapply
case _ => None
}
}
If you understand what you're doing you could also create a case class with overridden apply and unapply (like above) but that is almost always a signal that you shouldn't use a case class in the first place (because most likely toString, equals, hashCode, etc would have nonsensical implementation).

Shapeless type disjunction for more then 2 types

How to provide a type disjunction for 3 or more types in shapeless? Example:
import shapeless._
object Tst extends App {
sealed trait Base
final case class A() extends Base
final case class B() extends Base
final case class C() extends Base
final case class D() extends Base
def AorB[T: (A |∨| B)#λ](t: T): Unit =
t match {
case _: A => println("A")
case _: B => println("B")
}
AorB(A()) //Ok
AorB(B()) //Ok
def AorBorC[T: (A |∨| B |∨| C)#λ](t: T): Unit =
t match {
case _: A => println("A")
case _: B => println("B")
case _: C => println("C")
}
AorBorC(A()) //compile-error
AorBorC(B()) //compile-error
AorBorC(C()) //Ok
}
As can be seen for disjunction of 2 types it works completely fine. But for disjunction of 3 types it does not work as expected.
Compile errors are:
Error:(28, 10) Cannot prove that (Tst.A => Nothing) => Nothing <:< Object{type λ[X] = (X => Nothing) => Nothing <:< Tst.A => Nothing with Tst.B => Nothing => Nothing} => Nothing with Tst.C => Nothing => Nothing.
AorBorC(A())
and
Error:(29, 10) Cannot prove that (Tst.B => Nothing) => Nothing <:< Object{type λ[X] = (X => Nothing) => Nothing <:< Tst.A => Nothing with Tst.B => Nothing => Nothing} => Nothing with Tst.C => Nothing => Nothing.
AorBorC(B())
shapeless.|∨| doesn't work for more than 2 types.
http://milessabin.com/blog/2011/06/09/scala-union-types-curry-howard/
For more than 2 types encoding becomes more complicated.
One encoding is for 2, 4, 8 ... types
type ¬¬¬¬[T] = ¬¬[¬¬[T]]
type |∨∨|[T, U] = {
type λ[X] = ¬¬¬¬[X] <:< (T ∨ U)
}
def AorBorC[T: ((A ∨ B) |∨∨| (C ∨ C))#λ](t: T): Unit =
t match {
case _: A => println("A")
case _: B => println("B")
case _: C => println("C")
}
AorBorC(A()) //Ok
AorBorC(B()) //Ok
AorBorC(C()) //Ok
Another is for arbitrary number of types
trait Disj[T] {
type or[S] = Disj[T with ¬[S]]
type apply = ¬[T]
}
type ∨∨∨[T1, T2, T3] = Disj[¬[T1]]#or[T2]#or[T3]#apply
type |∨∨∨|[T1, T2, T3] = {
type λ[X] = ¬¬[X] <:< ∨∨∨[T1, T2, T3]
}
def AorBorC[T: |∨∨∨|[A, B, C]#λ](t: T): Unit =
t match {
case _: A => println("A")
case _: B => println("B")
case _: C => println("C")
}
AorBorC(A()) //Ok
AorBorC(B()) //Ok
AorBorC(C()) //Ok
How to define "type disjunction" (union types)?

How to solve type missmatch in scala

i need some help with this code in scala, i want to implement foldL method but im getting this:
asd.scala:73: error: type mismatch;
found : Option[MyTree[A]] => B
required: B
def myFoldLeft[B](z: B)(op: (B, A) => B): B = (_:Option[MyTree[A]]) match {
^
one error found
I know thats a type missmatch but im newbie with scala and Oriented Object and i dont
understand how to solve this situation.
class MyTree[A](val value: A, val left:Option[MyTree[A]],
val right:Option[MyTree[A]]) {
def myFoldLeft[B](z: B)(op: (B, A) => B): B = (_:Option[MyTree[A]]) match {
case Some(tree) => right.get.myFoldLeft (left.get.myFoldLeft (op(z, value)) (op)) (op)
case None => z
}
}
(_:Option[MyTree[A]]) ... is a lambda.
You should match like
class MyTree[A](val value: A, val left:Option[MyTree[A]],
val right:Option[MyTree[A]]) {
def myFoldLeft[B](z: B)(op: (B, A) => B): B = (left, right) match {
case (Some(left), Some(right)) => ???
case (Some(left), None) => ???
case (None, Some(right)) => ???
case (None, None) => ???
}
}

Inline function ambiguity in Scala

When passing an operator lifted to be a function to one of defined higher order functions, Scala allows for very concise syntax, e.g (please ignore the fact that it can be simplified to .product()):
List(1,2,3).fold(1)(_ * _)
To the above I can just pass _ \* _
However having defined my own toy function zipWith(), I need to be very explicit when passing a function:
implicit class EnrichedList[A](val self: List[A]) extends AnyVal {
def zipWith[B, C](that: List[B])
(implicit zipper: A => B => C): List[C] = {
def zipWithHelper(zipper: A => B => C)
(as: List[A])
(bs: List[B]): List[C] = {
(as, bs) match {
case (_, Nil) => Nil
case (Nil, _) => Nil
case (a :: restOfA, b :: restOfB) =>
zipper(a)(b) :: zipWithHelper(zipper)(restOfA)(restOfB)
}
}
zipWithHelper(zipper)(self)(that)
}
}
This: List(1, 3, 4).zipWith(List(3, 4, 5))(_ * _) will not work, saying
Error:(60, 46) missing parameter type for expanded function ((x$1: , x$2) => x$1.$times(x$2))
List(1, 3, 4).zipWith(List(3, 4, 5))(_ * _)
I need to say what type of arguments function takes:
List(1, 3, 4).zipWith(List(3, 4, 5))((x: Int) => (y: Int) => x * y)
Why won't the compiler allow me just to pass in a shorthand version _ * _?
The expression _ * _ is not shorthand for (x: Int) => (y: Int) => x * y. It's a shorthand for (x: Int, y: Int) => x * y. If you change the type of zipper to (A, B) => C instead of A => B => C, it should work. Currying is a thing, it's not just a fancy name for an identity function.
This here compiles:
implicit class EnrichedList[A](val self: List[A]) {
def zipWith[B, C](that: List[B])
(implicit zipper: (A, B) => C): List[C] = {
def zipWithHelper(zipper: (A, B) => C)
(as: List[A])
(bs: List[B]): List[C] = {
(as, bs) match {
case (_, Nil) => Nil
case (Nil, _) => Nil
case (a :: restOfA, b :: restOfB) =>
zipper(a, b) :: zipWithHelper(zipper)(restOfA)(restOfB)
}
}
zipWithHelper(zipper)(self)(that)
}
}
println( List(1, 3, 4).zipWith(List(3, 4, 5))(_ * _) )
and prints
List(3, 12, 20)

Scala: Using a TypeTag to match on a Some's type

See the following code:
def createOption[T: TypeTag](referentialData: Any) : Option[T] = {
Option(referentialData) match {
case Some(camelMessage: CamelMessage) => {
Option(camelMessage.body) match {
case Some(option: T) => Some(option)
case _ => None
}
}
case _ => None
}
}
Basically I am looking to return an Option[T] if camelMessage.body is non-null and of type T.
The uses of Option(referentialData) is effectively referentialData != null
Likewise for Option(camelMessage.body)
How do I use the TypeTag to determine if camelMessage.body is of type T.
(I know this can be re-written to not use TypeTags and Options but I want to learn how to use TypeTags so please no suggestions to re-write, thanks!)
Edit
I tried a new approach as could not find a solution for the above, but could not get this one to work either:
def createOption[T](referentialData: Any) : Option[T] = {
Option(referentialData) match {
case Some(option) => Try(option.asInstanceOf[T]).toOption
case _ => None
}
}
When I invoke this using createOption[Long]("test") I was presuming to get a None back, but instead I got a Some(String)
Where am I going wrong here?
This is a duplicate of this one.
But you want to try it with ClassTag to show the limitation:
scala> def f[A: ClassTag](x: Any): Option[A] = x match {
| case y: A => println("OK"); Some(y) ; case _ => println("Nope"); None }
f: [A](x: Any)(implicit evidence$1: scala.reflect.ClassTag[A])Option[A]
scala> f[String]("foo")
OK
res0: Option[String] = Some(foo)
scala> f[Long](2L)
Nope
res1: Option[Long] = None
scala> f[java.lang.Long](new java.lang.Long(2L))
OK
res2: Option[Long] = Some(2)
scala> def f[A: TypeTag](x: Any): Option[A] = Option(x) match {
| case Some(y: A) => println("OK"); Some(y) ; case _ => println("Nope"); None }
<console>:51: warning: abstract type pattern A is unchecked since it is eliminated by erasure
case Some(y: A) => println("OK"); Some(y) ; case _ => println("Nope"); None }
^
f: [A](x: Any)(implicit evidence$1: reflect.runtime.universe.TypeTag[A])Option[A]