Skip to content

Instantly share code, notes, and snippets.

View GregRos's full-sized avatar

Greg Ros GregRos

View GitHub Profile
namespace Solid.Implementations
open Solid
open Bitwise
open System
open Extensions
open System.Collections.Generic
open Solid.Implementations.MsdHashing
module TrieMap =
let keyWidth = 5
sdf
//We use an abstract class to define a general linked list.
// We don't use discriminated unions as is frequent because they cannot store any initialization data, which makes them unsuitable for caching purposes.
//The generic type parameters are somewhat byzantine, but I don't know how else to accomplish this.
[<AbstractClass>]
type LinkedListAbstract<'list,'value when 'list :> LinkedListAbstract<'list,'value>>(value : 'value, tail : 'list option) =
member this.Tail = tail
member this.Value = value
abstract member Create : 'value -> 'list option -> 'list
member this.Cons v = this.Create v (Some (this :?> 'list))
// These are basically the only instance members needed for full linked list functionality (unless I'm missing something).
// Learn more about F# at http://fsharp.net
// See the 'F# Tutorial' project for more help.
module SoldiTesting.Main
open System.Numerics
open System.Diagnostics
open System
@GregRos
GregRos / GeneralizedList.fs
Last active December 13, 2015 19:19
Generalized list code
namespace SolidTesting
open System
open System.Collections.Generic
open System.Collections
open System.Runtime.CompilerServices
open System.Runtime
module VariousLists =
//This attribute is added for optimization. It makes matches to the empty union case faster.
//http://stackoverflow.com/questions/11959321/why-is-none-represented-as-null
(* Fun fact: It is possible to define the Cons (::) symbolic union case
used by FSharp's own list data structures for your own discriminated unions. *)
type MyList<'t> =
| Nil
| (::) of 't * MyList<'t>
let x = 100::99::98::Nil
//The compiler will infer this to be of type MyList<int> since I explicitly used the Nil union case of MyList
//And not the [] union case of List.
(* While you can give primitive types aliases, like this:*)
type Count = int
(* There will never be any type checking involved when assigning from int to Count. *)
(* An interesting use of units of measure changes this. If I define: *)
[<Measure>] type quantity
@GregRos
GregRos / blah.cs
Last active December 14, 2015 23:39
var x =
from blah in Enumerable.Range(0, 10000).AsSequential()
where blah % 2 == 1
select blah;
//By default the result of this custom list comprehension is an object called DelayedSequential<T>.
//This object is evaluated and a concrete collection is returned via an implicit conversion.
Sequential<int> s = x;
//Alternatively, we can write
public sealed class CustomRange : IEnumerable<int>,IEnumerator<int>
{
private int cur;
private readonly int max;
public CustomRange(int start, int count)
{
cur = start - 1;
max = start + count;
}
var delayed_list =
from item in Enumerable.Range(0, 1000).DelayedList()
where item%2 == 0
select item*2;
// This is a 'delayed list'.
//Basically it is implicitly capable of producing a real List collection if we choose to force it.
//Before we do, we can just perform a few more LINQ queries...
var delayed_list2 =
from item in delayed_list