Node.js,152 137字节(139 - 2)
为了清楚起见,用换行符分隔,而不是字节计数的一部分。
f=_=>require('fs').writeFileSync(__filename,
`f=${f};f()`.replace(/(\d[^,]*),(\d[^\)]*)/,
(m,a,b)=>`${b=+b},${+a+b}`),console.log((0,1)));
f()
说明:
f=_=> // define `f` as function with a single unused argument `_`
require('fs').writeFileSync( // import the standard filesystem module and overwrite file
__filename, // string var containing path of file for current module
`f=${f};f()`.replace( // template string containing source of entire script
/(\d[^,]*),(\d[^\)]*)/, // regexp to match and group the numbers in this script
(m,a,b)=> // replace function with arguments match, group a, group b
`${b=+b},${+a+b}` // template string incrementing fibonacci numbers in place
), // end replace()
console.log( // prints to stdout, `undefined` passed to argument
(0,1) // comma separated group returns value of last expression
) // end console.log()
) // end fs.writeFileSync()
; // end statement defining `f` as arrow function
f() // run function to modify script and print fibonacci number
用法:
// assuming above script is stored in program.js
$ node program
1
$ node program
1
$ node program
2
$ node program
3
$ node program
5
...