Tagged: Utilities

F# utilities in haskell

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

Determining 64bit or 32 bit .NET assemblies

I work on a 64 bit machine but frequently deploy to 32 bit machines. The code I work on though has native hooks so I always need to deploy assembly entry points at 32 bit. This means I am usually paranoid about the build configuration. However, sometimes things slip up and a 64 bit dll gets sent out or an entrypoint is built with ANY CPU set. Usually this is caught on our continuous build server with some cryptic reason for a unit test that should be working is actually failing.

When this happens, what you’ll get is a message like this:

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'Some.dll' or one of its dependencies. An attempt was made to load a program with
 an incorrect format.
   at Test.Program.Run(Args args, Boolean fastStart)
   at Test.ProgramMain(String[] args) in Program.cs:line 36

The first thing I do here is to try and … Read more

Merging two immutable dictionaries in F#

If you ever need to merge two immutable dictionaries (maps) that may share the same key, here is how I did it

let mapMerge group1 group2 appender = 
    group1 |> Seq.fold(fun (acc:Map<'a,'b>) (KeyValue(key, values)) -> 
                      match acc.TryFind key with
                                        | Some items -> Map.add key (appender values items) acc
                                        | None -> Map.add key values acc) group2

// for example, assume map1 and map2 are dictionaries of type
// Map<string, seq<string>>
// then the appender deals with how to merge duplicate keys
let joinMaps = mapMerge map1 map2 Seq.append

It doesn’t matter which group you treat as the source and which group you treat as the seed since you are creating a new dictionary out of the two. By pre-seeding the fold with one of the dictionaries you know that the accumulator will already have some of the values you want. The map will iterate over the second dictionary (used … Read more

A collection of simple AS3 string helpers

This article was originally published at tech.blinemedical.com

We all know that it’s smart to create helper and utility classes when faced with a problem that can be encapsulated, but sometimes we forget that even the smallest of things can be put into a helper. Whenever you find yourself writing something more than once you should think about encapsulating that logic. Even small logical elements should be moved to a separate function. It helps with readability, maintainability, and a separation of logic. It also makes things easier to test. Here are a couple of ActionScript string utilities we use. We have tons of them and I’ll be posting snippets here and there of ones we find useful.

Check if a list is empty.

I use this one everywhere! It seems silly, but this may be the most used helper function in our entire application.

public static function isEmpty(list:IList):Boolean {
    return list 
Read more