5 people like it.
Like the snippet!
JoinList
A JoinList is a variation on the list type that offers a constant time append operation.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
|
type JoinList<'a> =
| Empty
| Unit of 'a
| Join of JoinList<'a> * JoinList<'a> * int (* the total length of the JoinList *)
with
interface IEnumerable<'a> with
member this.GetEnumerator() =
let enumerable = seq {
match this with
| Empty -> ()
| Unit x -> yield x
| Join(x,y,_) ->
yield! x :> seq<'a>
yield! y :> seq<'a> }
enumerable.GetEnumerator()
member this.GetEnumerator() =
let enumerable = seq {
match this with
| Empty -> ()
| Unit x -> yield x
| Join(x,y,_) ->
yield! x :> seq<'a>
yield! y :> seq<'a> }
enumerable.GetEnumerator() :> IEnumerator
module JoinList =
let empty<'a> : JoinList<'a> = Empty
let isEmpty l = match l with Empty -> true | _ -> false
let length l =
match l with
| Empty -> 0
| Unit _ -> 1
| Join(_,_,l) -> l
let singleton x = Unit x
let ofSeq s = Seq.fold (fun xs x ->
match xs with
| Empty -> Unit x
| Unit _ -> Join(xs, Unit x, 2)
| Join(_,_,l) -> Join(xs, Unit x, l+1)) Empty s
let toSeq (l:JoinList<_>) = l :> seq<_>
let toList (l:JoinList<_>) = List.ofSeq l // NOTE: There is likely a better conversion to the List type.
let toArray (l:JoinList<_>) = Array.ofSeq l // NOTE: There is likely a better conversion to the Array type.
let rec equal left right =
match left with
| Empty -> match right with Empty -> true | _ -> false
| Unit x -> match right with Unit y -> x = y | _ -> false
| Join(x,y,l) ->
match right with
| Join(x',y',l') -> l = l' && equal x x' && equal y y' // TODO: || iterate each and compare the values.
| _ -> false
let cons hd tl =
match tl with
| Empty -> Unit hd
| _ -> Join(Unit hd, tl, length tl + 1)
let append left right =
match left with
| Empty -> right
| _ -> match right with
| Empty -> left
| _ -> Join(left, right, length left + length right)
let rec head l =
match l with
| Unit x -> x
| Join(x,y,_) -> head x
| _ -> failwith "JoinList.head: empty list"
let tail (l:JoinList<'a>) : JoinList<'a> =
let rec step (xs:JoinList<'a>) (acc:JoinList<'a>) : JoinList<'a> =
match xs with
| Empty -> acc
| Unit _ -> acc
| Join(x,y,_) -> step x (append y acc)
if isEmpty l then Empty else step l Empty
type JoinList<'a> with
static member op_Equality(left, right) = JoinList.equal left right
static member op_Nil() = JoinList.empty
static member op_Cons(hd, tl) = JoinList.cons hd tl
static member op_Append(left, right) = JoinList.append left right
|
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
|
[<Test>]
let ``test should verify empty is Empty``() =
empty<_> |> should equal JoinList.Empty
let expected = Join(Unit 0, Join(Unit 1, Join(Unit 2, Join(Unit 3, Unit 4, 2), 3), 4), 5)
[<Test>]
let ``test length should return 5``() =
length expected |> should equal 5
[<Test>]
let ``test ofSeq should create a JoinList from a seq``() =
let test = seq { for i in 0..4 -> i }
JoinList.ofSeq test |> should equal expected
[<Test>]
let ``test ofSeq should create a JoinList from a list``() =
let test = [ for i in 0..4 -> i ]
JoinList.ofSeq test |> should equal expected
[<Test>]
let ``test ofSeq should create a JoinList from an array``() =
let test = [| for i in 0..4 -> i |]
JoinList.ofSeq test |> should equal expected
[<Test>]
let ``test cons should prepend 10 to the front of the original list``() =
cons 10 expected |> should equal (Join(Unit 10, expected, 6))
[<Test>]
let ``test singleton should return a Unit containing the solo value``() =
singleton 1 |> should equal (Unit 1)
[<Test>]
let ``test cons should return a Unit when the tail is Empty``() =
cons 1 JoinList.empty |> should equal (Unit 1)
[<Test>]
let ``test subsequent cons should create a JoinList just as the constructor functions``() =
cons 0 (cons 1 (cons 2 (cons 3 (cons 4 empty)))) |> should equal expected
[<Test>]
let ``test append should join two JoinLists together``() =
append expected expected |> should equal (Join(expected, expected, 10))
[<Test>]
let ``test head should return the first item in the JoinList``() =
head (append expected expected) |> should equal 0
[<Test>]
let ``test tail should return all items except the head``() =
tail (append expected expected) |> should equal (Join(cons 1 (cons 2 (cons 3 (cons 4 empty))), expected, 9))
[<Test>]
let ``test JoinList should respond to Seq functions such as map``() =
let testmap x = x*x
let actual = Seq.map testmap (append expected expected)
let expected = seq { yield 0; yield 1; yield 2; yield 3; yield 4; yield 0; yield 1; yield 2; yield 3; yield 4 } |> Seq.map testmap
Assert.That(actual, Is.EquivalentTo expected)
|
type JoinList<'a> =
| Empty
| Unit of 'a
| Join of JoinList<'a> * JoinList<'a> * int
interface IEnumerable<'a>
static member ( @ ) : left:JoinList<'a0> * right:JoinList<'a0> -> JoinList<'a0>
static member op_Cons : hd:'a0 * tl:JoinList<'a0> -> JoinList<'a0>
static member ( = ) : left:JoinList<'a0> * right:JoinList<'a0> -> bool (requires equality)
static member ( [] ) : unit -> JoinList<'a0>
Full name: FSharp.Collections.JoinList.JoinList<_>
union case JoinList.Empty: JoinList<'a>
union case JoinList.Unit: 'a -> JoinList<'a>
union case JoinList.Join: JoinList<'a> * JoinList<'a> * int -> JoinList<'a>
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
Multiple items
type IEnumerable =
member GetEnumerator : unit -> IEnumerator
Full name: System.Collections.IEnumerable
--------------------
type IEnumerable<'T> =
member GetEnumerator : unit -> IEnumerator<'T>
Full name: System.Collections.Generic.IEnumerable<_>
val this : JoinList<'a>
override JoinList.GetEnumerator : unit -> IEnumerator<'a>
Full name: FSharp.Collections.JoinList.JoinList`1.GetEnumerator
val enumerable : seq<'a>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Core.Operators.seq
--------------------
type seq<'T> = IEnumerable<'T>
Full name: Microsoft.FSharp.Collections.seq<_>
val x : 'a
val x : JoinList<'a>
val y : JoinList<'a>
IEnumerable.GetEnumerator() : IEnumerator<'a>
override JoinList.GetEnumerator : unit -> IEnumerator
Full name: FSharp.Collections.JoinList.JoinList`1.GetEnumerator
Multiple items
type IEnumerator =
member Current : obj
member MoveNext : unit -> bool
member Reset : unit -> unit
Full name: System.Collections.IEnumerator
--------------------
type IEnumerator<'T> =
member Current : 'T
Full name: System.Collections.Generic.IEnumerator<_>
val empty<'a> : JoinList<'a>
Full name: FSharp.Collections.JoinList.JoinList.empty
val isEmpty : l:JoinList<'a> -> bool
Full name: FSharp.Collections.JoinList.JoinList.isEmpty
val l : JoinList<'a>
val length : l:JoinList<'a> -> int
Full name: FSharp.Collections.JoinList.JoinList.length
val l : int
val singleton : x:'a -> JoinList<'a>
Full name: FSharp.Collections.JoinList.JoinList.singleton
val ofSeq : s:seq<'a> -> JoinList<'a>
Full name: FSharp.Collections.JoinList.JoinList.ofSeq
val s : seq<'a>
module Seq
from Microsoft.FSharp.Collections
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State
Full name: Microsoft.FSharp.Collections.Seq.fold
val xs : JoinList<'a>
val toSeq : l:JoinList<'a> -> seq<'a>
Full name: FSharp.Collections.JoinList.JoinList.toSeq
val toList : l:JoinList<'a> -> 'a list
Full name: FSharp.Collections.JoinList.JoinList.toList
Multiple items
type List<'T> =
new : unit -> List<'T> + 2 overloads
member Add : item:'T -> unit
member AddRange : collection:IEnumerable<'T> -> unit
member AsReadOnly : unit -> ReadOnlyCollection<'T>
member BinarySearch : item:'T -> int + 2 overloads
member Capacity : int with get, set
member Clear : unit -> unit
member Contains : item:'T -> bool
member ConvertAll<'TOutput> : converter:Converter<'T, 'TOutput> -> List<'TOutput>
member CopyTo : array:'T[] -> unit + 2 overloads
...
nested type Enumerator
Full name: System.Collections.Generic.List<_>
--------------------
List() : unit
List(capacity: int) : unit
List(collection: IEnumerable<'T>) : unit
val ofSeq : source:seq<'T> -> 'T list
Full name: Microsoft.FSharp.Collections.List.ofSeq
val toArray : l:JoinList<'a> -> 'a []
Full name: FSharp.Collections.JoinList.JoinList.toArray
module Array
from Microsoft.FSharp.Collections
val ofSeq : source:seq<'T> -> 'T []
Full name: Microsoft.FSharp.Collections.Array.ofSeq
val equal : left:JoinList<'a> -> right:JoinList<'a> -> bool (requires equality)
Full name: FSharp.Collections.JoinList.JoinList.equal
val left : JoinList<'a> (requires equality)
val right : JoinList<'a> (requires equality)
val x : 'a (requires equality)
val y : 'a (requires equality)
val x : JoinList<'a> (requires equality)
val y : JoinList<'a> (requires equality)
val x' : JoinList<'a> (requires equality)
val y' : JoinList<'a> (requires equality)
val l' : int
val cons : hd:'a -> tl:JoinList<'a> -> JoinList<'a>
Full name: FSharp.Collections.JoinList.JoinList.cons
val hd : 'a
val tl : JoinList<'a>
val append : left:JoinList<'a> -> right:JoinList<'a> -> JoinList<'a>
Full name: FSharp.Collections.JoinList.JoinList.append
val left : JoinList<'a>
val right : JoinList<'a>
val head : l:JoinList<'a> -> 'a
Full name: FSharp.Collections.JoinList.JoinList.head
val failwith : message:string -> 'T
Full name: Microsoft.FSharp.Core.Operators.failwith
val tail : l:JoinList<'a> -> JoinList<'a>
Full name: FSharp.Collections.JoinList.JoinList.tail
val step : (JoinList<'a> -> JoinList<'a> -> JoinList<'a>)
val acc : JoinList<'a>
Multiple items
module JoinList
from FSharp.Collections.JoinList
--------------------
type JoinList<'a> =
| Empty
| Unit of 'a
| Join of JoinList<'a> * JoinList<'a> * int
interface IEnumerable<'a>
static member ( @ ) : left:JoinList<'a0> * right:JoinList<'a0> -> JoinList<'a0>
static member op_Cons : hd:'a0 * tl:JoinList<'a0> -> JoinList<'a0>
static member ( = ) : left:JoinList<'a0> * right:JoinList<'a0> -> bool (requires equality)
static member ( [] ) : unit -> JoinList<'a0>
Full name: FSharp.Collections.JoinList.JoinList<_>
static member JoinList.op_Cons : hd:'a0 * tl:JoinList<'a0> -> JoinList<'a0>
Full name: FSharp.Collections.JoinList.JoinList`1.op_Cons
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>
Full name: Microsoft.FSharp.Collections.Seq.map
More information