## Blog: Ramón Soto Mathiesen

### Background

As I usually do every Sunday, I skim through Sergeys F# Weekly just to see if there are anything interesting happening in the F# Community.

This week I found Lucas Reis’ blog post really well written, educational and didactic, specially the visualization of final state machine representation.

What seem to tingle a bit my OCD was the implementation of the EventStore:

### Problem by introducing OO data structures into F# (or OCaml)

As Lucas mention, you can just declare a type with () and define it’s members, and then you have a new data structure in F#. As with Lucas EventStore, I will point out the main issue by taking this approach. If we look into MSDN, we can see that ResizeArray is just a type abbreviation for a generic .NET list:

So my example will also made by using the built-in ResizeArray data structure:

We can see that the final reduced sum is a non-deterministic as well as incorrect result:

So why is this happening? Well if you are used to work with the .NET platform, you might as well (if you actually read the documentation on MSDN) have seen the following text on the bottom of almost every Class definition, under the Thread Safety sections:

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

The main point here is that .NET collections are not immutable and therefore don’t fit well with the functional paradigm that F# is mainly built-on, even though it has support for other paradigms as imperative and OO.

### Build your data structures the right way

Is there a way to solve this self inflicted problem? Yes, we can create constrained types in F#, see Scott Wlaschin Gist in the References below for more information, where you can avoid exposing types from a module. They are accessible from inside the module, but not from code importing the module.

With this in mind, I will create an immutable array like this:

where the 'a iarray is visible from outside, while the single-case union constructor T is marked as private | T of 'a array therefore it can only be accessed from inside the module (and sub modules).

As you can see in the sub (and sub sub) modules, I’m just extracting the standard (and mutable) array type from the single-case union constructor and then using the built-in functions to perform the desired logic.

If you look carefully, I’m never exposing the underlying and mutable array, therefore, as I don’t allow any external piece of code to instantiate my type iarray unless it’s by using the init function, I can therefore argue that my data structure is sound to be used as an immutable F# data structure as the native built-in would be used.

Snippets:

Output:

### Functor modules as in OCaml

In my current implementation of iarray my additions to the array are in linear time, as a new array +1 needs to be allocated on another spot in memory, while my indexed access still is in constant time. So in the case that I was using this data structure for a lot of reads but very few inserts, it would be ideal, but what about if I had a lot of inserts but very few reads? Or what if I had more or less fifty/fifty on reads and writes? Well, in the case that I had a lot of writes and few reads, I would have used a standard built in list as the underlying data structure due to constant addition and linear reads while in the case where I had fifty/fifty reads and writes I would probably go for a balanced tree, logarithmic reads and writes. In all these cases, I would actually have to create new and separated modules for each of the approaches I mention.

Therefore it would be really nice if F# could port the Functor modules from OCaml as it would allow us to change the underlying datastructures inside a module.

I’ve POC an approach where I used records as modules, as you can see in the References, but it’s very hackerish and doesn’t really gets the job done …

### Conclusion:

I think it’s a change of the mindset that you need to do when your are coding with functional programming languages that are multi-paradigm, as you will be able to do things the way you are used to do, in an OO way, but that might not always be the appropriate approach.

### References:

• Sergey Tihon’s Blog:
• Lucas Reis’ Blog:
• MSDN:
• Scott Wlaschin on GitHub Gist:
• Blog: Ramón Soto Mathiesen
• Part I - An introduction to OCaml:

### References:

• Verify F* Online (just copy/paste):

### Background

This is the second part on how to delete accounts. In this blog post we will explain why we also exposed CRUD request on our CrmData module.

We also moved the repetitive code (Client SDK and Proxy) to a Helper script file DG.Delegate.HowToDaxif.DataManagement.Helper.fsx

### Delete all accounts

As mentioned above, we just moved a few function to another file and created a prime version of the script we used in the previous blog post DG.Delegate.HowToDaxif.DataManagement.Prime.fsx.

It’s only the second step that we changed:

If we now run the two deletion script for comparison, we can see that they have similar performance, due to the size of data:

Note: There is a limitations on MS CRM Online (only two ExecuteMultiple can be executed at the same time.

Output from evaluating script from part 1:

Output from evaluating script from part 2:

### Bonus

While I was writing this blogpost, we got the following question: “We have a question about timezones. It seems like there is no way to set up a default timezone that will be used when new users are added to CRM. Do you know of a way that we can set up a timezone that gets used for each user without having to edit them individually?

• Post plug-in added on the SystemUser Create event: You can hook up an event that sets the time zone when a new users is added (you will have to find the related UserSettings created by the kernel). Downside is that this approach only work with new created SystemUsers.

• As Microsoft have bought AdxStudio, they are slowly moving all the fancy PowerShell scripts from their ALM Toolkit to Microsoft Xrm Data Powershell library (which is nice). Here is an example on how to update a System Users settings: UpdateCrmUsersSettings.ps1

• Last but not least, you could use Daxif (now it’s open source) and run the following F# script when a user is created and also on some time frequency, to ensure that users are using the time zone that you specify:

(semi-type safe approach which is readable and generic for all MS CRM instances)

### Updated

Based on feedback from Joakim, fellow co-founder of the F#unctional Copenhageners Meetup Group - MF#K, in order to be a functor it must define a map function with the follwoing signature map: (‘a -> ‘b) -> ‘a t -> ‘b t. For more info, see References Defining Functors in Scala.

### References:

• Part I - An introduction to OCaml:
• Computing, Math and Beauty:

### References:

• Implementing Left-Pad in 13 Languages: