isaacschemm: A cartoon of myself as a snail (snail8)
Entry tags:

Using F# collections in C# records for immutability and structural equality

When working in .NET, it can be awkward to figure out whether two objects are "equal". With reference types, two variables might refer to the same object on the heap, or to two different objects with identical properties. In the picture below, changing Snail C's shirt would also change Snail B's, because they are the same snail, while the otherwise identical Snail A is unchanged.

A points to one drawing, B and C both point to another        identical drawing; A is not equal to B, but B is equal to C

Value types are more like C structs. So giving Snail C a jacket and hat does not affect Snail B. It wouldn't make sense to do a "reference comparison" on a value type; the objects are always stored in different places (setting aside explicit pointers to objects in memory, of course - something C# also allows for).

A      and B point to separate but identical drawings, and are equal; C      points to a different drawing, where the snail has a hat, and is      not equal to B

Immutable types with built-in structural equality checks are a great way to work around this issue, but there's still the question of what to do for collection types - the standard .NET HashSet, List and Array types aren't immutable, and even ImmutableList and ImmutableSet don't consider two lists or two sets with the same items to be "equal". But there's a very easy way to handle this problem: you can use the collection types in the F# standard library (FSharp.Core), even without using F#.

Read more... )
isaacschemm: A cartoon of myself as a snail (snail8)
Entry tags:

This whole blog post is also a .NET UTF-32 string library

// UTF-32 is a Unicode encoding that uses exactly four bytes for each
// codepoint. Unlike UTF-8 (used widely on the Web) or UTF-16 (used in Java
// and .NET), each codepoint / character takes up the same number of bytes,
// making it much easier to do string processing (counting, substrings) based
// on character count.

// One common application is applying styling or custom behavior to Twitter
// posts based on the data provided by the API:
// https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/entities
Read more... )
isaacschemm: Drawing of myself as a snail (snail)
Entry tags:

My favorite single line of code: sortable ProductVersion type for .NET

Sometimes I need to take a version number - something like 5 or 6.4 or 2.25.1 - and see whether it's newer or older than another version number, or maybe just take a list of version numbers like this and put them in order.

The projects I'm working on usually have at least a bit of F# code, or a dependency on an F# library (even if most of the code is C#), and once I've taken the dependency on FSharp.Core, I might as well use it to create an equatable, sortable, and immutable "version number" type.

This might be absolute favorite line of code:

type ProductVersion = { components: int list }
Read more... )