[Question]
  [
You will be given as input an infinite stream of positive integers.
Your task is to write a program which outputs an infinite sequence of lists with two requirements:
* All lists in the output are finite sub-lists of the input stream.
* Every sub-list must be output *eventually* by your program.
Sub-lists are not necessarily contiguous, so `[1,1]` *is* a sub-list of `[0,1,0,1,0,...`, and should be included in your output if your input was `[0,1,0,1,0,...`.
The empty list `[]` is a sub-list of every possible input, so it should always be output at some point.
Aside from this, you can do what you want. You can output them in whatever order suits you, you can output any sub-list any (positive) number of times. It's up to you to figure out what's best.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so answers will be scored in bytes with fewer bytes being the goal.
## IO
Outputting an infinite sequence is done as per the defaults of [the sequence tag wiki](https://codegolf.stackexchange.com/tags/sequence/info).
Input of an infinite sequence is basically the dual of those rules. You may:
* Take the input as a [black-box function](https://codegolf.meta.stackexchange.com/a/13706) which takes \$n\$ and outputs the \$n\$th term
* Take the input as a [black-box function](https://codegolf.meta.stackexchange.com/a/13706) which takes \$n\$ and outputs all terms up to the \$n\$th term
* Take the input as a lazy list, generator or stream.
* Take the input by repeatedly querying STDIN each time receiving the next term of the sequence.
  
[Answer]
# [Haskell](https://www.haskell.org/), ~~31~~ 27 bytes
-4 bytes thanks to Wheat Wizard!
Takes an infinite list as input and returns each finite sublist an infinite number of times.
```
f(a:l)=[]:do x<-f l;[a:x,x]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P00j0SpH0zY61iolX6HCRjdNIcc6OtGqQqci9n9uYmaegq1CbmKBb7xCQVFmXomCikKaQrShnl7sfwA "Haskell – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 1 byte
```
æ
```
[Try it online!](https://tio.run/##yy9OTMpM/f@oY97/w8v@/wcA "05AB1E – Try It Online")
# [05AB1E](https://github.com/Adriandmen/05AB1E) no builtin, 5 bytes
```
[DNÏ,
```
[Try it online!](https://tio.run/##yy9OTMpM/f@oY97/aBe/w/06//8DAA "05AB1E – Try It Online")
Takes the input as an 05AB1E infinite lazy list
```
[     infinite loop
 D    create a copy of the infinite list
 N    iteration index (starting from 0)
 Ï    elements of the list for which the corresponding element of the index is 1 
 ,    print
```
[Answer]
# [Python](https://www.python.org), 43 bytes
```
lambda s,n:[s(b)for b in range(n)if n>>b&1]
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72soLIkIz9vweI025ilpSVpuhY3tXMSc5NSEhWKdfKsoos1kjTT8osUkhQy8xSKEvPSUzXyNDPTFPLs7JLUDGNhWkBK8hBKDA0MDDStCooy80o00jSMFPTi4wvyy-PjdfI0NSFaFiyA0AA)
Outputs the `n`th sublist.
# [Python](https://www.python.org), 55 bytes
```
n=0
while[print([s(b)for b in range(n)if n>>b&1])]:n+=1
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72soLIkIz9vwZpiWyMFvfj4gvzy-PilpSVpuhY3zfNsDbjKMzJzUqMLijLzSjSiizWSNNPyixSSFDLzFIoS89JTNfI0M9MU8uzsktQMYzVjrfK0bQ0h2hdAKSgNAA)
Takes input using a black-box function [predefined under the name](https://codegolf.meta.stackexchange.com/a/13707) `s`, which takes a number and outputs the corresponding 0-indexed value of the sequence. Shown in the ATO link using the powers of 2 `[1, 2, 4, 8, ...`. Outputs infinitely to STDOUT.
Uses the binary representation of each non-negative integer `n` to produce a sub-list; `1`s in the binary representation correspond to indices where the list's value is included in the output.
A bug fix for -12 bytes (!):
By using `n` itself rather than the bit length of `n` to determine how many bits to select, we also get, pretty much for free, the ability to sometimes output sublists which don't include the first item.
Because `n` is always greater than or equal to its bit length (besides for `0`), we will effectively select some `0` bits which form part of its "padding", but which allow us to skip some of the first few elements.
When `n` is `0`, this is the only number with no `1`s in its binary. As a result, by initialising `n` to `0`, we output an empty list at the start, satisfying that requirement at no byte cost.
---
# [Python](https://www.python.org), ~~63~~ 62 bytes
```
def f(s,*e):
 yield e
 for x in f(s,next(s)):yield from{x,e+x}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=Nc5BDoIwEAXQfU8xyxbRBFeKwbugTGESmJK2xBLjSdyw0Tt5G6ngJH_1k__m-e5H3xieptfg9fbwOVeoQUuXJqhyASNhWwEK0MZCAOJfxxi8dErlS62t6e4hxU14rCsX6npjPbjRiTk7h97idbCODLfUkZfHeEpErZZcZBG7NdQiZOsqnzgp9iLC5QLXciZ7S-xlqRbp__cX)
This is a version based on [@ovs' clever Haskell answer](https://codegolf.stackexchange.com/a/241144), but with some changes to make it work in Python where we don't have infinite lists. It might be improvable.
-1 thanks to Command Master
Thanks to loopy walt, this version now outputs every sub-list exactly once, for the same byte count.
[Answer]
# Scala, 89 bytes
```
_.scanLeft(Seq[Int]())(_:+_)flatMap(l=>l.indices.toSet.subsets.map(_.toSeq.sorted map l))
```
[Try it online!](https://scastie.scala-lang.org/N2jvydukSDqBPxhNLz2Byg)
Welp, this turned out much longer than I expected. Time to reconsider this approach. Input is a `LazyList[Int]`. It outputs all the sublists up to the current integer for every integer.
```
_                            //The input (infinite list)
.scanLeft                   //Scan left over the sequence,
  (Seq[Int]())(_:+_)        //building an infinite list of subsequences
flatMap(l=>                 //Map each of those subsequences l to its sublists
 l.indices.toSet.subsets   //Get all subsets of the indices
  .map(
   _.toSeq.sorted          //Sort the indices to ensure sublists are in order
   map l)                  //Map the index to the corresponding element in l
)
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 30 bytes
```
a=>n=>a(n).filter(_=>(n/=2)&1)
```
[Try it online!](https://tio.run/##LYxBCsJADAC/0pMkVLdV8FSz4EtKqLsSWZOSFsHXr3vwMHObefGHt8Vl3U9qj1QzVaaoFBkUQ5ayJ4eZIuhAFzycsWbzDoTGSW7Xpr7HbjHdrKRQ7AkZWnx35@9/UGDE8OYVQI@CFAURGlP9AQ "JavaScript (Node.js) – Try It Online")
Input array as a function which output first n elements. Output n-th element.
[Answer]
# [R](https://www.r-project.org/), ~~73~~ 68 bytes
Or **[R](https://www.r-project.org/)>=4.1, 61 bytes** by replacing the word `function` with a `\`.
*Edit: -5 bytes thanks to [@Giuseppe](https://codegolf.stackexchange.com/users/67312/giuseppe).*
```
function(g)while(T<-T+1)for(i in 1:2^T)show(g(T)[!i%/%2^(T:1-1)%%2])
```
[Try it online!](https://tio.run/?fbclid=IwAR3OZreuoZF254JJXfYmeB1949ewhQQ32LJmGGPom2Ia6XkZdRp2jj3JAgA##PczRCoIwFADQ5/YVN2JwL01i6yESP2NvopFj06FsMBWL6NsXvXg@4KTsoCoguzWYxceAPW2DnyzqqtBnSS4m9OADyFK1muYhbtijpvro@YWrFnUpC0mcq4ZyNz3N@Oji61/uY6CPwauQQombuMPhBO@4Jpj8vMBgk2WMalmGhn2Zw72g/AM "R – Try It Online")
Explanation:
1. Take a black-box function `g` as an argument.
2. Increment `T` in each iteration - how many numbers to read from `g`.
3. In the loop, calculate all the sub-sequences of the current vector (and some more):
	* take all numbers from `1` to `2^T`,
	* convert to binary,
	* use as logical indices, which numbers from `g(T)` to print.
[Answer]
# [Ruby], ~~64 56~~ 48 bytes
```
->g{1.step{|n|n.times{p *g[n].combination(_1)}}}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72kqDSpcsGiNNulpSVpuhY3DXTt0qsN9YpLUguqa_Jq8vRKMnNTi6sLFLTSo_Ni9ZLzc5My8xJLMvPzNOINNWtrayH69hYUZeaVKKRF69rlQbXrpWUWFZdo5GnWxkLULFgAoQE)
* saved 8 Bucks thanks to @ovs
takes a black box function returning first *n* elements.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 21 bytes
```
ＷＳ«Ｅ⌕Ａ⮌⍘Ｌυ²1§υκＤ⎚Ｄ⊞υι
```
[Try it online!](https://tio.run/##VYrLCsIwEEXXna8IXU1AwdYnuKqKUFAo@gWhHZpgjCVNqiB@e0zBjZvDuZdTS2Hrh9AhPKXSxLA0nXdXZ5VpkXP2hqSK7vAsOjwq0xRa44UGsj3hTvT0S09kWifR8wnLeUSapZGFK01DL/QTduOcbyE5@HuHo@w1CYt/V@V7OaYqjk8IM8gghzksYAkrWMMGwnTQXw "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
ＷＳ«
```
Repeatedly input the next integer as a string. (This will actually stop on a blank line, which is what I use to allow the linked example to halt, but for positive integers it will keep prompting for input.)
```
Ｅ⌕Ａ⮌⍘Ｌυ²1§υκ
```
Convert the length of the predefined empty list to binary and find the positions of the `1`s counting the least significant bit as `0`. Output the elements of the predefined empty list with matching positions.
```
Ｄ⎚Ｄ
```
Output the canvas double-spaced. (Charcoal's default output format lists one element per line, so it's not easy to distinguish consecutive lists otherwise, although if you were running it interactively then the `Enter input:` prompt would do the trick.)
```
⊞υι
```
Append the next term of the input stream to the predefined empty list.
[Answer]
# [Pari/GP](http://pari.math.u-bordeaux.fr/), 35 bytes
```
g->i->[g(j)|j<-[0..i],bittest(i,j)]
```
[Try it online!](https://tio.run/##DckxDoAgDAXQ3VM4tgklGFfhIoRBE2k@gxJk9O7oW1/dG0TryLMfKgESolLht2wSnbVI5kDv59MJpnAaSmCPKd@N4J1ZF1Mbrk6ZlP9iHh8 "Pari/GP – Try It Online")
Take the input as a black-box function which takes \$n\$ (0-indexed) and outputs the \$n\$th term.
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 1 byte (non-competing)
```
ṗ
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCLDnuKIniIsIuG5lyIsIiIsIiJd)
Non-competing as this builtin and many others were [recently given infinite list support](https://github.com/Vyxal/Vyxal/issues/586) because of this challenge.
[Answer]
# Haskell + [hgl](https://gitlab.com/WheatWizard/haskell-golfing-library), 2 bytes
```
sS
```
I just finished adding builtins having to do with subsequences to hgl. `sS` gets all the subsequences of the input and works just fine on infinite lists.
It outputs subsequences in order of the index of their last element last element. That is first it outputs the empty list, then it outputs all sequences that end in the 0th element, then all sequences that end in the 1st element, then all sequences that end in the 2nd etc.
# Haskell + [hgl](https://gitlab.com/WheatWizard/haskell-golfing-library), 21 bytes
```
jn<<mM(ap[i,p]<p)<+cg
```
Since it's no fun to just post a built-in here's one that solves the task without `sS` or related functions. First we make `jn<<mM(ap[i,p]<p)`. This takes a *finite* list and outputs all subsequences.
To do this it uses `mM`, (`mapM` or `traverse` if you are a Haskell user) which is a little complicated, but it's equivalent to `sQ<m` (`sequence.map` if your a Haskell user) so I'll explain it like that.
First `m(ap[i,p]<p)` will replace every element with a list of a singleton containing itself and an empty list:
```
>>> m(ap[i,p]<p)[1,2,3]
[[[],[1]],[[],[2]],[[],[3]]]
```
Then `sQ` basically does the generalized cartesian product. It takes n lists as input and gives all the ways to take 1 item from each list. Since we have two possibilities in each list, the integer singleton or any empty list, this gives us basically binary numbers of n digits with 0 being the empty list and 1 being the value.
```
>>> mM(ap[i,p]<p)[1,2,3]
[[[],[],[]],[[],[],[3]],[[],[2],[]],[[],[2],[3]],[[1],[],[]],[[1],[],[3]],[[1],[2],[]],[[1],[2],[3]]]
```
Now we want to get rid of the singletons so we use `m jn`
```
>>> jn<<mM(ap[i,p]<p)$[1,2,3]
[[],[3],[2],[2,3],[1],[1,3],[1,2],[1,2,3]]
```
Now we have all the subsequences of the input! However this stalls out on infinite lists.
Our fix here is to use `cg`. `cg` gives all the *contiguous* subsequences of the input, possibly infinite. So we first run `cg` and then run this finite subsequence on every result.
Unlike the first solution this
## Reflections
Nice to see that we used no 3 byte functions anywhere. Still
* `ap[i,p]<p` is a little long for my liking `(<p)<ap` could probably be it's own function. I think it would have use beyond this challenge.
[Answer]
# [Julia 1.0](http://julialang.org/), ~~45~~ 31 bytes
```
f\n=[f(i) for i=0:n if n&2^i>0]
```
[Try it online!](https://tio.run/##HcpBCoAgEAXQvaf4q9CdtVOwi2RCUAMT8Yso6PYWvfVb702n9qlVMtMgVh1kP6HJR0IFbLqivR@rWDokdIXmD1DCxxCCwec4lddGK1mdWTjXFw "Julia 1.0 – Try It Online")
Port of [pxeger's answer](https://codegolf.stackexchange.com/a/241143). Outputs the n-th sublist
## [Julia 1.0](http://julialang.org/), 45 bytes
```
!(x,a=[[]])=for i=x a=[@show a;vcat.(a,i)]end
```
[Try it online!](https://tio.run/##HcpBCoAgEADAr2w3DYnqGkLX3hAeljLaKBdMy9@bdByYI56EXcq5EkmhnmdjpN7YA@kExeO98ws4PAuGRqAiaaxby@5rgv8BOZiC9RjY383C0YXN8yVaKfMH "Julia 1.0 – Try It Online")
Input is an iterator. Output has a bit of unnecessary stuff but hopefully it's acceptable
[Answer]
# [Pip](https://github.com/dloscutoff/pip) `-p`, 18 bytes
```
lPBlW1P*lAL:_AEqMl
```
Reads values from stdin, outputs sublists to stdout. Each sublist will be printed infinitely many times. [Attempt This Online!](https://ato.pxeger.com/run?1=m724ILNgWbSSboFS7IKlpSVpuhabcgKccsINA7RyHH2s4h1dC31zIBJQ-QW7DbmMuUy5zLksuQwNuQyNuQxNuQzNuQwtuYwMIUoA)
### Explanation
We track every sublist of the inputs-thus-far in `l`. At each iteration, we read a new input, append it to each of the existing sublists, append that list to the list of existing sublists, and output the whole thing.
```
lPBlW1P*lAL:_AEqMl
l                   Variable preset to empty list
 PBl                Push the empty list to l; l is now [[]]
    W1              While 1 (loop forever):
                Ml   Map this function to each sublist in l:
               q      Read a line of stdin
            _AE       and append it to the sublist
        lAL:         Append those results to l in-place
      P*             Print each sublist in l
```
]