samedi 16 juin 2018

anonymous pattern matching

I think my dilemma is actually "what is anonymous pattern matching"? This is the error i get below.

type Bit = Int

def function(bits: List[Bit]): something = 
  bits.map(
    bit => bit match {
      case 0 =>  <do something 0>
      case 1 =>  <do something 1>
    }
  )

I have found the fix. It is below.

bits.foreach(
          case 0 =>  <do somthing 0>
          case 1 =>  <do somthing 1>
      )

I want to understand my error before i dumbly apply the solution. (Why cant i use .map? ) Thank you.

Aucun commentaire:

Enregistrer un commentaire