Build Lazy Iterator Helpers

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write four generator functions that work on any iterable: arrays, Sets, strings and other generators, even endless ones. map(iterable, fn) yields fn(value) for each value. filter(iterable, predicate) yields the values for which predicate(value) is truthy. take(iterable, n) yields the first n values and then stops. chunk(iterable, size) yields arrays of size values, the last one shorter if the values run out. All four are lazy: they read a value from their input only when asked for their next one, so fn is called only for values that are actually used. take stops as soon as it has yielded n values, without reading another, and take with n of 0 reads nothing.

Your code must pass

  • Maps and filters any iterable
  • Takes the first n values
  • Splits into chunks
  • Works on an endless generator
  • Calls fn only for values that are used