How to process lazily two strings at once?

三世轮回 提交于 2020-01-07 03:56:07

问题


Suppose I need a function to filter out characters of chars from a string str and then take only k first characters from the result:

def cleanTrim(str: String, chars: Set[Char], k: Int): String =
  str.filterNot(chars).take(k)

This implementation is suboptimal since it needlessly scans the whole string. In order to optimize it we can use view or event Stream to scan the input lazily, e.g:

def cleanTrim(str: String, chars: Set[Char], k: Int): String =
  str.view.foldLeft("") { case (r, c) => if (chars.contains(c)) r + c else r }.take(k)

Suppose now that I need to clean and trim lazily two strings. I would like to fold them lazily to process a single character from both of them at once and return both results.

def cleanTrim2(str1: String,
               str2: String,
               chars: Set[Char],
               k: Int): (String, String) = ???

How would you suggest implement it ?


回答1:


I do not see any gain of using laziness. In your second implementation of cleanTrim you still scan whole string, actually you can't check if string contains char without scanning whole string (or stream or view).

UPD: @thwiegan right, I didn't read the question carefully.

UPD2: Ok, my second try, don't know if it is important for you to use fold, but I see the way to make it more clear:

def cleanTrim2(str1: String, str2: String, chars: Set[Char], k: Int): (String, String) = {
        val result1 = str1.iterator.filterNot(chars).take(k).mkString
        val result2 = str2.iterator.filterNot(chars).take(k).mkString
        (result1, result2)
    }


来源:https://stackoverflow.com/questions/44699776/how-to-process-lazily-two-strings-at-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!