Having a moment of spare time I decided to go back to
my approach to 99 scala problems. When I started I have used pure scala scripts, so I needed eg. to call them like this:
scala -i p05.scala -i p04.scala -i p03.scala p06.scala
Now I've learnt that it may be easier to go on using
sbt console
However my code was not sbt-friendly, as it caused complaints regarding missing object or class definition. You cannot just define methods like in REPL. So I ended up with urgent neccessity to include my functions into some nice objects. As I am currently playing with
rather small machine full IDE like IntelliJ is not an option. So basically I needed to use my command line to:
add object definition line before content
add closing brace after content
where the latter is trivial append to file, usually
echo "}" >> P01.scala
How to append a line of text at beggining of file?
Enter ed.
Ed is an editor with tradition, used to scarry out younger programmers in team. I have read
a fantastic intro to ed not so long ago. This is the ed script for making the changes with line by line explanation what it is doing (note ed will not get this comment):
1 //goto first line
i //start inserting
package konopski.skala.ninety.nine { //actual text
object PP00 { //more text
. //single dote
//ends writing
$ //goto last line
a //append
} //some
} //text
. //ok, that's enough
w //wq seems familiar
q //have you left any program this way?
After that it took one line in
shell I use to complete the task:
for p in p*.scala; cat edme | sed -e s/PP00/$p -e s/\.scala// | ed $p ; end
Note I have used ed's famous cousin - sed to replace placeholder value PP00 to actual file name without extension. That's it, the massive work is done and I have not used copy/paste or even any vim macro. I must say it was amusing equally to playing with scala, however seems to be even more practical than next way to reverse a list ;>