I'm new to Scala, and I'm running into this strange situation.
def bar[A, B](implicit foo: A => B): B = {
// do something
foo
}
And then I got error like
require B but found A => B
How should I get B form A => B
Here's the reason why I did this, I have two functions:
def funcA: String = {
def getStrA: String = "A"
// then there's the same operation in both functions
Try{ } match {
case Success(_) => getStrA
case Failure(_) => // exactlly same error handler in both function
}
}
def funcB: Int = {
def doSomething(x: Int): Int = {
// do something
x / 1
}
val x = 1
Try{ } match {
case Success(_) => doSomething(1)
case Failure(_) => // exactlly same error handler in both function
}
}
Here's what I want to achieve
def funcA: String = {
implicit def getStrA: String = "A"
bar
}
def funcB: Int = {
val x = 1
implicit def doSomething(x: Int): Int = {
// do something
x / 1
}
bar
}
def bar[A, B](implicit foo: A => B): B = {
Try{ } match {
case Success(_) => foo
case Failure(_) => // exactlly same error handler in both function
}
}
You have a conversion from A to B. You need to return B. The only way to do this is to pass A into the function. This signature has an implied assumption that you have some valid A value (most likely hardcoded) that you will always use here.
def bar[A, B](implicit foo: A => B): B = {
val a: A = ... // hmm...
foo(a)
}
Considering, that A is parametric, then you are either missing some information, or this A is impossible to create (it cannot be null because not all types can take null as a value), so you might need to throw exception in such case. Probably you are either missing some A provider or you should always fail this operation.
UPDATE:
There is no need for using implicits at all in your code:
def bar[B](f: onSuccess: A => B) =
Try{ some operations } match {
case Success(value) => onSuccess(value)
case Failure(_) => // error handler
}
def funcA = bar(_ => "A")
def funcB = bar(_ => 1)
I had an imperative program which deserializes to a Binary Tree from an array. Its a BFS algorithm. I was wondering how to do this in Scala with functional programming concepts.
class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {
val value: Int = _value
val left: TreeNode = _left
val right: TreeNode = _right
}
def createTree(list: Array[Int]): TreeNode = ???
For reference this is the imperative version in Javascript. The algorithm is described here. https://support.leetcode.com/hc/en-us/articles/360011883654-What-does-1-null-2-3-mean-in-binary-tree-representation-
class TreeNode {
constructor(val){
this.val = val;
this.left = this.right = null;
}
}
function makeTree(arr){
let root = new TreeNode(arr[0]);
let q = [root];
let index = 1;
while(q.length){
let node = q.splice(0,1)[0];
if(arr[index] != null){
node.left = new TreeNode(arr[index]);
q.push(node.left);
}
index++;
if(arr[index] != null){
node.right = new TreeNode(arr[index]);
q.push(node.right);
}
index++;
}
return root;
}
First of all, you can use a case class to simplify your tree class, and you should use Option instead of null:
case class Tree(value: Int, left: Option[Tree], right: Option[Tree])
Next, the main trouble here is because your tree is immutable, it needs to be built with a depth-first post-order traversal, and your serialization format requires a breadth-first level-order traversal. So you first have to deserialize to a data structure that can then be traversed in depth-first order. The following uses a Map from (row, column) to the node value:
#scala.annotation.tailrec
private def bfsTraverse(
serialized: List[Option[Int]],
queue: Queue[(Int, Int)],
map: Map[(Int, Int), Int]): Map[(Int, Int), Int] = {
val ((row, col), queueTail) = queue.dequeue
if (serialized.isEmpty) {
map
} else if (serialized.head.isEmpty) {
bfsTraverse(serialized.tail, queueTail, map)
} else {
val newQueue = queueTail.enqueueAll(List((row + 1, col * 2), (row + 1, col * 2 + 1)))
val newMap = map + ((row, col) -> serialized.head.get)
bfsTraverse(serialized.tail, newQueue, newMap)
}
}
Now you can use the output of that function to build your Tree:
private def buildTree(row: Int, col: Int, map: Map[(Int, Int), Int]): Option[Tree] = {
map.get((row, col)).map{value =>
Tree(value, buildTree(row + 1, col * 2, map), buildTree(row + 1, col * 2 + 1, map))
}
}
This solution is a bit verbose but uses some functional concepts and defines the data structures thoroughly.
The algorithm you provided works better with mutable nodes. It's possible to have a shorter solution with just one mutable class, but here, there are two versions (one node class with mutable left/right and the other completely immutable).
Case classes automatically provide a lot of useful features such as comparison and friendly print-out
The processValues function tail-recursively performs the tasks equivalent to the makeTree function you provided.
The #tailrec annotation tells the compiler to check that the function is tail recursive.
Pattern matching using the match and case keywords replace checking for null-ness or different subtypes, and the compiler can check for a non-exhaustive match clause.
The App trait allows you to easily make an object (static class) into an entrypoint to run some examples.
import scala.annotation.tailrec
sealed trait TreeNode[T]
sealed trait MutableTreeNode[T]
object MutableTreeNode {
final case class Empty[T]() extends MutableTreeNode[T]
case class Branch[T](val value: T) extends MutableTreeNode[T] {
protected var left: MutableTreeNode[T] = Empty()
protected var right: MutableTreeNode[T] = Empty()
def setLeft(newLeft: T): Branch[T] = {
left = Branch(newLeft)
left.asInstanceOf[Branch[T]] // shouldn't be necessary but compiler requires it
}
def setRight(newRight: T): Branch[T] = {
right = Branch(newRight)
right.asInstanceOf[Branch[T]]
}
override def toString: String = this.toImmutable().toString
/* converts given node to immutable version */
private def toImmutable(node: MutableTreeNode[T]): TreeNode[T] = {
node match {
case Empty() => TreeNode.Empty()
case b#Branch(value) => TreeNode.Branch(value, toImmutable(b.left), toImmutable(b.right))
}
}
def toImmutable():TreeNode[T] = toImmutable(this)
}
/**
* Modifies nodes inside of queue
*/
#tailrec def processValues[T](values: Seq[Option[T]], queue: Seq[MutableTreeNode.Branch[T]]): Unit = {
(queue, values) match {
case (Nil, _) => ()
case (_, Nil) => ()
case (qHead :: qTail, Some(vLeft) :: Some(vRight) :: vTail) =>
processValues(vTail, qTail :+ qHead.setLeft(vLeft) :+ qHead.setRight(vRight))
case (qHead :: qTail, Some(vLeft) :: None :: vTail) =>
processValues(vTail, qTail :+ qHead.setLeft(vLeft))
case (qHead :: qTail, None :: Some(vRight) :: vTail) =>
processValues(vTail, qTail :+ qHead.setRight(vRight))
case (qHead :: qTail, None :: None :: vTail) =>
processValues(vTail, qTail)
}
}
}
object TreeNode {
final case class Empty[T]() extends TreeNode[T]
final case class Branch[T](value: T, left: TreeNode[T], right: TreeNode[T]) extends TreeNode[T]
def deserialize[T](values: Seq[Option[T]]): TreeNode[T] = {
values match {
case Some(headVal) :: tail =>
val root: MutableTreeNode.Branch[T] = MutableTreeNode.Branch(headVal)
MutableTreeNode.processValues(tail, Seq(root))
root.toImmutable()
case Nil => Empty()
case _ => throw new RuntimeException("Invalid argument values")
}
}
}
object TreeNodeTest extends App {
val input = Seq(Some(5), Some(4), Some(7), None, None, Some(2), None)
val treeNode:TreeNode[Int] = TreeNode.deserialize(input)
println(treeNode)
}
As has been noted, Scala avoids null whenever possible, preferring Option to indicate the absence of a value.
Mutable variables are also shunned, which makes it much easier to construct a B-tree in a depth-first manner rather than breadth-first.
So all you really need is an easy-to-use breadth-first-serialization --to--> depth-first-serialization translator.
I did it in two steps.
//from Breadth-First-Serialization to full tree representation
def BFS2full[A](bfs:IndexedSeq[Option[A]]) :List[List[Option[A]]] = {
val bfsLen = bfs.length
if (bfs.isEmpty) Nil
else
List(bfs.head) :: List.unfold((List(bfs.head),1)){case (pr,idx) =>
Option.when(bfsLen > idx){
val ns = pr.foldLeft((List.empty[Option[A]],idx)){
case ((acc,x), None) => (acc ++ List(None,None), x)
case ((acc,x), _) => (acc ++ List(bfs.lift(x).flatten
,bfs.lift(x+1).flatten), x+2)
}
(ns._1, ns)
}
}
}
//from full tree representation to Depth-First-Serialization
def toDFS[A](lloa :List[List[Option[A]]]
,lvl :Int = 0) :List[Option[A]] = lloa match {
case Nil => List(None, None)
case List(None) :: Nil => List(None)
case List( oa ) :: tl => oa :: toDFS(tl, lvl)
case row :: tl => row.drop(lvl*2) match {
case List(None,None,_*) => List(None, None)
case List(None, ob ,_*) => None :: (ob::toDFS(tl,2*lvl + 1))
case List( oa ,None,_*) => (oa::toDFS(tl,2*lvl)) ++ List(None)
case List( oa , ob ,_*) => (oa :: toDFS(tl, 2*lvl)) ++
(ob :: toDFS(tl,2*lvl + 1))
}
}
Now let's parameterize the tree so that we can build Int trees, Float trees, String trees, etc.
We're also going to make the constructor private so that node creation is only done via factory methods.
case class Tree[A] private (value : A
,left : Option[Tree[A]]
,right : Option[Tree[A]])
All that's left is to supply the factory methods.
object Tree {
private def BFS2full[A]( . . . //as above
private def toDFS[A]( . . . //as above
def fromDFS[A](dfs :IterableOnce[Option[A]]) :Option[Tree[A]] = {
val itr = dfs.iterator
def loop(): Option[Tree[A]] =
Option.when(itr.hasNext)(itr.next())
.flatten
.map(new Tree(_,loop(),loop()))
loop()
}
def fromBFS[A](bfs:IndexedSeq[Option[A]]) :Option[Tree[A]] =
fromDFS(toDFS(BFS2full(bfs)))
}
testing:
Tree.fromBFS(Vector(Some('A'),None,Some('B'),Some('C'))).get
//res0: Tree[Char] = Tree(A,None,Some(Tree(B,Some(Tree(C,None,None)),None)))
Here's a basic solution. It doesn't use lazy vals or any data structures other than List, Option, and Either. You might find it easier to understand because of that or harder because of the verbosity.
I've defined Tree like this, just to make things easier.
sealed trait Tree[+T]
case class Node[+T](data: T, left: Tree[T], right: Tree[T]) extends Tree[T]
case object Empty extends Tree[Nothing]
Also, instead of an Array[Int], I'm using a List[Option[T]] (where T is a type parameter of the makeTree method). Some means there's a node, None is like -1 in your code. This is more idiomatic and also works for types other than Int.
The thing about doing this breadth-first is that you need to kinda keep passing the input list around to the children. First you try to make the left child, then when that's done, you try make the right child. Then you try to make the left child's children, then the right child's children, then their children, and so on.
One way to deal this without using var would be to take in the input list containing the serialized binary tree and see what the head of the list is. If it's a None, we can just return an Empty, because we know it's the end of the tree. If it's a Some however, we can't yet return a tree, but we can return another function that takes in the next round of input and returns a tree.
Since there are 2 different types that can be returned, these functions will be of type List[Option[T]] => Either[List[Option[T]] => ThisFunctionsType, Tree[T]]. Since the function may return another function of the same type, we'll have to define a new type that can return itself:
trait RecFun[T] extends ((List[Option[T]]) => (List[Option[T]], Either[RecFun[T], Tree[T]]))
The reason that it's List[Option[T]] => (List[Option[T]], Either[RecFun[T], Tree[T]]) and not just List[Option[T]] => Either[RecFun[T], Tree[T]] is that in case one child turns out to be a leaf somewhere in the middle of the list, we still need to continue, so the first element of the returned tuple contains the rest of the list after processing.
Now we can define this. The helper function is so that as long as the RecFun returns a Left[RecFun], it keeps passing the remaining input into that function.
def makeTree[T](list: List[Option[T]]): Tree[T] = {
def helper(f: RecFun[T], l: List[Option[T]]): Tree[T] =
f(l) match {
case (_, Right(tree)) => tree
case (next, Left(f)) => helper(f, next)
}
list match {
case Some(x) :: tail => helper(createRec(x), tail)
case _ => Empty
}
}
def createRec[T](data: T): RecFun[T] = {
case None :: Nil | Nil => (Nil, Right(Node(data, Empty, Empty)))
case Some(l) :: Nil => (Nil, Right(Node(data, Node(l, Empty, Empty), Empty)))
case lo :: ro :: rest =>
(rest, (lo, ro) match {
case (Some(l), Some(r)) =>
Left(waitForChildren(data, createRec(l), createRec(r)))
case (Some(l), None) =>
Left(waitForChild(Node(data, _, Empty), createRec(l)))
case (None, Some(r)) =>
Left(waitForChild(Node(data, Empty, _), createRec(r)))
case (None, None) => Right(Node(data, Empty, Empty))
})
}
def waitForChildren[T](data: T, leftF: RecFun[T], rightF: RecFun[T]): RecFun[T] =
input => {
val (next, res) = leftF(input)
res match {
case Right(tree) =>
(next, Left(waitForChild(Node(data, tree, _), rightF)))
case Left(leftF2) => {
val (next2, res2) = rightF(next)
(next2, Left(res2 match {
case Right(tree) => waitForChild(Node(data, _, tree), leftF2)
case Left(rightF2) => waitForChildren(data, leftF2, rightF2)
}))
}
}
}
def waitForChild[T](ctor: Tree[T] => Node[T], f: RecFun[T]): RecFun[T] =
input => {
val (next, res) = f(input)
(next, res match {
case Right(tree) => Right(ctor(tree))
case Left(recFun) => Left(waitForChild(ctor, recFun))
})
}
Scastie
I have an array of objects of type Either[A, B]. If I know for a particular element whether it is an A or a B, how do I call a method on it that only exists on one of the 2 types. For example:
import scala.util.Random
object EitherTest extends App {
def newObj(x: Int): Either[A,B] = {
if (x == 0)
Left(new A())
else
Right(new B())
}
val random = new Random()
val randomArray = (0 until 10).map(_ => random.nextInt(2))
val eitherArray = randomArray.map(newObj)
(0 until 10).foreach(x => randomArray(x) match {
case 0 => eitherArray(x).aMethod()
case 1 => eitherArray(x).bMethod()
case _ => println("Error!")
})
}
class A {
def aMethod() = println("A")
}
class B {
def bMethod() = println("B")
}
When I compile this code, the lines
case 0 => eitherArray(x).aMethod()
case 1 => eitherArray(x).bMethod()
both have the error "value aMethod is not a member of Either[A,B]". How can I solve this?
I don't know why fold doesn't get the respect it deserves. It can be so useful.
eitherArray.foreach(_.fold(_.aMethod(), _.bMethod()))
Well, you can do it if you exctract the logic to another method, and do some pattern matching over the value Either, then check if it is Right or Left, and that's it!
object HelloWorld {
import scala.util.Random
def main(args: Array[String]) {
val random = new Random()
val randomArray = (0 until 10).map(_ => random.nextInt(2))
val eitherArray = randomArray.map(EitherTest.newObj)
(0 until 10).foreach(x => randomArray(x) match {
case 0 => EitherTest.callmethod(eitherArray(x))
case 1 => EitherTest.callmethod(eitherArray(x))
case _ => println("Error!")
})
println("Hello, world!")
}
}
class EitherTest
object EitherTest {
def callmethod(ei : Either[A,B]) = {
ei match {
case Left(a) => a.aMethod()
case Right(b) => b.bMethod()
}
}
def newObj(x: Int): Either[A,B] = {
if (x == 0)
Left(new A())
else
Right(new B())
}
}
class A {
def aMethod() = println("A")
}
class B {
def bMethod() = println("B")
}
Will print for you, for one random example:
A
B
A
B
A
A
A
B
B
B
Hello, world!
Basically, the way you do with Either is projections: Either.left gives you the projection of the left type, and Either.right gives you that of the right.
The projections are somewhat similar to options, in that they can be empty (if your Either is a Right, then the left projection is empty and vice versa), and you can use the usual monadic transformations with them, like map, flatMap, foreach, getOrElse etc.
Your example, could look like this:
randomArray.foreach { either =>
either.left.foreach(_.aMethod)
either.right.foreach(_.bMethod)
}
You could also use pattern-matching instead, that's less general, but, perhaps looks a bit clearer in this case:
randomArray.foreach {
case Left(a) => a.aMethod
case Right(b) => b.bMethod
}
In Scala we have a by-name-parameters where we can write
def foo[T](f: => T):T = {
f // invokes f
}
// use as:
foo(println("hello"))
I now want to do the same with an array of methods, that is I want to use them as:
def foo[T](f:Array[ => T]):T = { // does not work
f(0) // invokes f(0) // does not work
}
foo(println("hi"), println("hello")) // does not work
Is there any way to do what I want? The best I have come up with is:
def foo[T](f:() => T *):T = {
f(0)() // invokes f(0)
}
// use as:
foo(() => println("hi"), () => println("hello"))
or
def foo[T](f:Array[() => T]):T = {
f(0)() // invokes f(0)
}
// use as:
foo(Array(() => println("hi"), () => println("hello")))
EDIT: The proposed SIP-24 is not very useful as pointed out by Seth Tisue in a comment to this answer.
An example where this will be problematic is the following code of a utility function trycatch:
type unitToT[T] = ()=>T
def trycatch[T](list:unitToT[T] *):T = list.size match {
case i if i > 1 =>
try list.head()
catch { case t:Any => trycatch(list.tail: _*) }
case 1 => list(0)()
case _ => throw new Exception("call list must be non-empty")
}
Here trycatch takes a list of methods of type ()=>T and applies each element successively until it succeeds or the end is reached.
Now suppose I have two methods:
def getYahooRate(currencyA:String, currencyB:String):Double = ???
and
def getGoogleRate(currencyA:String, currencyB:String):Double = ???
that convert one unit of currencyA to currencyB and output Double.
I use trycatch as:
val usdEuroRate = trycatch(() => getYahooRate("USD", "EUR"),
() => getGoogleRate("USD", "EUR"))
I would have preferred:
val usdEuroRate = trycatch(getYahooRate("USD", "EUR"),
getGoogleRate("USD", "EUR")) // does not work
In the example above, I would like getGoogleRate("USD", "EUR") to be invoked only if getYahooRate("USD", "EUR") throws an exception. This is not the intended behavior of SIP-24.
Here is a solution, although with a few restrictions compared to direct call-by-name:
import scala.util.control.NonFatal
object Main extends App {
implicit class Attempt[+A](f: => A) {
def apply(): A = f
}
def tryCatch[T](attempts: Attempt[T]*): T = attempts.toList match {
case a :: b :: rest =>
try a()
catch {
case NonFatal(e) =>
tryCatch(b :: rest: _*)
}
case a :: Nil =>
a()
case Nil => throw new Exception("call list must be non-empty")
}
def a = println("Hi")
def b: Unit = sys.error("one")
def c = println("bye")
tryCatch(a, b, c)
def d: Int = sys.error("two")
def e = { println("here"); 45 }
def f = println("not here")
val result = tryCatch(d, e, f)
println("Result is " + result)
}
The restrictions are:
Using a block as an argument won't work; only the last expression of the block will be wrapped in an Attempt.
If the expression is of type Nothing (e.g., if b and d weren't annotated), the conversion to Attempt is not inserted since Nothing is a subtype of every type, including Attempt. Presumably the same would apply for an expression of type Null.
As of Scala 2.11.7, the answer is no. However, there is SIP-24, so in some future version your f: => T* version may be possible.
I want to the get the manifest of one List's inner type like following and pass it to another function, how can I do that ? Thanks
def f(any: Any) = any match {
case x: Int => println("Int")
case a: List[_] => // get the manifest of List's inner type, and use it in the function g()
}
def g[T:Manifest](list:List[T]) = {}
Add the manifest as an implicit requirement to your method, and tweak the type signature a tiny bit:
def f[T](any: T)(implicit mf: Manifest[T]) = mf match {
case m if m == Manifest[Int] => println("Int")
case m if m == Manifest[List[Int]] => println("List of Ints")
//etc...
}
The Manifest class has a method, typeArguments, that should serve your purpose for finding the "inner type". For example
manifest[List[Int]].typeArguments == List(manifest[Int])
You could tweak #Dylan's answer a bit and try this as well:
object ManifestTest {
def f[T](t: T)(implicit m:Manifest[T]) = t match {
case x: Int => println("Int")
case a: List[Any] =>
val innerType = m.typeArguments.head
println(innerType)
}
def main(args: Array[String]) {
f(1)
f(List("hello", "world"))
f(List(1))
}
}