Smalltalk Squeak 4.x flavour many bytes!
I could implement one of the recursive form in Integer in 71 char
f:y n:n n=1or:[^(2to:y)inject:self into:[:x :i|self f:x n:n-1]].^self+y
Then reading from a file or FileStream stdin will cost me an arm... Squeak was obviously not designed as a scripting language. So I will spend many bytes to create my own general purpose utilities unrelated to the problem:
Implement this 21 char method in Stream (to skip seaparators)
s self skipSeparators
Implement this 20 char method in Behavior (to read an instance from a Stream)
<s^self readFrom:s s
Then 28 chars in String (to create a file handle)
f^FileDirectory default/self
Then 59 chars in FileDirectory (to create a readStream)
r^FileStream concreteStream readOnlyFileNamed:self fullName
Then 33 chars in BlockClosure (to evaluate it n times)
*n^(1to:n)collect:[:i|self value]
Then 63 chars in Array (evaluate the argument with receiver and arguments taken from the Array)
`s^self first perform:s asSymbol withArguments:self allButFirst
then solve the problem by evaluating this 31 char snippet anywhere to read from file named x
|s|s:='x'f r.[0class<s]*3`#f:n:
Even without counting the utilities, that's 71+31=102 chars already...
Now, since I'm sure to lose the codeGolf, I have a funnier implementation in Integer:
doesNotUnderstand:m
(m selector allSatisfy:[:c|c=$+])or:[^super doesNotUnderstand:m].
self class compile:
m selector,'y y=0or:[^(2to:y)inject:self into:[:x :i|self'
,m selector allButLast,'x]].^'
,(Character digitValue:()asBit)
,(m selector size-2min:1)hex last.
thisContext sender restart
This method will define (compile) a binary messages made of n + if it does not exist (is not understood by the receiver of the message m), and will restart execution at the beginning of sender context. I inserted additionnal carriage return and spaces for readability.
Note that (m selector size-2min:1)hex last
is a shorted form of (m selector size>2)asBit printString
.
If it was not to demonstrate Smalltalk evil superpowers, the last statement could be replaced by shorter and simpler
^m sendTo:self
Now implement 28 chars utility in Character (to repeat it n times in a String)
*n^String new:n withAll:self
Then evaluate this 43 chars expression:
|i s|i:=0class.s:='x'f r.[i<s]*2`($+*(i<s))
We can accelerate with 10 more chars by implementing in Integer:
++y^self*y
and in this case we also have shorter code because we can replace ^',(m selector size-2min:1)hex last
with ^1'
For such a high price, the code work with second integer=0 :)
n=1
? If it'sx+y
orx+1
,1 1 1
should return2
\$\endgroup\$