Implement DistinctBy

First part of the fix for #284. This is all the hard work, but leaves the most interesting test
pending because it will require a second sort to be introduced in the compiler. That PR will 
be coming soon.
It should probably be stored in the compiler's `State` monad. Any issue with that?