Snippets Collections
#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,
bookmark
dashboard

Mon Mar 30 2020 12:18:27 GMT+0000 (UTC) https://www.30secondsofcode.org/python/s/unfold/

#python #python #iterator #functions

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension