So my question is, are these two implementations equivalent ?
Your two implementations are not technically equivalent, since they both return different kinds of iterator objects, but they are functionality equivalent, since the resulting objects they return will behave the same as an itertools.compress
object.
yield from
and return
are two different syntactic constructs and thus technically have two different semantic meanings:
yield from <iter>
is equivalent to for element in <iter>: yield element
, which means when you call your function, it will return a generator, so each subsequent next
call on the generator will yield
another element from <iter>
.
return <expr>
on the other hand, will simply return the <expr>
object, unchanged, and the function execution will end at the return statement.
In your case, both are actually functionally equivalent to itertools.compress
, since in the first case a generator object is returned, which is functionally equivalent to an itertools.compress
object, and in the second case a map
iterator is returned, which is also functionally equivalent to an itertools.compress
object.
So either option, purely functionally speaking would work as custom implementations of itertools.compress
. In terms of clarity and conciseness, I would prefer the second version, as the yield from
is superfluous, and you get the same functionality just by returning the map
iterator itself.