gpt4 book ai didi

scala - 我们可以在 Scala 中定义一组 DSL 操作,它们可以像在 Linux 中使用管道处理一样并行执行吗?

转载 作者:行者123 更新时间:2023-12-04 17:55:57 25 4
gpt4 key购买 nike

原谅我糟糕的英语,但我会尽力表达我的问题。

假设我要处理一个大文本,其操作是通过一个关键字来过滤内容;将它们更改为小写;然后将它们打印到标准输出上。众所周知,我们可以在 Linux BASH 脚本中使用管道来做到这一点:

cat article.txt | grep "I" | tr "I" "i" > /dev/stdout

哪里 cat article.txt , grep "I" , tr "I" "i" > /dev/stdout并行运行。

在 Scala 中,我们可能会这样做:
//or read from a text file , e.g. article.txt 
val strList = List("I", "am", "a" , "student", ".", "I", "come", "from", "China", ".","I","love","peace")
strList.filter( _ == "I").map(_.toLowerCase).foreach(println)

我的问题是我们如何制作 filter , mapforeach平行线?

谢谢

最佳答案

在 2.9 中,添加了并行集合。要并行化循环,您所要做的就是通过调用 par 来转换它。成员函数。

您的代码如下所示:

val strList = List("I", "am", "a" , "student", ".", "I", "come", "from", "China", ".","I","love","peace")  // or read from a text file , e.g. article.txt 
strList.par.filter( _ == "I").map(_.toLowerCase).foreach(println)

关于scala - 我们可以在 Scala 中定义一组 DSL 操作,它们可以像在 Linux 中使用管道处理一样并行执行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8891732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com