Skip to main content

Posts

Sasa.Weak<T> - Typed Weak References

This is the ninth post in my ongoing series covering the abstractions in Sasa. Previous posts: Sasa.Parsing - type-safe, extensible lexing and parsing framework Sasa.Dynamics - type-safe polytypic/reflective programming Sasa.Func - Type-Safe Delegate Combinators Sasa.Option - Handling Optional Values Sasa.Result - Handling Exceptional Values Sasa.Numbers - Generic Number Extensions Sasa.Strings - General String Extensions Sasa.Types - Runtime Types And CLR Metadata System.WeakReference is a special runtime class used for collection and finalization of resources that have no other live references. The only problem is that the encapsulated value is of type "object", and so using it requires a lot more casting than it should. More often than not, a WeakReference will only encapsulate a value of one type, so this casting is often superfluous. Enter Sasa.Weak<T> , which is a struct that wraps WeakReference and provides a typed interface to encapsulated values. ...

Tabular v1.0 - Import/Export Tabular Data to Excel and CSV

Awhile ago, I wrote a small library for describing, importing, and exporting tabular data to and from Excel XML, and CSV formats. I just never got around to releasing it, but I'm making a more concerted effort recently to push releases forward. The full documentation is online, and the binaries here . The license is LGPL. Describing Tabular Data Tabular.dll is an assembly via which you can declaratively describe tabular data: var table1 = new Table { Name = "SomeTable", Rows = { new Row { 1, "foo", 5.7 }, new Row { 2, "bar", 99.99M }, new Row { 3, "baz", 0.0 }, } }; There are really only three classes of interest, Table , Row , and Cell . A table consists of a series of rows, a row consists of a series of cells, and each cell consists of a string value together with an alleged data type describing the string contents. The DataType enumeration is the list of recognized data strings. Cell prov...

Sasa.Types - Runtime Types And CLR Metadata

This is the eighth post in my ongoing series covering the abstractions in Sasa. Previous posts: Sasa.Parsing - type-safe, extensible lexing and parsing framework Sasa.Dynamics - type-safe polytypic/reflective programming Sasa.Func - Type-Safe Delegate Combinators Sasa.Option - Handling Optional Values Sasa.Result - Handling Exceptional Values Sasa.Numbers - Generic Number Extensions Sasa.Strings - General String Extensions Sasa.Types is a static class containing a number of extension methods on System.Type , together with extensions that mirror some of the CLR metadata instructions which aren't typically available in C#. It's available in the core Sasa.dll. Sasa.Types.Constructor Sasa.Types.Constructor accepts a lambda expression with a "new" expression designating a type's constructor. It then extracts and returns the ConstructorInfo used that expression: struct Foo { public Foo(int i) { ... } } ... var x = Types.Constructor(() =...

C# for Haskell and ML Programmers

An interesting question was posed to /r/Haskell today: is there a quick intro to C# from Haskell programmers? Well there are plenty of explanations of C#'s basic syntax, classes, structs, etc., but nothing that specifically addresses the functional mindset a Haskell programmer would be starting with, so I wrote a reply providing links to the various familiar concepts from functional programming found in C#, and described various caveats that might be surprising to a Haskell user. I'll reproduce the post here for posterity: I think there's a reasonable C# subset for functional programming, so if you stick to that you should be able to pick it up relatively quickly. Read up on: LINQ -- you can use the query comprehension syntax, or the regular first-class function syntax. The former is sugar for the latter. Tuples Lambdas and delegates (delegates are first-class functions) Parametric polymorphism is known as generics . You can place generic parameters on methods...

Sasa.Strings - General String Extensions

This is the seventh post in my ongoing series covering the abstractions in Sasa. Previous posts: Sasa.Parsing - type-safe, extensible lexing and parsing framework Sasa.Dynamics - type-safe polytypic/reflective programming Sasa.Func - Type-Safe Delegate Combinators Sasa.Option - Handling Optional Values Sasa.Result - Handling Exceptional Values Sasa.Numbers - Generic Number Extensions The Sasa.Strings static class provides a number of useful extension methods on System.String, including slicing, simple tokenizing, conversions and formatting. It is available in the core Sasa.dll. Sasa.Strings.Format Sasa.Strings.Format are extension methods that wrap System.String.Format : Console.WriteLine("i = {0}".Format(1234567)); string invariant = "{0:#,##0.00}".Format(CultureInfo.InvariantCulture, 10000); Console.WriteLine(invariant); string de = "{0:#,##0.00}".Format(CultureInfo.GetCultureInfo("de-de...

Sasa.Numbers - Generic Number Extensions

This is the sixth post in my ongoing series covering the abstractions in Sasa. Previous posts: Sasa.Parsing - type-safe, extensible lexing and parsing framework Sasa.Dynamics - type-safe polytypic/reflective programming Sasa.Func - Type-Safe Delegate Combinators Sasa.Option - Handling Optional Values Sasa.Result - Handling Exceptional Values The Sasa.Numbers static class provides a number of useful extensions methods on all of the CLR's number types. It is available in the core Sasa.dll. Sasa.Numbers.UpTo Generating a sequence of numbers is a pretty common operation in programming, so there's a special overload extension called Sasa.Numbers.UpTo : // iseq = 1, 2, 3 IEnumerable<int> iseq = 1.UpTo(end: 4); // iseq2 = 1, 3, 5, 7 IEnumerable<int> iseq2 = 1.UpTo(end: 8, step: 2); // dseq = 5.0, 5.5, 6.0, 6.5, 7.0 IEnumerable<double> dseq = 5.0.UpTo(end: 7.5, step: 0.5); The simplest overload simply takes the inclusive lower and exclusive upper bounds...

Sasa.Result - Handling Exceptional Values

This is the fifth post in my ongoing series covering the abstractions in Sasa. Previous posts: Sasa.Parsing - type-safe, extensible lexing and parsing framework Sasa.Dynamics - type-safe polytypic/reflective programming Sasa.Func - Type-Safe Delegate Combinators Sasa.Option - Handling Optional Values Sasa.Result&ltT> encapsulating the results of a computation, either it's return value, or the error it generated, if any. For instance, you could spawn a thread to perform some calculation and deposit the result into an instance of Result<T>. Sasa.Result<T> is available in the core Sasa.dll. Result<T> implements all the usual equality tests on itself, and on T, so you can perform direct comparisons to values. Sasa.Result&ltT>.Value The Sasa.Result<T>.Value property allows clients to obtain the value that was returned from the computation. If an error was instead generated, then this throws InvalidOperationException with the InnerExce...