Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 0.54 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. def toNGrams1(lst: List[String], n: Int): List[List[String]] = {
  2.     List.range(0, lst.length - n).map(i => lst.slice(i, i+n))
  3.   }
  4.  
  5.   def toNGrams2(lst: List[String], n: Int): List[List[String]] = {
  6.     @tailrec
  7.     def toNGramAcc(acc: List[List[String]], lst: List[String]): List[List[String]] = {
  8.       if (lst.length <= n) acc
  9.       else toNGramAcc(lst.take(n) :: acc, lst.drop(1))
  10.     }
  11.     toNGramAcc(Nil, lst)
  12.   }
  13.  
  14.   def toNGrams3(lst: List[String], n: Int) = {
  15.     for (i <- List.range(0, lst.length - n) ) yield lst.slice(i, i+5)
  16.   }