Thursday, October 29, 2020

Add Line Numbers to a Text File

 You want

line 1

line 2

line 3

..

to become

1. line 1

2. line 2

3. line 3

...

My way : pipe through perl -p -e 's/^/$.. /;'

nl -ba and cat -n both give a large number of spaces before the line number. Don't know how to prevent that..

Tuesday, October 20, 2020

The Best Book Review in the World

https://www.amazon.com/Programming-Kotlin-Expressive-Performant-Applications/dp/1680506358

Reviewed in the United States on October 6, 2020
Pros: The broad organization of the book, in terms of order of chapters, headings and subheadings, is good. The summary at the end of each chapter is concise.
Cons:
1. The body text tends to be repetitious, The code examples are trivial and silly, and sometimes exemplars of lousy code. A poster child for the latter is the functional-programming recipe for testing whether a number is primreame. The author's code tries to divide the number by every number from two up to the number itself. In reality, for a non-trivially large number, you only need to divide by numbers up to the approximate rounded-up square root of the number to be tested - a recipe that Eratosthenes figured out 2 millennia ago.

Another instance of a bad example: he defines his own Complex class using Pairs - of *integers* for the real and imaginary parts. (They should be decimal or floating point numbers.)

2. The author has an unhealthy obsession about the supposed evils of read-write variables (declared with var). Traditional languages have them for a good reason: you can't keep cloning large collections (arrays, lists, maps, etc.) each time you decide to modify a collection by adding or removing an element. Kotlin itself has a family of classes prefixed with the name Mutable (e.g., MutableList) which, it turns out, can be declared with val - they permit modifications to the contents while not allowing replacement of the entire collection with another. (The fact that you get some safety by declaring mutable collections with val is not mentioned in the book: you have to go to the online Kotlin documentation.)

3. Some chapters are superficial placeholders that will take you from beginner to -- beginner. For example, chapter 20 on Kotlin programming on the android platform is a joke.

4. The book gives every impression of being a rush job, with more thought being given to the jokes in the text (which are sometimes decent, but often labored) than to the examples and detailed exploration of this excellent language, to which this book does a disservice.

5. The author repeatedly emphasizes conciseness (you don't have to type semicolons and sprain your right pinky - wow!), but mere conciseness without clarity (which is important for long-term code maintainability) is worthless. The old days where show-off C programmers tried to squeeze in as much semantics into a single statement, combining multiple assignments separated by commas, and increment/decrement operators are over - opaque code gets programmers fired today Kotlin allows a reasonable degree of conciseness, but I expect that, at some point, when enough experience with the language has accumulated, a style guide will be written that identifies the optimum balance between conciseness and clarity.

In summary, the author does a quick survey of Kotlin, but I have to wonder if he's ever written serious, production-quality code for a living.

Friday, October 02, 2020

Get the Length of Each Line : Something unix wc Can't Do

Replace each line with the character count :

perl -p -e 's/^(.+)$/sprintf("%d",length($1))/e;'

Prefix each line with character count :

perl -p -e 's/^(.+)$/sprintf("%d:$1",length($1))/e;'