f#

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.

FParsec: Alternative or Choice

We left off with a working solution for parsing either “po/k”, “x4/0”, and “s12” into our choice type of Operation. We can always go back and look at our original solution here. We really want to be able to parse a bunch of operations separated by a comma. Parsing Many When we want to parse a large blob of operations separated by a comma we can use the sepBy function. sepBy : Parser<‘a, ‘u> -> Parser<‘b, ‘u> -> Parser<‘a list, ‘u> We can see by the function signature, it takes two parsers as arguments.

Starting Small With FParsec

I had used regular expression to process the inputs for Advent of Code 2017 Day 16. I thought it would be a good learning example to try FParsec. My goal was to take the sample strings given, and convert them into a choice type (discriminated unions). Sample String We’ll be processing inputs for three different operations. A sample input looks like this “po/k,x4/0,s12”. There are three operations which need to be parsed.

FParsec and Fsi

I like exploring ideas and new libraries using the REPL (Read Evaluate Print Loop). There’s something about being able to get started quickly without having to plan out an entire project. I started looking at parser combinators. It’s been on my “to learn” list for some time now. I started with FParsec for .Net and hit a couple of snags getting things running with F# Interactive. The FParsec documentation has the solutions to these issues but I thought I would put them here so I can remember.

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--- .