Scala: Run function on only one member of a list -
i'm pretty new scala , trying run function on 1 member of list. given list looks like
val list = list("hi", "bye now", "see ya later")
i return list looks like
val list = list("hi", "bye now", "see", "ya", "later")
i know if run list.map(_.split(" "))
list = ("hi", "bye", "now", "see", "ya", "later"). so, how do using lamdas without doing
list.append(list(2).split(" ")) , list.remove(list(2))?
thanks in advance,
if you're intent on applying function specific entries in list, could:
list.zipwithindex.flatmap { case (entry, idx) if (idx == 2) => entry.split(" ") case (entry, idx) => list(entry) } this generalized little more going along lines of:
// add variance, curry, etc. needed... def flatmaponly[a](list: list[a], offsets: set[int], f: => seq[a]): list[a] = list.zipwithindex.flatmap { case (entry, idx) if (offsets.contains(idx)) => f(entry) case (entry, _) => list(entry) } scala> flatmaponly(list("hi", "bye", "see ya later"), set(1, 2), { str: string => str.split(" ").tolist }) res39: list[string] = list(hi, bye, see, ya, later)
Comments
Post a Comment