Incremental Quine

23

​Your task is to write a program or function that outputs the first character of its source code, then the second, then the third... each time it is run.

For example, if your program was foo in language bar in file baz.bar, then you should get output similar to this:

λ bar baz.bar
f
λ bar baz.bar
o
λ bar baz.bar
o

Your program can do anything once its done printing its source code in this fashion. You may modify the source code for the file, but remember that the source code to be printed is the original source code.

This is a code-golf, so the shortest program in bytes wins.

Conor O'Brien

Posted 2016-12-20T01:30:28.020

Reputation: 36 228

1If we can modify the source code for the file, does that mean we can read it too? – FlipTack – 2016-12-21T11:29:10.540

1@Flp.Tkc Good question. I honestly have no idea. I don't want you to construct the quine by reading the source, but I'm okay if you read the source for the modification of the file only. – Conor O'Brien – 2016-12-21T15:44:32.517

Answers

6

Jelly, 12 bytes

“;⁾vṾ®ȯ©Ḣ”vṾ

This is a niladic link. Try it online! (Includes code to call the link twelve times.)

How it works

“;⁾vṾ®ȯ©Ḣ”vṾ  Niladic link.

“;⁾vṾ®ȯ©Ḣ”    Set the left argument and the return value to s =: ';⁾vṾ®ȯ©Ḣ'.
           Ṿ  Uneval; yield r =: '“;⁾vṾ®ȯ©Ḣ”', a string representation of s.
          v   Eval; execute s as a Jelly program with argument r.

 ⁾vV          Yield 'vṾ'.
;             Concatenate r and 'vṾ', yielding q =: '“;⁾vṾ®ȯ©Ḣ”vṾ'.
    ®ȯ        Take the flat logical OR of the register (initially 0) and q.
              This replaces 0 with q in the first run, but it will yield the
              content of the register in subsequent runs.
      ©       Copy the result to the register.
       Ḣ      Head; pop and yield the first character of the register.
              This modifies the string in the register, so it holds one fewer
              character after each call.

As an added bonus, since the register will hold an empty string after the twelveth call, it is once again falsy and the link is ready to start over. Calling the link 24 times will output the source code twice, calling it 36 times thrice, etc.

Dennis

Posted 2016-12-20T01:30:28.020

Reputation: 196 637

I don't know Jelly, so what does the footer do precisely? Why ^17? – Conor O'Brien – 2016-12-20T02:08:16.303

Call the link above (¢), identity function (¹, not really needed after the first call), set return value to linefeed (, implicitly prints the previous return value), repeat. ¹ and are both superscripts, but they're unrelated. I've replaced them with the less confusing ¢;¢;¢;¢;¢;¢;¢;¢;¢;¢;¢;¢ (call, concatenate, repeat). – Dennis – 2016-12-20T02:09:49.657

12

Javascript - 26 bytes

Defines f() that returns the source code character by character.

n=0;f=x=>("n=0;f="+f)[n++]

Returns undefined after it runs out of characters.

n=0;f=x=>("n=0;f="+f)[n++]
for(i=0;i<30;i++){console.log(f())} //test harness

Maltysen

Posted 2016-12-20T01:30:28.020

Reputation: 25 023

If I'm not mistaken, you have to include the function call as part of the code. – Mama Fun Roll – 2016-12-20T03:09:44.020

@MamaFunRoll link to relevant meta post? – Downgoat – 2016-12-20T03:20:44.010

I know of none :P However, I've always thought that the function call itself was part of the quine; maybe there's something I'm missing? – Mama Fun Roll – 2016-12-20T04:05:33.493

@MamaFunRoll The spec specifically allows functions, so a call should not be needed. It wouldn't make much sense for this particylar challenge anyway. – Dennis – 2016-12-20T04:57:46.890

Alrighty, just making sure. – Mama Fun Roll – 2016-12-20T14:19:41.877

2

stacked, noncompeting, 34 bytes

[tostr ':!' + execCounter # out]:!

A variation on the standard quine. This is a full program. This uses execCounter to get how many times this program specifically was run. Errors after outputting everything.

Try it here!

Conor O'Brien

Posted 2016-12-20T01:30:28.020

Reputation: 36 228

2

Pip, 31 bytes

{Y"{Y (yRsRPy++v)}"(yRsRPy++v)}

An anonymous function. Test it on TIO!

Explanation

Start with this standard Pip quine:

Y"Y yRsRPy"yRsRPy
Y"Y yRsRPy"        Yank that string into y
           yRsRPy  In y, replace the space with repr(y)

Wrap this in curly braces to make it a function. Now, instead of returning the whole source, we need to index into it. Using a global variable for the index and incrementing it each time will fulfill the "next character each time it's called" requirement. v is the best candidate because it is preinitialized to -1. Incrementing it the first time gives an index of 0, next time 1, etc.

Pip has cyclical indexing, so once the function prints its last character, it will start over at the beginning.

DLosc

Posted 2016-12-20T01:30:28.020

Reputation: 21 213

1

Python, 90 bytes

An extension on the standard Python quine (golfing tips welcome):

def f(n=0,s='def f(n=0,s=%r):\n while 1:yield(s%%s)[n];n+=1'):
 while 1:yield(s%s)[n];n+=1

This is a python generator function, meaning that you iterate over it, and each iteration provides the next character in the source code. When all the characters have been returned, this crashes with IndexError.

For testing, simply append this script to the end of the program:

source = ''
try:
    # Call generator, add characters to the string
    for char in f():
        source += char
except IndexError:
    # Generator has reached end of source code
    pass

print(source)

Or try it online!

FlipTack

Posted 2016-12-20T01:30:28.020

Reputation: 13 242

1

Microscript II, 40 33 bytes

A code block literal, the language's closest equivalent to a function:

{ss1K+>K>s#<v{o}sl*v!(123v)lKp<o}

After running, it puts itself back into x to make it easier to invoke again.

SuperJedi224

Posted 2016-12-20T01:30:28.020

Reputation: 11 342

1

*><>, 13 21 bytes

" r:2+a1Fi1+:1F1+[ro;

Creates a file named \n to keep track of the index.

This may be able to be golfed more, but nothing immediately jumps out at me ...

Output

$ starfish incrementalquine.sf 
"$ starfish incrementalquine.sf 
 $ starfish incrementalquine.sf 
r$ starfish incrementalquine.sf 
:$ starfish incrementalquine.sf 
2$ starfish incrementalquine.sf 
+$ starfish incrementalquine.sf 
a$ starfish incrementalquine.sf 
1$ starfish incrementalquine.sf 
F$ starfish incrementalquine.sf 
i$ starfish incrementalquine.sf 
1$ starfish incrementalquine.sf 
+$ starfish incrementalquine.sf 
:$ starfish incrementalquine.sf 
1$ starfish incrementalquine.sf 
F$ starfish incrementalquine.sf 
1$ starfish incrementalquine.sf 
+$ starfish incrementalquine.sf 
[$ starfish incrementalquine.sf 
r$ starfish incrementalquine.sf 
o$ starfish incrementalquine.sf 
;$ starfish incrementalquine.sf

Explanation

" r:2+                 build the quine
      a1F              open file named "\n"
         i1+:          read input, increment by 1, duplicate
             1F        save incremented input to file
               1+[     increment input, copy that many values to a new stack
                  ro   output the character at the beginning of the new stack
                    ;  end

Cheating Incremental Quine

a1Fi1+:0go1F;

Explanation

a1F            open file named "\n"
   i1+         read input and add 1 to it
      :0g      push the value at (input,0) to the stack
         o     output the value
          1F   save the incremented input to the file
            ;  exit

redstarcoder

Posted 2016-12-20T01:30:28.020

Reputation: 1 771

haha, I had no idea you could name a file "\n". Though this technically reads the source code, by the g instruction. – Conor O'Brien – 2016-12-21T17:49:05.413

@ConorO'Brien, ah fair enough. hmm ... – redstarcoder – 2016-12-21T17:51:00.153

Does *><> have some sort of stack indexing? Or command repetition? Then, you can use the standard quine framework "your code goes here; for ><>, then access the nth member on the stack – Conor O'Brien – 2016-12-21T18:11:27.223

@ConorO'Brien yeah Teal Pelican showed me this quine variant on my other cheating quine answer :). – redstarcoder – 2016-12-21T18:43:54.747

1@redstarcoder I've been working more on quines (YAY!) and I figured out a nice trick, if you replace r for # you can :1- the # like; #;or[+1F1:+1iF1a-1:" I cannot test this at the moment but I believe it cuts 1 byte from your code. ><> quines can be done like #o<-1:" etc :D – Teal pelican – 2016-12-23T12:01:38.317

@Tealpelican haha that's really cool and thanks again! I'll definitely remember these. I still get 21 bytes though because it seems you need a } :p, otherwise the " is output first! #;or[+1F1:+1iF1a}-1:" it does seem like it can be golfed more still though hmm... – redstarcoder – 2016-12-23T13:38:01.440

1

Mathematica, 91 bytes

Comments very welcome; I'm still learning the ropes about what quines are proper quines.

(If[!NumberQ[q], q = 0]; StringTake[ToString[#0]<>FromCharacterCode[{91, 93}], {++q}]) & []

Defines a function called repeatedly with no arguments. After the 91st call, it throws a big error and returns unevaluated.

There were two issues to overcome: first, I wanted to just use StringTake[ToString[#0]<>"[]"], but ToString[] seems to erase the quotation marks; so I had to replace "[]" by FromCharacterCode[{91, 93}]. Second, Mathematica variables start out uninitialized, so I can't call ++q before q is defined; this is why the initial If[!NumberQ[q], q = 0] is needed.

Irrelevant coda: while looking up NumberQ, I learned that Mathematica has a function called TrueQ ... which, yes, returns True if the argument is True and False if the argument is False! (The utility is that it returns False on all other arguments as well.)

Greg Martin

Posted 2016-12-20T01:30:28.020

Reputation: 13 940

0

Bash (and zsh, ksh), 39-bytes

a=`<$0`;pwd>>0;echo ${a:`wc -l<0`-1:1}

It prints nothing after once the program is printed out.

Ensure 0 does not exists in the current directory and run:

bash iquine.bash

Kaito Udagawa

Posted 2016-12-20T01:30:28.020

Reputation: 1

Welcome to PPCG! You've got a clever idea for incrementing the index. Unfortunately, it looks like this answer works by reading its own source code, which means it's not a valid quine by our standards. If you modify it to use quine techniques other than reading its own source, it'll be a good answer.

– DLosc – 2016-12-25T05:16:16.777