[Tutor] this group and one liners

avi.e.gross at gmail.com avi.e.gross at gmail.com
Tue Jul 5 22:31:11 EDT 2022


Peter mentioned the cult of the one-liner and I admit I am sometimes one of the worshippers.

 

But for teaching a relatively beginner computer class, which in theory includes the purpose of this group to provide hints and some help to students, or help them when they get stuck even on a more advanced project, the goal often is to do things in a way that makes an algorithm clear, often using lots of shorter lines of code.

 

Few one-liners of any complexity are trivial to understand and often consist of highly nested code. 

 

As an example, lots of problems could look like f(g(h(arg, arg, arg, i(j(arg), arg)))) and I may not have matched my parenthese well. Throw in some pythonic constructs like [z^2 for z in …] and it may take a while for a newcomer even to parse it, let alone debug it.

 

And worse, much such code almost has to be read from inside to outside.

 

What is wrong with code like:

 

Var1 = p(…)

Var2 = q(Var1, …)

…

 

I mean sure, there are lots of extra variables sitting around, maybe some if statement with an else and maybe some visible loops. But they show how the problem is being approached and perhaps allow some strategic debugging or testing with print statements and the like.

 

An interesting variation I appreciate in languages with some form of pipeline is to write the algorithm forwards rather than nested. Again, this is easier for some to comprehend. I mean using pseudcode, something like:

 

Do_this(args) piped to

Do_that(above, args) piped to

Do_some_more(above, args) placed in

Result

 

I do this all the time in R with code like:

 

data.frame |>

  select(which columns to keep) |>

  filter(rows with some condition) |>

  mutate(make some new columns with calculations from existing columns) |>

  group_by(one or more columns) |>

  summarize(apply various calculations by group generating various additional columns in a report) |>

  arrange(sort in various ways) |>

  ggplot(make a graph) + … + … ->

  variable

 

The above can be a set of changes one at a time that build on each other and saves the result in a variable, (or just prints it immediately) and you can build this in stages and run just up to some point and test the results and of course can intervene in many other ways. Functionally it is almost the same as using temporary variable and it can implement an algorithm in bite-sized pieces. The ggplot line is  tad complex as it was created ages ago and has it’s own pipeline of sorts as “adding” another command lets you refine your graph and add layers to it by effectively passing a growing data structure around to be changed.

 

The point I am making is whether when some of us do one-liners, are we really helping or just enjoying solving our puzzles?

 

Of course, if someone asks for a clever or obfuscated method, we can happily oblige. 😉

 

 

   

  

 



More information about the Tutor mailing list