Category Archives: Programming

Haskell’s Infinite Lists = Fascinating + Mind-Stretching

I like to expose myself to programming languages rooted in different paradigms so that I can expand my skill set, broaden my thinking and learn new ways to solve problems. Lately, I’ve been exploring the world of pure functional programming by studying Haskell.

My object-oriented mind finds the following fascinating:

naturalNumbers = [1..] -- [1,2,3,4,5,…]

This simple statement defines a list starting with 1 and increasing by 1 each step all the way to infinity. The result is not a range that can be enumerated to infinity (for example, 1..1.0/0 using Ruby’s Range class); it’s an actual list that goes to infinity. Continue reading

Dynamic Unpivot Stored Procedure

Pass in a query and a column name (or a column list) and receive an unpivoted result set back. Specify either the columns to unpivot or the columns to leave unpivoted (i.e. unpivot all but the specified columns). Behind the scenes, this stored proc casts value columns to a common data type of your choosing, eliminating “the type of column ‘x’ conflicts with the type of other columns specified in the UNPIVOT list” errors. It can also be used as a SQL generator, outputting the dynamically-built query’s text instead of executing it. Continue reading

Knowledge, Reality and Reconciling the Difference (Part 1)

Values often need to be tracked as they change over time. Imagine we’re building a payroll system. A foundational requirement would be to store an employee’s pay rate. In all but the most basic of systems, saving this rate as a simple value is insufficient (“Joe’s wage = $20/hour”); rather, a history of the rate over time needs to be maintained (“Joe’s wage = $15/hour as of 10/15/13; Joe’s wage = $20/hour as of 2/1/14”). We do this by giving pay rate a time dimension.

What our system knows about a given moment in time and what really was at that same moment can differ. Suppose Joe’s manager signs paperwork on January 20 giving Joe a pay raise but leaves for vacation before turning in the paperwork. At close of business on January 20, the payroll system knows Joe’s rate to be one amount when, in reality, it’s a different amount. Joe has been given a raise but the system doesn’t yet know about it. Continue reading

Quick Tip: Setting Variables Inside Array & Hash Literals

Did you know that you can assign variables inside Ruby’s array and hash literals?

While the values used inside square bracket array literals are typically hard-coded, provided by a variable, computed from an inline expression or generated by a method, any valid Ruby statement can be used. The same is true for both keys and values used in hash literal key => value pairs. Continue reading

RSpec: be true & be_true Are Different!

RSpec’s be true and be_true look deceptively similar. In fact, their naming suggests that they might be synonyms for the same assertion. Can the two clauses be used interchangeably to produce the same effect? No!

Both check trueness but from different perspectives. be true checks whether the compared-against value is true: “Does the value literally equal true?” be_true asserts that the test value evaluates to true: “Is the value truthy?” be false and be_false are the respective inverse match clauses. Continue reading

Let It Quack!

Duck Crossing Warning SignAn entire day passed. The repugnant lines sat festering. Finally, their odious stench wafted strong enough to give me a start. What had I done?!

The situation started simply enough. I realized several kinds of transactions shared common logic. Despising duplication as a good programmer should, I placed this shared behavior in a base class which the various kinds of transactions would subclass. Continue reading

Perusing Prolog (Part 2)

In interview style, here’s the conclusion of my reflections after studying Prolog for two months. If you’d like, read Part 1 first.

What did you find unique about the language?

Unification! Prolog works by trying to make terms equal one another. In most languages, the statement “Cost = 100” sets the variable “Cost” to a value of 100. In Prolog, “Cost = 100” means (in effect): “can Cost and 100 be unified?” During execution, Prolog takes into account the entire set of logical rules that have been defined to figure out if Cost and 100 are allowed to equal the same value. While possible, the need for statements manipulating variables’ values within an operation occurs far less frequently in Prolog than in the languages I’m used to. Continue reading

Perusing Prolog (Part 1)

Reading my way through Seven Languages in Seven Weeks this past fall, Prolog caught my attention. The language worked so differently than any I’d used. It offered to solve problems challenging to code in the languages I was familiar with. Intrigued, I started setting aside an hour each workday to study Prolog. Learn Prolog Now! served as my primary tutorial. In interview-style, here are my thoughts after two months of exploring Prolog. Continue reading