Domanda

I've been using Python a great deal recently and have been wondering if there are any modern statically typed languages with syntactic support for generators like

def gen():
    for i in range(10):
        yield i << 100

for big_num in gen():
    print(big_num)

I thought Scala had them, but it seems this isn't the case.

È stato utile?

Soluzione

Haskell has conduits which are somewhat more general, but certainly able to achieve a behaviour very similar to Python's generators:

import Control.Monad
import Data.Bits
import Data.Conduit
import qualified Data.Conduit.List as CL

gen :: (Monad m) => Source m Integer
gen = forM_ [0..9] $ \i -> 
          yield (i `shiftL` 100)

main = gen $$ CL.mapM_ print
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top