Slowly I am getting more familiar with Haskell, but there are some things that really irk me. For example, a lot of the point free functions are right to left, instead of left to right. Coming from an F# background this drives me nuts. I want to see what happens first first not last.
For example, if we wanted to do
(x+2)+3 in f#
let chained = (+) 2 >> (+) 3
Compare to haskell:
chained :: Integer -> Integer
chained = (+3) . (+2)
In haskell, the +2 is given the argument 3, then that value is given to +3. In f# you work left to right, which I think is more readable.
Anyways, this is an easy problem to solve by defining a cusotm infix operator
(>>>) :: (a -> b) -> (b -> c) -> (a -> c)
(>>>) a b = b . a
Now … Read more
Dealing with time is hard, it’s really easy to make a mistake. Whenever I’m faced with a problem that deals with time I tend to spend an inordinate amount of time making sure I’m doing things right.
Today I ran into a situation where I needed to be able to calculate durations and ranges from the current time compared to 24 hour block time. The current time however has the full date, but the 24 hour times are just relative. For example, if the current time is 17:00, and the range is 15:00 to 1:00, then I want to say that the current time is within the range. Also, lets say I have the current time is 17:00 but my range is 1:00 to 5:00. I want to know how far it is from now to the start of the 24 hour range. The ranges though, don’t have date information, … Read more
I’m not a java person. I’ve never used it in production, nor have I spent any real time with it outside of my professional work. However, when a language dawns upon lambdas I am drawn to try out their implementation. I’ve long since despised Java for the reasons of verbosity, lack of real closures or events, type erasure in generics, and an over obsession with anonymous classes, so I’ve shied away from doing anything in it.
Still, at least the Java world is trying. While I’d love to just ignore the fact that Java exists, I can’t. Lots of interesting projects are done in Java, and a lot of world class tools are written in Java, so from a professional standpoint it’d be good for me to know it.
In the past when I looked into how past Java utilities did “functional” it never felt natural to me. People have … Read more
A few weeks ago I was working on a sample application that would simulate a complex state machine. The idea is that there is one control room, and many slave rooms, where each slave room has its own state. The control room can dispatch a state advance or state reverse to any room or collection of rooms, as well as query room states, and other room metadata.
But to do this I need a way to get commands from the control room in order to know what to do. In my application clients were connected via tcp sockets and I wanted commands to be newline seperated. This made it easy to test out via a local telnet (I didn’t need to design any binary protocol).
You can never assume you’ve read what you want off a socket, since you’re only ever guaranteed 1 or more bytes when a … Read more
Continuing my journey in functional programming, I decided to try doing the 99 haskell problems to wean my way into haskell. I’ve found this to be a lot of fun since they give you the answers to each problem and, even though I have functional experience, the haskell way is sometimes very different from what I would have expected.
For example, I discovered Arrows via the following problem:
Run-length encoding of a list. Implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as lists (N E) where N is the number of duplicates of the element E.
* (encode ‘(a a a a b c c a a d e e e e))
((4 A) (1 B) (2 C) (2 A) (1 D)(4 E))
Example in Haskell:
My initial solution I did the way I’d probably write it in F#:
encode … Read more
IDEA had a haskell plugin but it didn’t work and caused exceptions in intellij using intellij 12+. Since my main ide’s wouldn’t work with haskell I took to researching what I could use.
While some people frown on the idea of an IDE, I personally like them. To quote Erik Meijer
I am hooked on autocomplete. When I type xs “DOT” it is a cry for help what to pick, map, filter, flatMap. Don’t know upfront.
Not only that, but I want … Read more
A few weeks ago we had our second DC F# meetup with speaker Phil Trelford where he led a hands on session introducing decision trees. The goal of meetup was to see how good of a predictor we could make of who would live and die on the titanic. Kaggle has an excellent data set that shows age, sex, ticket price, cabin number, class, and a bunch of other useful features describing Titanic passengers.
Phil followed Mathias‘ format and had an excellent .fsx script that walked everyone through it. I think the best predictor that someone made was close to 84%, though it was surprisingly difficult to exceed that in the short period of time that we had to work on it. I’d implemented my own shannon entropy based ID3 decision tree in C# so this wasn’t my first foray into decision tree’s, but the compactness of the tree … Read more
Just wanted to share a couple little functions that I was playing with since it made my code terse and readable. At first I needed a way to fold a function until a predicate. This way I could stop and didn’t have to continue through the whole list. Then I needed to be able to do the same kind of thing but choosing all elements up until a predicate.
First, folding. I wanted to be able to get all the characters up until white space. For example:
let (++) a b = a.ToString() + b.ToString()
let upToSpaces str = foldTill Char.IsWhiteSpace (++) "" str
Which led me to write the following fold function. Granted it’s not lazy evaluated, but for me that was OK.
let foldTill check predicate seed list=
let rec foldTill' acc = function
|  -> acc
| (h::t) -> match check h with
| false … Read more
Bear with me, this is going to be a long post.
For the past several months I’ve been working on a production single page application written with AngularJS and TypeScript, and I wanted to share how myself and my team have architected the application. In case you were wondering, the app was written using typescript 0.8.3 and not 0.9.1 (which is out now with generics).
In general, I was really unhappy with a lot of the AngularJS examples I had found on the internet when researching how to structure the application, since they all looked flimsy and poorly constructed. While the examples found online were easy to read and follow, they clearly wouldn’t work with an actual large application.
I had several goals:
- I didn’t want to have to constantly register directives/filters/controllers/etc with Angular everytime I added something
- I didn’t want to have to update my main index.html page with
… Read more
In the course of working on ParsecClone I needed some code that could take in an arbitrary byte array and convert it to a corresponding bit array. The idea is if I have an array of
[|byte 0xFF;byte 0x01|]
Then I should get
I’ve done plenty of bit slingin’ in my day, and the trick is just to apply a sequence of bit masks to each byte and collect all the bit arrays. In other languages this is always a little bit of a pain, but in F# the solution was amazingly elegant
As with anything F#, I like to start with the data
type Bit =
override this.ToString() =
match this with
| One -> "1"
| Zero -> "0"
Make bit masks
Now lets generate the meat and potatoes of this: a sequence of bit masks
let bitMasks = Seq.unfold (fun bitIndex … Read more