3 ms·
The equivalent in Python would be: T = TypeVar("T") def odd_numbers(a: list[T]) -> Iterator[T]: # implementation I don't really find that less verbo
by xigoi 6y ago
The equivalent in Python would be:
T = TypeVar("T")
def odd_numbers(a: list[T]) -> Iterator[T]:
# implementation
I don't really find that less verbose, in terms of brackets and symbols…
- adenozine 6y agoYeah, I guess you're right.
- memco 6y agoDoes Nim have comprehensions? Even if you're striving for equivalent signatures the inner body of the function can be expressed as a comprehension in Python: T = TypeVar("T") def odd_numbers(a: Iterable[T]) -> Iterator[T]: yield from (n for n in a if n % 2 == 1)
- michaelsbradley 6y agoSee collect: https://nim-lang.org/docs/sugar.html#collect.m%2Cuntyped%2Cuntyped https://nim-lang.org/docs/sugar.html#collect.m%2Cuntyped%2Cu...
- xigoi 6y agoUnfortunately, Nim doesn't have `yield from`, which would make a comprehension useless here. That's one of the things I'm missing in the language.
- planetis 5y agoThat's an obscure/not optimal way to write this code though. I mean why would you use LIST comprehension if you are going to yield each element.
- nerdponx 5y agoIt's not list comprehension, it's generator comprehension, which is lazily computed. However it's still an unnecessary layer of iteration, when you could just return the comprehension instead.
- nerdponx 5y agoI think the "full" version is easier to read: T = TypeVar("T") def odd_numbers(nums: Iterable[T]) -> Iterator[T]: for n in nums: if n % 2 == 1: yield n Although yet another option is to just return the generator instead of immediately yielding from it: T = TypeVar("T") def odd_numbers(nums: Iterable[T]) -> Iterator[T]: return (n for n in a if n % 2 == 1)