
Coding Intentionally in Bash Grains - ihid
https://exercism.io/blog/coding-intentionally-in-bash-grains
======
cellularmitosis
I think exercism.io is hugely valuable for solving three problems:

\- Where can I find a set of practice problems which will force me to really
engage with learning a new language?

\- Given a specific problem, what does the solution look like in various
langauges?

\- Given a specific language, what do the solutions to a variety of problems
look like?

Unfortunately, exercism's web interface is really only set up to cater to the
first of those.

So I made an couple of indexes to solve for the other two:

[https://gist.github.com/cellularmitosis/ae76bb09973f55418d97...](https://gist.github.com/cellularmitosis/ae76bb09973f55418d97cab3cd0cb86e)

[https://gist.github.com/cellularmitosis/2aec8d88a76771452f35...](https://gist.github.com/cellularmitosis/2aec8d88a76771452f3562f3d8b2e253)

~~~
ihid
Nice work! I'd be interested in how we could improve the UI to cater better to
your third point. If I'm interested in how a language looks I tend to find one
good solution, then look at the poster's profile and browse other of their
solutions in that language. I'd welcome your thoughts :) Would like to open an
issue on
[https://github.com/exercism/exercism](https://github.com/exercism/exercism)
and we can discuss this more widely with the Exercism community?

~~~
cellularmitosis
Great! Actually I had opened an issue a while ago but with a much larger scope
in mind -- to also do some AST analysis and try to clump the solutions by
similarity, so that the UI could present "There are three basic ways to solve
this..."

[https://github.com/exercism/exercism/issues/4333](https://github.com/exercism/exercism/issues/4333)

(that's my work account)

~~~
ihid
Ah awesome. Sorry I dropped the ball on that. I'll reply now. Have you seen
about [https://exercism.io/blog/automated-mentoring-support-
project](https://exercism.io/blog/automated-mentoring-support-project) ?

------
daotoad
The code presented in the article doesn't communicate the problem. It
communicates a clever optimization that will break if the spec changes at all.

This code should be readable enough to give you an idea what is happening and
how to change it.

    
    
      my $board-depth = 8;
      my $board-width = 8;
      
      my $squares = $board-depth * $board-width;
      my @grains-per-square = ( 1 .. $squares ).map({ 2 ** ($_-1) })
      
      say [+] @grains-per-square;
    

Which if you read Perl6 says number of squares, and in each one put 2^n-1
grains, then add them all up.

Hint: `[]` is the reduction metaoperator, it lets you specify an operator to
use when reducing a list. [+] means "add it up".

It finishes basically instantly, and while Perl6 has gotten a lot faster, it
is not a fast language (yet?). So no need to optimize this.

I mean you write the code and then marketing will have a poorly specified
variation like "double only the odd ones".

If you implemented this as `2^64-1` then you are starting from scratch.

For the above the map only needs a tweak:

    
    
      map({ $_ %% 2 ?? $_ :: 2**(n-1) })
    

Or whatever it turns out that marketing meant.

In the article, I didn't see informational intentionality, I saw premature
optimization and obfuscation.

~~~
colordrops
Orthogonal to your content, but perl6 allows "-" in variable names? ugh. I get
that the "$" delineates a variable, but on first read it looks like "board
minus depth".

~~~
aib
Why would anyone name their variable "_-1" ? Oh wait—

~~~
b2gills
`_-1` is an invalid variable name.

The `-` must be followed by an alphabetic character (or `_`) for it to be seen
as part of an identifier.

So `_-1` is the same as `_ - 1`

    
    
        {
          my \_ = 4;
    
          say _-1; # 3
        }
    
        {
          sub _ () { 8 }
    
          say _-1; # 7
        }
    

It may be a bad idea to name a variable or subroutine `_` but that is for you
to decide, not for Perl6 to decide. (It's not your overprotective mother.)

\---

I suppose if you really want to do something completely daft like that, there
is not really anything stopping you:

    
    
        {
          my \_ = 3;
          say _-1; # 2
    
          my \term:<_-1> = my $ = 4;
    
          say _-1; # 4
          _-1 = 53;
          say _-1; # 53
    
          say _ -1; # 2
        }
    

Note that it doesn't just create a variable named `_-1`.

What it does is much more powerful than that. It modifies the parser lexically
to add `_-1` as a term. (Since it is lexical it stops being valid after the
closing `}`)

The `my $` is just so that it has a rewritable container so that it can be
reassigned to `53` later.

This can be useful for constants that wouldn't otherwise be a valid
identifier, and for writing subroutines that are parsed as a bare identifier
like a constant would be.

    
    
        constant term:<> = …
    
        sub term:<foo> () {…}
    
        foo;
    
        foo(); # ERROR: Undeclared routine: foo used at line …
    
        foo 1; # ERROR: Two terms in a row

------
rgoulter
On the one hand, the article's title that "solutions should be written in such
a way that the intent is clear" makes sense for code which needs to be read
and maintained. e.g. Martin Fowler suggests even 1 line of code methods can be
useful if they describe a distinct intent.
[https://martinfowler.com/bliki/FunctionLength.html](https://martinfowler.com/bliki/FunctionLength.html)

But I don't think the point is well made with a contrived whiteboard-interview
style problem.

------
cpursley
I'm basically stuck on the Elixir track waiting for a mentor...

I'd be glad to pay per puzzle or a subscription to get faster feedback.

Please, exercism. Take my money.

~~~
bdcravens
Perhaps connect up with someone on codementor.io to review what you've done?

------
marsrover
I just want to point out that it's really annoying that the "Available
language tracks" on the front page are completely random and change on
reloads. It's already hard enough to figure out where the language I'm curious
about is, even worse when it changes after I click a new page.

Also, clicking "explore languages" at the bottom of the blog does nothing.

~~~
Tushon
FWIW, the logged in view of this is an alpha-sorted list, basically this view
[https://exercism.io/tracks](https://exercism.io/tracks)

I would guess the design choice on the front page change on reloads to try to
surface something potentially interesting to everyone, but I am not affiliated
with them/have no additional insight. I can see how it would be frustrating.

They must have removed the link you are referencing, because I don't see it
now. Clicking it from the front page works (probably because it is an anchor
link).

------
hnruss
Expressing the intent of the code significantly improves maintainability. I
think it's important to realize that the problem of missing intent is often
only noticed upon review of the code.

So in my opinion, the moral of the story isn't simply "design with intent",
it's "get your code reviewed".

~~~
01100011
Often it makes it through review just fine. It's usually not discovered until
someone unfamiliar with the problem or complete set of initial circumstances
is exposed to the code, at which time their main focus is on fixing/enhancing
the code and not changing its readability or documenting intent. The new dev
vomits out some code to meet their deadline and now the code is even more
convoluted than it was to begin with.

In a perfect world, readability would be one criteria during reviews,
unfortunately it is often overlooked.

------
eridius
Since this is using Bash, it might be nice to mention that you can't use
Bash's built-in math evaluation to solve the problem because Bash uses fixed-
width integers and therefore

    
    
      let 2**64==0

------
ken
You can do this in any area. In "Without Limits" [1], Bill Bowerman calls a
team meeting at 7:27, specifically so everyone is sure to show up on time.

[1]:
[https://www.youtube.com/watch?v=yuAtOMGyUx8](https://www.youtube.com/watch?v=yuAtOMGyUx8)

