TWC 160: Mystic/Math Balance

In which we pine for The Good Place, while visiting The Bad Place.

(Still editing)

TWC Task #1 - Four Is Magic

This task allowed me to revisit a technique that I was recently considering writing about; using patterns of output of idioms to recognize visualize the decomposition.

Six is three, three is five, five is four, four is magic.

  • Comma-separated ==> build a list, then join() on comma.
  • All elements are "Foo is Bar" ==> map with interpolation or map with join, with input of pairs or two-element lists.
  • First element of each list is the second element of prior list ==> rotor(), or Z with an initial .skip.
  • Each element the list is determined by the prior element ==> Seq via ...

Raku

Raku one-liner

Perl

Perl one-liner

YaBasic

At the bottom of the code, I detail some quirks in the language. For a BASIC implementation, YaBasic works fine; I may even use it for an early introduction to programming with my grandchildren. "If it was good enough for me,...!"

For my own use, I never expect to inflict any variant of BASIC on myself again.

Things I really missed in YaBasic:

  • Returning arrays from sub (complex returns)
  • Dynamic arrays
  • Interpolation in strings
  • TAP test harness
  • Simple array equality with eqv
  • Array initialization and array assignment
  • Initializing while declaring a local variable
  • Block-scoping of variables
  • Zero-based array indexes
  • Having a scalar type that can handle strings and numbers.
  • Having a array type that can handle strings and numbers.
  • Code as a parameter, and map()
  • Shrinkable arrays. (Can never reduce the size on an array?, Oh wait, you can. But only via split???)
  • Empty arrays. You cannot have an initial zero-element array expanded to non-zero, which limits the usefulness of fake-push.
  • Postfix if; even though YaBasic has a one-line form, it give no early indication that you are using it, so your eyes keep reading to end-of-line, and hopefully you see that then is missing.

You can tell I missed these things, from all the code I wrote to emulate them.

TWC Task #2 - Equilibrium Index

I am the author of one of the three Raku solutions to this problem, and had studied the other two extensively (12 years ago).

To make up for it, I tried to do some clever things by updating the FP solution with :k and R+ into a one-liner, and by solving the broader problem of possible equilibrium at half-way points and finding smallest imbalances.

Raku

Raku one-liner

Raku overkill

Perl

Perl one-liner

YaBasic

It's a Human insult. It's devastating. You're devastated right now. -- audio Michael, "The Good Place"

Leave a comment

About Bruce Gray

user-pic "Util" on IRC and PerlMonks. Frequent speaker on Perl and Raku, but infrequent blogger.