A Foolish Manifesto

fREWdiculous!

Friday Refactor

It’s Friday, so a long post is not in order. With that in mind, a simple refactor for your pattern matching skulls and skills:

before:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   my @files = File::Find::Rule->file()->name('*.t')
      ->maxdepth( 1 )->in(
         File::Spec->catdir(
            $self->get_directory, 't'
      ) );
 
   my @total_results;
 
   foreach my $file (@files) {
      push @total_results,
         "<span class='file'>$file</span>";
      push @total_results,
         @{ $self->test( $file ) };
   }
   return join "\n", @total_results;

Do you see what I see? We’re iterating over a list and generating a new list… And then we are just doing a join on that. Enjoy the nice and functional rewrite.

after:

1
2
3
4
5
6
7
8
   return join "\n", map {
      ( "<span class='file'>$_</span>",
         @{ $self->test( $_ ) } );
   } File::Find::Rule->file()->name('*.t')
      ->maxdepth( 1 )->in(
         File::Spec->catdir(
            $self->get_directory, 't'
      ) )
  • 0 Comments
  • Filed under: perl
  • Here’s some sexy code:

    1
    2
    3
    4
    5
    6
    7
    var boolArr = parseInt(localEnabled, 16).
       toPaddedString(16,2).
       split('').map(
          function (v) {
             return v === "1";
          }
       );

    It should be clear what it does from the title. The how is clear from the above. But I will explain how so that I can explain the why for each step.

    So first we start with a string something like “43c9″.

    parseInt(Str, 16) will parse that string into the actual number it represents. That’s not too complicated. So now we have 17353.

    Next we use the toPaddedString given to us by prototype. I originally used toString, but the problem there is that if your result is “0001″ it turns into “1″, which is not ok. So we have toPaddedString which gives us a string of length 16, in base 2. So now we have “0100001111001001″.

    The last part is easy, the split turns it into an array of single characters: ["0","1",0"...], and then map maps each item to something else, in this case a boolean expression. So our function above in the map just gives us an array which is based on some code applied to each item in the first array. So notice that the function is v === “1″; if our value is “1″ we get true, otherwise it’s false.

    Also note: the reason that we use === is because in javascript, much like other scripting languages, 0, “”, and null all evaluate to false; so if something has a chance of being one of those things === actually checks for equivalence. If I were to write this code again I’d use the regular == because we aren’t saying “” == 0 (which is true).

    Hope you found this interesting!

  • 0 Comments
  • Filed under: Uncategorized
  • Join = reduce

    I was driving today and I realized that join is just a form of reduce. Here’s some perl6:

    1
    2
    3
    sub join(Str $string, @array) {
       @array.reduce: { $^a ~ $string ~ $^b }
    }

    It works exactly as expected.

  • 1 Comment
  • Filed under: Uncategorized
  • Perl6 vs Ruby: reduce

    Ruby:

    1
    sum = (1..10).reduce {|x,y| x+y}

    or maybe

    1
    sum = (1..10).reduce {:+}

    Perl6:

    1
    my $sum = [+] 1..10;

    That has got to be some of the sexiest perl syntax ever!

  • 1 Comment
  • Filed under: Uncategorized
  • Ruby style functional programming in Perl!

    So recently I was asking if andand exists in perl (here and here) and someone implemented it! How awesome is that? See it here.

    Anyway, so I looked at the code and figured, “Well heck, if it’s that easy, I should do this for map and join on arrays!”

    It was already done! The autobox::Core module does it already! You have to use more javascript-y syntax instead of regular perl-ish, but I think it makes things more clear anyway.

    Example:

    1
    2
    3
    4
    5
    6
    7
    #!/usr/bin/perl
    use feature ":5.10";
    use autobox::Core;
    my @foo = (1,2,3);

    say join( ',', map { $_ * 2 } @foo );
    say @foo->map(sub { $_ * 2 })->join(',');

    To be perfectly clear, you would probably think of the first one as: we are joining the results of the map that multiplies each item by two and the second one as: multiply each item by two and then join them with a comma.

    Anyway, I am *so* stoked to use this at work.

  • 1 Comment
  • Filed under: Uncategorized