A September Challenge

Recently, I saw someone had started a project to write 500 words a day for every day in the month of September. I thought it was a good idea, and definitely somehting more do-able than the relatively crazy NaNoWriMo, which makes the focus of your life for a month to write every day for a month. 500 words can be done. Heck, I write a comic review every week that comes out anywhere between 2000 and 3000 words. Add a couple of blog posts on top of that (in theory) and I could easily hit 500 words a day for a month with very little extra effort. The idea, though, is to do it every day. Challenging, but workable.

Would that be the best expenditure of my energy, though? I started brainstorming other ideas for a month-long project. Here’s what I came up with:

  • A Drawing a Day: I’m doing probably four or five a week right now, particularly with Sketch Dailies. So what if I dedicated myself more to that? What if I made it a point to draw every day, and to make it something I wasn’t afraid to publich?

  • A Blog Post a Day: I missed being a regular blogger, though something always seems to get in the way. I have three blogs. Would I need to concentrate on one? Or could I promise one post a day on any one of those sites, every day for a month? That little bit of variety guarantees it would be easy to do.

  • A comic review a day: I have a backlog of comics I want to read through. This would force me to do that. Problem is, reviewing individual issues of a longer storyline can sometimes be tough. It seems like cheating to review, for example, the latest five part storyline in “Five Weapons,” when I should just review the trade paperback that just came out.

  • A Tech Blog Post a Day: This one is the most beneficial to my productivity, I’d have to think. Writing technical articles would get me to learn a lot more, which is what I strive for on a daily basis already anyway.

  • 30 Days of Elixir: Focusing it even more, what if I wrote something about Elixir every day for 30 days? Small tutorials, code reviews, hints and tips, sharing code of my own, etc.? The language is hitting 1.0 shortly, so this would be an apt celebration.

  • 30 Days of Github Contributions: Forget writing about it. What if I just coded every day? I could light up my Github activity board a bright green with 30 straight days of “git push origin master” commands.

In the end, these are all just tricks I’d play on myself. It’s a way to force myself to focus. Problem is, which ever one I pick will take time away from any of the others. But is that such a bad thing? I’m so scattered sometimes, I need some focus.

I have roughly 24 hours to figure this out. Honestly, I’m leaning towards Elixir right now.

Or not doing anything at all. Just producing something every day for myself is more than enough. It doesn’t always have to be in public. That’s just the element there to shame myself into doing the daily task.

Talking in circles. That’s what I’m doing now…

How To Write Elixir Code: The Advanced Method

Just in case: The following is a work of loving parody. Maybe satire? Maybe just having fun. Ya know I love ya guys and gals, right?

1. State Its Purpose

I want to write a program to generate even numbers.

(For the purposes of this tutorial, I’m choosing a ridiculously simple purpose, one so simple that it’s already solved by the core of the language itself. Just work with me here.)

2. Write a Test with ExUnit

1
assert Number.is_even(0) == true

I looked it up on Wikipedia to be sure this was true. Then I tested it in ExUnit to make sure it passed. Only THEN did I show you this test as if I was so confident it would work because I knew in advance that 0 is considered even.

If you have a problem with that, I invite you to look deep inside yourself and define “truthy” out loud with a straight face.

This is computer programming. We’re paid to make stuff up as we go along with utter confidence that Stack Overflow was right.

3. Write the Code to Pass the Test

defmodule Number do

  def is_even(0), do: true

end

Computer programming has gone so high level that any fifth grader could do this stuff after a day in MineCraft…

4. Repeat 2 and 3 As Often As Possible to Complete What Step 1 Asks for

Bonus points for using guard clauses or recursion. I got one of those two in here.

1
2
3
4
5
6
7
8
9
10
11
12
13
defmodule NumbersTest do
  use ExUnit.Case

  test "Even Numbers Check" do
    assert Number.is_even(0) == true
    assert Number.is_even(1) == false
    assert Number.is_even(2) == true
    assert Number.is_even(3) == false
    assert Number.is_even(4) == true
    assert Number.is_even(9) == false
    assert Number.is_even(200) == true
  end
end

Where’s QuickCheck when I need it? Or is that Concuerror I need? I can never keep those two straight. I do, however, recognize that ReBar wouldn’t help here. Yet.

1
2
3
4
5
6
7
8
9
10
11
defmodule Numbers do

  def is_even(0), do: true

  def is_even(value) when rem(value, 2) > 0 do
    false
  end

  def is_even(_), do: true

end

So far, so normal.

5. Now, Rewrite it, With More Pipes

I picked far too simple an example for this. Trust me, though, if you want to hang with the cool kids, everything is a transformation, and every step of that transformation comes after a pipeline operator. Every program, once it’s boiled down to its most basic structure, is this:

1
2
3
Number
  |> Function
  |> Print

Remember that.

6. Good, Now Take Out all the Parentheses You Can

1
2
3
4
5
6
7
8
9
defmodule Number do

  def is_even 0, do: 1
  def is_even(value) when rem(value, 2) > 0 do
    value+1
  end
  def is_even(_), do: true

end

It’s usually harder to read, but the cool kids will like you more.

7. Use OTP

No matter what your program does, it’ll be sweeter if 100 processes are doing it at the same time, waiting to receive your message and increment a counter somewhere. If you can break up the task to run across multiple servers at the same time, you’ll also be crowned King of FP for the moment.

I changed the program to return the next even number.

1
2
3
4
5
6
7
8
9
10
11
12
13
defmodule Even.Server do

  use GenServer

  def handle_call(:prime_or_not, _from, number) do
    {:reply, Number.is_even(number), Number.is_even(number) }
  end

  def next_even_number(number) do
    Even.Server.handle_call(:prime_or_not, number)
  end

end

8. Post it to Hex.pm

Instructions yet to come. Get it in now, though, while all the good names aren’t already taken.

9. Brag on Twitter that you’ve reinvented the wheel, but it’s functional!

10. Go back to getting paid for making Ruby on Rails sites for corporate intranets.

I saw your fingers twitching there. I know what you want to do. I know what you’re thinking:

1
rails new

11. Convert it to Modern Elixir

That is, scrap your OTP port and convert it to the Phoenix framework.

12. Extra Credit!

Rewrite the program using as many macros as possible….

Now go update your resume with “Elixir Ninja” and consider yourself “Full Stack”!

Simple Elixir Pleasures

You know that part of a blog where there’s x number of comments and your job, as the programmer, is to properly label them with “comment” or “comments”? It’s an annoying little bit of code, usually involving another if/then sequence. You can sorta shortcut it if you want to look cheap by saying “comment(s)” and saving yourself one check.

Bill C, in his Redditex repo, has a better solution, thanks to Elixir’s pattern matching of functions:

1
2
3
defp comment(0), do: ""
defp comment(1), do: "(1 comment)"
defp comment(n), do: "(#{n} comments)"

I love that. It’s so clear and so clean looking to me.

Sigh. I’m turning into one of those one jerks who hates if/then/else.

Elixir Case Statements

I had a stupid simple Elixir question the other day that none of the documentation I looked up had the answer for.

Then I felt dumb and realized I could test the idea myself far faster than it was taking me to look it up in books and DDG it.

That idea is, can a case statement have more than one statement between pattern matches? Every example I’ve ever seen has just one line. But then I came across an Erlang program that I wanted to translate into Elixir that had two lines for each match.

Here, run this:

1
2
3
4
5
IO.puts "CASE WITH ONE LINE:"
case 1 + 1 do
  11 -> IO.puts "Wrong"
  2  -> IO.puts "RIGHT"
end

And you get what you expect:

1
2
CASE WITH ONE LINE:
RIGHT

We knew that would be the case. What happens with multiple lines, though?

1
2
3
4
5
6
7
8
9
10
IO.puts ""
IO.puts "CASE WITH TWO LINES:"

case 1 + 1 do
  2 ->
     IO.puts "Line 1"
     IO.puts "Line 2"
  10 ->
     "WRONG"
end

Yup, it works:

1
2
3
CASE WITH TWO LINES:
Line 1
Line 2

You don’t have to add any extra characters or do/doend statements or curly braces or anything to cover that chunk of code.

So, rest easy. The obvious solution works best here.

Immutable Infrastructure

Chad Fowler nails this one. It’s a year old, but still on point:

Cron jobs spring up in unexpected places, running obscure but critical functions that only one person knows about. Application code is deployed outside of the normal straight-from-source-control process.

The system becomes finicky. It only accepts deploys in a certain manual way. The init scripts no longer work unless you do something special and unexpected.

And, of course the operating system has been patched again and again (in the best case) in line with the standard operating procedures, and the inevitable entropy sets in. Or, worse, it has never been patched and now you’re too afraid of what would happen if you try.

The system becomes a house of cards. You fear any change and you fear replacing it since you don’t know everything about how it works.

I especially like this post on a week where I’m trying to configure a server at work to match current servers we already have and, well, it hits home a little too hard…

Give Erlang Its Due

To run an Erlang program from the command line, or to start the Erlang REPL, you start by typing

> erl

As someone whose spent his career starting programs by typing lines starting with

> perl

…I find this a very easy transition to make.

Ruby at OSCONs Past

As a follow-up to my earlier post, “Ruby MIA at OSCON”:

I went back and found the schedules of OSCONs past. Here’s a brief summary, complete with a link to the Ruby track in the schedule (where appropriate) and the number of talks found there:

So, what did we learn? Ruby made its first official appearance at OSCON in 2002. Rails debuted in 2005. Things peaked in 2009, faded slightly in 2010 and 2011, and all but disappeared in the last two years.

This all started with a conversation I had at OSCON this year with someone who had attended multiple times in past years. He said that Ruby had made a big appearance at the show a few years back (he might have guessed 2010, which is in the right time frame, as you see above), but had all but disappeared now.

I’m just curious as to why. One other possibility, of course: OSCON has a political component to it of open source changing the world through caring and sharing. And while the Ruby community is one that shares heavily and whose tools have helped change the world, it does feel like Rails was the thing that brought Ruby to the fore, and most of the people interested in that (by sheer quantity) were doing it as consultants with building social networks and ecommerce platforms, not world-changers and political thought leaders. As the Rails boom has peaked and numerous other technologies fight for space on the OSCON schedule, Ruby and Rails programmers have moved on to be excited by new technologies, or to just move with their own flock.

Or the OSCON schedulers hate Ruby and are shutting it out.

I doubt that last one, of course, but in the interests of bringing up all the possibilities, there you go.

Looking Forward to Revisiting ElixirConf 2014

There are a few talks from last weekend’s ElixirConf that I’m looking forward to watching again when they make their way to ConFreaks.com in a couple of weeks. Here’s a preview:

Dave Thomas: “Opening Keynote”

elixirconf-08

It was the first talk of the show and the one that sticks with me the most. It’s the one that made me look at programming in a different way. “Parse and Transform” might just be the next Elixir rallying cry after this. Thomas delivers it in an easy package. I didn’t have any problems following him as he went. I just want to follow his logic again.

James Smith: “Writing Command Line Applications with Elixir”

elixirconf-25

Writing command line scripts with Elixir is something I haven’t played with yet, but it’s the kind of thing that might bring Elixir into my office. For that reason, I want to watch his work again and see his code samples to get a better idea of how to do it. Technical talks are great, but I often need to sit down with them a second time with an open vi editor and the pause button to get the most from them.

Greg Vaughn: “Elixir Elevated”

elixirconf-26

Greg gave us an OTP example of an elevator in operation. As OTP is the thing I’ve been working with the most lately in my own Elixir practice, this one strikes a chord with me.

Martin Schurrer: “Otp In Production: The Nitty-Gritty Details Of Game Servers”

elixirconf-94

Finally, Martin hit a sweet spot for me. I’ve been working on a card game with Elixir since I started playing with it. I’ve written tutorials about shuffling a deck of cards. I’m in the middle of one right now with OTP. But Martin has actually done it in real life. He talks mostly about how the OTP processes line up for the web-based card game he’s a programmer on, so he’s killing two birds with one stone for me here.

Ruby MIA at OSCON?

I attended four of the five days of OSCon 2014 last week. I was mildly surprised to see all of one or two Ruby-themed panels.

What happened? Has the Ruby community retreated into itself? Are the five million Ruby-themed conferences enough? Is OSCON considered “Not Necessary” from the Ruby community?

I’m guessing it’s just that there are so many Ruby conferences that none of those speakers bothered to submit a talk for OSCON, but maybe I’m mistaken. Any other theories?