| in pattern matching

Can someone explain to me why the pipe character works here? The pipe character is a method or String ? StringOps? Tried searching the API, nope no pipe character method in either.

def doTranslate(condons:Iterator[String], accum:List[String]):List[String] = condons.hasNext match {
    case true => condons.next match {
      case "AUG" => doTranslate(condons, accum :+ "Methionine")	
      case "UUU" | "UUC" => doTranslate(condons, accum :+ "Phenylalanine")
      case "UUA" | "UUG" => doTranslate(condons, accum :+ "Leucine")	
      case "UCU" | "UCC" | "UCA" | "UCG" => doTranslate(condons, accum :+ "Serine")		
      case "UAU" | "UAC" => doTranslate(condons, accum :+ "Tyrosine")	
      case "UGU" | "UGC" => doTranslate(condons, accum :+ "Cysteine")	
      case "UGG" => doTranslate(condons, accum :+ "Tryptophan")
      case "UAA" | "UAG" | "UGA" => accum
    }
    case false => accum
  }

Hi

It’s part of the language itself rather than the library.

See Scala Language Specification 8.1.11 Pattern Alternatives [1].

Kind regards
Brian

[1]
http://scala-lang.org/files/archive/spec/2.12/08-pattern-matching.html#pattern-alternatives

3 Likes

By the way, you can express this as a mapping operation:

def translate(codons: Iterator[String]): Iterator[String] =
  codons map {
    case "AUG" => "Methionine"
    case "UUU" | "UUC" => "Phenylalanine"
    case "UUA" | "UUG" => "Leucine"
    case "UCU" | "UCC" | "UCA" | "UCG" => "Serine"
    case "UAU" | "UAC" => "Tyrosine"
    case "UGU" | "UGC" => "Cysteine"
    case "UGG" => "Tryptophan"
  }
1 Like