Hope a few things to eliminate your confusion:
Compliance in scala follows a common pattern:
x match { case SomethingThatXIs if(SomeCondition) => SomeExpression // rinse and repeat // note that `if(SomeCondition)` is optional }
It looks like you might have tried to use the match / case expression as more of the if / else if / else block, and as far as I can tell, x doesn't really matter within the specified block. If that happens, you might be fine with something like
case _ if (d(counter)._1 == key) => d(counter)._2
BUT
Some information about List in scala. You should always think of it as a LinkedList , where an indexed search is an O(n) operation. Lists can be mapped to head :: tail , and Nil can be an empty list. For instance:
val myList = List(1,2,3,4) myList match { case first :: theRest =>
It looks like you are creating a kind of ListMap, so I will write a more "functional" / "recursive" way to implement your get method.
I assume d is a support list of type List[(String, Any)]
def get(key: String): Option[Any] = { def recurse(key: String, list: List[(String, Any)]): Option[Any] = list match { case (k, value) :: _ if (key == k) => Some(value) case _ :: theRest => recurse(key, theRest) case Nil => None } recurse(key, d) }
Three cases can be explained as follows:
1) The first element in a List is a collection of (k, value) . The rest of the list matches _ , because in this case we do not care about that. The condition asks if k key we are looking for. In this case, we want to return value from the tuple.
2) Since the first element did not have the correct key, we want to return it. We do not care about the first element, but we need the rest of the list so that we can rewrite it.
3) case Nil means that there is nothing in the list, which should mean "failure" and the end of the recursion. In this case, we return None . Consider this the same as your counter > acc clause from your question.
Please feel free to contact for further clarification; and if I accidentally made a mistake (I donβt compile, etc.), indicate this and I will fix it.