3 ms·
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
by daotoad 8y ago
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.
- reallydude 8y agoSimilarly, in your code, theres nothing about rows or columns inherent to the problem. There is a reference concept of a (standard) chess board that is really there to be 64 tiles in a linear set. I wouldnt add things that arent necessary or prematurely optimize for nonexistent future requests.
- colordrops 8y agoOrthogonal 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 8y agoWhy would anyone name their variable "_-1" ? Oh wait—
- b2gills 8y ago`_-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
- daotoad 8y agoKebab (or train) case (foo-bar) is actually really nice to type and easy enough to read once you get used to it. It's nicer than using underscores because you don't have to keep chording the shift and minus keys. As to mixing variable names up with subtraction, you put spaces in your math formulas? someVar-anotherVar*thirdVar is pretty unpleasant to read, so not being able to do that is not much of a problem. Perl6's relationship to sigils (like $) is a bit weird at first, but is very consistent and "fits lightly under your hands" in practice. Suffice to say, sigils indicate context and constrain the type of data you can put in a container. If you want to refer directly to a value, you use a sigil-less variable: my \the-great-answer = 42; See the docs on varibles if you want more information: https://docs.perl6.org/language/variables https://docs.perl6.org/language/variables
- lizmat 8y agomap({ $_ %% 2 ?? $_ !! 2**(n-1) }) Ternaries in Perl 6 are done with ?? and !!, not with ?? and ::
- daotoad 8y agoMy fingers hate that and I always, always get it wrong. Always. Fortunately the error messages are very good.