An attempt to merge sort functionally in C#

In a month I’ll be starting a new job which will be focused on big data using Scala and Apache Spark.
In preparation, I’ve been re-familiarising myself with Scala by implementing various of the most common data structures and algorithms in Scala.

When comparing how C# (my current occupation programming language) programmers and Scala programmers implement the merge sort, I noticed they are usually quite different in approach. This is partly due to C# and Scala implementing their list data type very differently.
So to help me with my understanding of the merge sort, I decided to try and write it in C# in a way that people generally implement it in Scala.

This github link shows what I came up with.
https://github.com/CSGrimey/MiscSnippets/blob/master/Code/Algorithms/MergeSort.cs

I think this has helped my understanding of the logic and implementation of the merge sort, so I may continue doing this with other algorithms. As that forces me to really understand what is going on instead of memorising the code.

Advertisements

Join two tables based on value only present in one

I needed to join two tables where one is a list of orders, and the other is a list of items in the orders.
These items can only be for orders created by a specific customer.

The orders table has a foreign key via the customer ID, but the items do not.

You could get all the items, and then load it into memory and filter based on the order’s customer ID value, but doing this uses up a lot of memory and is very slow. I needed to do this as lazy as possible.

Fortunately in LINQ, you can join based on anonymous objects that you can create using the table values and other values not in the table. And as it’s all done via IQueryables, so the execution is deferred as late as possible.
int customerID = 12345;

return from item in context.items
join order in context.Orders
on new { orderID = item.OrderID, customerID = customerID } equals new { orderID = order.ID, customerID = order.CustomerID }
select item;

So even though there is no customer ID column in the items table, the anonymous object I created has one that is set to the same value as the customerID I’m looking for. So as long as the customer ID value in an order row matches that, I will get the items I want.

Deep property filtering inside ng-repeat

Something that I’ll likely need to remember how to do in future is filtering a list by an element’s property. This can be taken further by filtering by that property’s property and so on.

 <div ng-repeat=”prescription in orderData.prescriptions | filter: {patient: { id: selectedPatient.patient.id}}”>

Just a matter of creating an inline JSON object that reflects the path to the relevant property. Much simpler than expected.