The concept in this gist works properly, but evaluates eagerly. Also, its type signature is a bit more complicated than necessary, with the self-typing signature trying to preserve the outer sequence type.
I recommend the breakable
package, which supports lazy evaluation, along with some other useful features: