Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List[String] -> Vector[Vector[Char]]

I am trying to convert a list of strings to a vector of char vectors:

import collection.breakOut

def stringsToCharVectors(xs: List[String]) =
    xs.map(stringToCharVector)(breakOut) : Vector[Vector[Char]]

def stringToCharVector(x: String) =
    x.map(a => a)(breakOut) : Vector[Char]

Is there a way to implement stringToCharVector that does not involve mapping with the identity function? Generally, are there shorter/better ways to implement stringsToCharVectors?

like image 389
fredoverflow Avatar asked Jul 28 '26 13:07

fredoverflow


1 Answers

You can pass a String directly to the varargs constructor for Vector:

def stringToCharVector(x: String) = Vector(x: _*)

at which point having a separate method seems kind of silly. breakOut is for optimization; if you just want to convert, you can

Vector(xs.map(x => Vector(x: _*)): _*)

at the relatively modest expense of one extra object per list element. (All the chars will most likely be the memory-intensive part.)

like image 94
Rex Kerr Avatar answered Jul 31 '26 13:07

Rex Kerr