Recreating Elixir's Enum.sum

I’m learning Elixir, and today as an exercise, I was challenged to recreate Enum.sum using recursion. Here’s my solution. For those new to the language, Enum.sum iterates over a list and returns the sum. Here’s an example: iex(0)> Enum.sum([1,2,3,4,5]) 15 Here is my implementation: defmodule MyList do @moduledoc """ Defines the list struct and functions. """ @doc """ Add all the items in a list together. """ def sum(list) do sum(list, 0) end defp sum([], count) do count end defp sum([h|t], count) do sum(t, count + h) end end And the execution: ...

June 30, 2016

Server Side Sorting in Ruby

Recently a friend asked me this question about server-side sorting in Rails. Here’s the question and my answer. The question: I’m working on an app that displays movies. The index page simply lists all movies in a table %table#movies in HAML. There is a clickable ‘Movie Title’ header above the list of movies and when clicked, the page should reload but in alphabetical order. Therefore I assume I need to use .order(:title) somewhere in the app. I was able to simply order all movies when the page loads by doing the following in movies_controller.rb: ...

June 28, 2016

Keyword, Match, and Region in VimScript

After a recent talk I gave that included VimScript, an attendee asked a question about the differences between keyword, match, and region following syn in a Vim syntax highlighting file. Here’s my answer. These three short chapters from ‘Learn VimScript the Hard Way’ help explain: Chapter 45 Chapter 46 Chapter 47 Here’s the short version: keyword assigns a variable name directly to a word or multiple words match assigns a variable name to a Vim regex region assigns a variable name to something between a “start” pattern and an “end” pattern For more info, check out the Vim help pages for each: ...

April 20, 2016

From One End to the Other in Ruby

A while back I tackled the following programming challenge: From One End to the Other: Find the smallest possible (positive) integer that ends in a six such that if that six is removed and placed in front of the remaining digits of the number, the resulting number will be four times as large as the original. My solution is below, and here: https://gist.github.com/jwworth/5a5b3647a44b9a782896 I like that this solution is relatively fast, at 0.36 seconds. I like that it doesn’t imply what the answer might be by having a ceiling to the search— the code will run forever if my conditional is wrong. I like that it uses the rotate method, something I didn’t know existed until I tried to solve this problem. ...

March 6, 2016

Vim Buffer Problem

A few weeks back I tackled the following programming challenge from my colleague Josh Branchaud: Vim Buffer I open up a new Vim buffer and type all the numbers 1 to 10,000, separated by spaces. Then, my cat walks on the keyboard and somehow activates a substitution command that replaces all the ‘0’ digits (zeros) with spaces. If I now sum up all the numbers in the buffer, as delineated by spaces, what is the total? ...

March 6, 2016

Building a Markov-Chain MVP in Ruby

Project announcement! Last night I built a Markov Chain-generated novel, titled Ceramic Nation. It’s presented as a robot named Ellis Champlin, who is diligently composing one chapter a day based on a corpus of classic public-domain novels and philosophy texts. Here is the site and source code. This was an exercise in rapidly building an MVP, styling, testing, Dev Opsing, and Markov-chaining. I’m curious what this bot will produce as the days go on, and am hoping for greatness. I plan to refine the logic slowly to produce better and better chapters, and rotate the source documents as I am inspired by new books and ideas. ...

September 11, 2015

The N + 1 Problem

Recently, I was thinking about the N + 1 problem. N + 1 is a performance issue in a web application, where a method call unleashes a torrent of database queries. Take this method, which loads all users: def index @users = User.all end Simple enough, but call some records in the view associated with each user, such as ‘friends’ and ‘reviews’, and Rails builds a SQL query for each user, each user’s friends, and each user’s reviews. This is redundant, and with a big database, unacceptably slow. This is an N + 1 problem. ...

February 22, 2015

Ruby Symbol#to_proc

What does &: mean in Ruby? In this post, I’ll explain. Explore a Rails app, and you might see something like this: def self.names find(:all).collect(&:name) end What is that &:, you might be wondering? It’s shorthand for this: def self.names find(:all).collect { |p| p.name } end Often in Ruby, we are using a method that invokes a given block. The methods map, collect, and select are just three examples. To employ one of these methods, you might write something like this: ...

October 31, 2014

The Case for One Computer Monitor

I have one computer monitor on my desk. Not long ago, this was common, but today, many people have two, three, or four monitors. In this post, I’d like to make the case that most professional programmers can use just one monitor. Disclaimer Hardware is a personal preference, and you can use any configuration you like. Some people may need more than one monitor for physical reasons. Some people may have jobs where secondary or tertiary displays perform a crucial function. I’m sure there are valid exceptions here. ...

March 31, 2014