Why does
List.range(0,100).contains(2)
Work, while
List.range(0,100).par.contains(2)
Does not?
This is planned for the future?
Why does
List.range(0,100).contains(2)
Work, while
List.range(0,100).par.contains(2)
Does not?
This is planned for the future?
Copyright © 2021 Jogjafile Inc.
The non-teleological answer is that it's because
containsis defined inSeqLikebut not inParSeqLike.If that doesn't satisfy your curiosity, you can find that
SeqLike'scontainsis defined thus:So for your example you can write
ParSeqLikeis missing a few other methods as well, some of which would be hard to implement efficiently (e.g.indexOfSlice) and some for less obvious reasons (e.g.combinations- maybe because that's only useful on small datasets). But if you have a parallel collection you can also use.seqto get back to the linear version and get your methods back:As for why the library designers left it out... I'm totally guessing, but maybe they wanted to reduce the number of methods for simplicity's sake, and it's nearly as easy to use
exists.This also raises the question, why is
containsdefined onSeqLikerather than on the granddaddy of all collections,GenTraversableOnce, where you findexists? A possible reason is thatcontainsforMapis semantically a different method to that onSetandSeq. AMap[A,B]is aTraversable[(A,B)], so ifcontainswere defined forTraversable,containswould need to take a tuple(A,B)argument; howeverMap'scontainstakes just anAargument. Given this, I thinkcontainsshould be defined inGenSeqLike- maybe this is an oversight that will be corrected.(I thought at first maybe parallel sequences don't have
containsbecause searching where you intend to stop after finding your target on parallel collections is a lot less efficient than the linear version (the various threads do a lot of unnecessary work after the value is found: see this question), but that can't be right becauseexistsis there.)