functional

Active Pattern to Domain Driven

Active Pattern I gave a talk on Beyond the Basics with F# at Prairie Dev Con Regina in October 2018. I was approached after the talk by an attendee with an excellent question. The person was looking to use Active Patterns to validate email addresses with regular expressions. We decided to use a complete active pattern. (Note: The expression pattern used is not exhaustive. Don’t use in production.) let (|ValidEmail|InvalidEmail|) (email : string) = let m = System.

Zipwith Kata Revisited

I went looking for mapi in C# and found it as an overload of LINQ Select. As I worked away, I got thinking about the Rangify kata we recently. One solution proposed for the Rangify kata used zipWith. I realized we could use mapi in place of the zipWiths for our kata. Kata Recap: pretty print ranges. Given a list of sorted integers, consecutive values should be printed in a range.

Zip With

We were recently having a discussion in the Winnipeg .Net user group Katas channel, someone mentioned having a task to pretty print ranges. Given a list of sorted integers, consecutive values should be printed in a range. An example of [2, 3, 4, 5, 7, 9, 10, 11, 12, 15] would be printed as “2-5,7,9-12,15”. Fold I immediately reached for fold. Initially using a list of lists for my accumulator, I then switched to a list of tuples.

Continuing the Pipeline With Tee

I always seem to end up writing the same adapter functions over and over again when starting a new project. The functions are usually one or two lines and could easily just be used as lambdas throughout the application. I have found that giving these small functions a name can help make things more succinct. A lot of those functions can be found in FSharpx.Extras, specifically in Prelude.fs. tee You’ve written your application and have a nice clean pipeline to define the flow of your operations.

Advent of Code 2017 Day 3 Part 2

Advent of Code 2017 Day 3 part 2 was quite the gruelling exercise. We needed to locate the first memory value larger than the input we are given. The memory locations store an accumulative value (still spiralled) based on its existing neighbours. This is the example given. 147 142 133 122 59 304 5 4 2 57 330 10 1 1 54 351 11 23 25 26 362 747 806---> .

Advent of Code 2017 Day 3 Part 1

Day 3 part 1 was about calculating the Manhattan Distance for a specific memory location to memory location 1. The memory blocks are stored in a spiralling out formation with the first block being in the middle of the grid. The memory blocks would look like this: 17 16 15 14 13 18 5 4 3 12 19 6 1 2 11 20 7 8 9 10 21 22 23--- .