Scala performance question

后端 未结 7 1230
Happy的楠姐
Happy的楠姐 2021-02-03 11:45

In the article written by Daniel Korzekwa, he said that the performance of following code:

list.map(e => e*2).filter(e => e>10)

is muc

7条回答
  •  孤城傲影
    2021-02-03 11:52

    To avoid traversing the list twice, I think the for syntax is a nice option here:

    val list2 = for(v <- list1; e = v * 2; if e > 10) yield e
    

提交回复
热议问题