Garfieldtech

Technical thoughts, tutorials, and musings

Blog

Advent of Functional PHP: Day 1

Posted:

Today's challenge asks us to interpret a list of numbers. In the first part, our goal is to determine how many elements in the list are larger than their immediate predecessor.

The imperative way would be to toss it in a foreach() loop and track some state along the way. But we want to be functional and avoid "track some state," because the whole point of functional programming is to avoid tracking state, as tracking state is error prone.

When looking at a foreach-style operation that has some state, my first inclination is to look at a reduce operation. A reduce operation walks over a list and performs the same operation (function) on each item, using the output of the previous iteration as an input. That is, each step takes the output of the previous operation and the next element, and produces an output. It's quite elegant.

However, reduction operations only allow you to pass forward one intermediary result. In our case, the input to a given operation is the current element and its predecessor. That can be squeezed into a reduction operation if we try, but it feels awkward to me, so I'm going to allow some for-style loops. As long as they're controlled, that's OK.

Let's think about the steps involved in our problem:

  1. Read the input file in from disk.
  2. Break it up into a series of integers (or numeric strings, which for PHP is close enough).
  3. Determine which of those elements is larger than its predecessor.
  4. Count the number of elements that pass the test in step 3.
  5. Profit!

Advent of Code 2021: Functional PHP

Posted:

I am planning to participate in Advent of Code this year. For those not familiar with it, it's a daily coding challenge that runs through December, until Christmas. Mostly it's just for fun, but some people take it as an opportunity to either push themselves (by solving the puzzles in a language they're unfamiliar with) or to show off some feature of a language they like, which they then blog about.

In my case, I'll be solving puzzles in PHP, of course, but specifically using functional techniques. My goal is to demonstrate how functional programming in PHP is not just viable but creates really nice solutions. At least, I hope it works out that way; I haven't seen any of the challenges yet. :-)

I will be using the just-released PHP 8.1, because it's a really nice release that I am already in love with. I will also be either using or extending a small functional library I've built recently to support my own projects, Crell/fp. Mostly it's built to support pipe operations, by providing a user-space pipe() function (since a syntactic one sadly didn't get accepted to PHP itself) and a series of functions that return closures in order to play nicely with it.

A secondary goal of this endeavor is to help flesh out that utility library more. :-) (It doesn't even have any common monads yet, like Either, which I expect I'll end up making along the way somehow.)

Each day, I will blog my PHP solution to the problem to share with the rest of the community in the hopes that it will be educational. At least, each day until I decide I can't keep up with it. We'll see how long I last...

Let the coding commence!