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
contains
is defined inSeqLike
but not inParSeqLike
.If that doesn't satisfy your curiosity, you can find that
SeqLike
'scontains
is defined thus:So for your example you can write
ParSeqLike
is 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.seq
to 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
contains
defined onSeqLike
rather than on the granddaddy of all collections,GenTraversableOnce
, where you findexists
? A possible reason is thatcontains
forMap
is semantically a different method to that onSet
andSeq
. AMap[A,B]
is aTraversable[(A,B)]
, so ifcontains
were defined forTraversable
,contains
would need to take a tuple(A,B)
argument; howeverMap
'scontains
takes just anA
argument. Given this, I thinkcontains
should be defined inGenSeqLike
- maybe this is an oversight that will be corrected.(I thought at first maybe parallel sequences don't have
contains
because 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 becauseexists
is there.)