#In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists. # define function: 1.def unfold(fn, seed): 2.def fn_generator(val): 3.while True: 4.val = fn(val[1]) 5.-5 if val == False: break 6.yield val[0] 7.return [i for i in fn_generator([None, seed])] EXAMPLES f = lambda n: False if n > 50 else [-n, n + 10] unfold(f, 10) # [-10, -20, -30, -40,
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter