Skip to content

Commit

Permalink
Add C# docs for PersistentHashMap - references #2
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Dec 10, 2013
1 parent 6bea1ed commit 34aa6f1
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 17 deletions.
1 change: 1 addition & 0 deletions FSharpx.Collections.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{560133F0-6
ProjectSection(SolutionItems) = preProject
help\index.fsx = help\index.fsx
help\PersistentHashMap.fsx = help\PersistentHashMap.fsx
help\PersistentHashMapC.md = help\PersistentHashMapC.md
help\PersistentVector.fsx = help\PersistentVector.fsx
help\PersistentVectorC.md = help\PersistentVectorC.md
EndProjectSection
Expand Down
10 changes: 10 additions & 0 deletions help/PersistentHashMapC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PersistentHashMap
================

A Map is a collection that maps keys to values. Hash maps require keys that correctly support GetHashCode and Equals. Hash maps provide fast access (log32N hops). Count is O(1).

[lang=csharp,file=csharp/PersistentHashMap.cs,key=create-hashmap]

PersistentHashMaps are immutable and therefor allow to create new version without destruction of the old ones.

[lang=csharp,file=csharp/PersistentHashMap.cs,key=modify-hashmap]
50 changes: 50 additions & 0 deletions help/csharp/PersistentHashMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using FSharpx.Collections;
using System.Runtime.CompilerServices;

namespace CSharp
{
class PersistentHashMapSamples
{
public static void Samples([CallerFilePath] string file = "")
{
// ------------------------------------------------------------
// Creating PersistentVectors
// ------------------------------------------------------------

// [create-hashmap]
// Create an empty PersistentHashMap and add some elements
PersistentHashMap<int,string> map =
PersistentHashMap<int, string>.Empty()
.Add(42,"hello")
.Add(99, "world");

Console.WriteLine(map[42]); // hello
Console.WriteLine(map[99]); // world

// Check no. of elements in the PersistentHashMap
Console.WriteLine(map.Length); // 2

// [/create-hashmap]

// [modify-hashmap]
PersistentHashMap<int, string> map2 =
map
.Add(104, "!")
.Add(42, "hi"); // replace existing value

Console.WriteLine(map2[42]); // hi
Console.WriteLine(map[42]); // hello

Console.WriteLine(map.Length); // 2
Console.WriteLine(map2.Length); // 3

// remove the last element from a PersistentHashMap
PersistentHashMap<int, string> map3 = map2.Remove(104);

Console.WriteLine(map3.Length); // 2

// [/modify-hashmap]
}
}
}
37 changes: 22 additions & 15 deletions help/csharp/PersistentVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,35 @@ public static void Samples([CallerFilePath] string file = "")

// [create-vector]
// Create an empty PersistentVector and add some elements
PersistentVector<string> v =
PersistentVector<string>.Empty()
.Conj("hello")
.Conj("world")
.Conj("!");
PersistentVector<string> vector =
PersistentVector<string>.Empty()
.Conj("hello")
.Conj("world")
.Conj("!");

Console.WriteLine(v[0]); // hello
Console.WriteLine(v[2]); // !
Console.WriteLine(vector[0]); // hello
Console.WriteLine(vector[2]); // !

// Check no. of elements in the PersistentVector
Console.WriteLine(v.Length); // 3
Console.WriteLine(vector.Length); // 3

// [/create-vector]

// [modify-vector]
PersistentVector<string> v2 = v.Conj("!!!").Update(0,"hi");
// [modify-vector]
PersistentVector<string> vector2 = vector.Conj("!!!").Update(0,"hi");

Console.WriteLine(vector2[0]); // hi
Console.WriteLine(vector[0]); // hello
Console.WriteLine(vector2[3]); // !!!

Console.WriteLine(vector.Length); // 3
Console.WriteLine(vector2.Length); // 4

// remove the last element from a PersistentVector
PersistentVector<string> vector3 = vector2.Initial;

Console.WriteLine(v2[0]); // hi
Console.WriteLine(v[0]); // hello
Console.WriteLine(v2[3]); // !!!
Console.WriteLine(vector3.Length); // 3

Console.WriteLine(v.Length); // 3
Console.WriteLine(v2.Length); // 4
// [/modify-vector]
}
}
Expand Down
1 change: 1 addition & 0 deletions help/csharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Program
static void Main(string[] args)
{
PersistentVectorSamples.Samples();
PersistentHashMapSamples.Samples();
}
}
}
1 change: 1 addition & 0 deletions help/csharp/csharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="PersistentHashMap.cs" />
<Compile Include="PersistentVector.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
5 changes: 3 additions & 2 deletions help/templates/template-project.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ <h3 class="muted">{project-name}</h3>
<li class="nav-header">Using from F#</li>
<li><a href="PersistentVector.html">PersistentVector</a></li>
<li><a href="PersistentHashMap.html">PersistentHashMap</a></li>
<li class="nav-header">Using from C#</li>
<li><a href="PersistentVectorC.html">PersistentVector</a></li>
<li class="nav-header">Using from C#</li>
<li><a href="PersistentVectorC.html">PersistentVector</a></li>
<li><a href="PersistentHashMapC.html">PersistentHashMap</a></li>
<li class="nav-header">Documentation</li>
<li><a href="apidocs/index.html">API Reference</a></li>
</ul>
Expand Down
1 change: 1 addition & 0 deletions help/templates/template.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<li class="divider"></li>
<li class="nav-header">Using from C#</li>
<li><a href="@Root/PersistentVectorC.html">PersistentVector</a></li>
<li><a href="@Root/PersistentHashMapC.html">PersistentHashMap</a></li>


<li class="nav-header">Documentation</li>
Expand Down

0 comments on commit 34aa6f1

Please sign in to comment.