Skip to content

Instantly share code, notes, and snippets.

@dadhi
Last active April 17, 2019 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadhi/bafd75fd4e7f9d004faa24e53156f3d8 to your computer and use it in GitHub Desktop.
Save dadhi/bafd75fd4e7f9d004faa24e53156f3d8 to your computer and use it in GitHub Desktop.
Wrapping immutable value in a mutable box
using System;
using ImTools;
public class Program
{
public static void Main()
{
var map = ImHashMap<string, string>.Empty;
var map1 = map.AddOrUpdate("a", "42");
Console.WriteLine(map1.GetValueOrDefault("a"));
}
}
public static class Ext
{
public static void Add<K, V>(this Ref<ImHashMap<K, V>> map, K key, V value)
{
map.Swap(m => m.AddOrUpdate(key, value));
}
}
public static class FamiliarApi
{
public static void Example()
{
var map = Ref.Of(ImHashMap<string, string>.Empty);
map.Add("b", "3.14");
Console.WriteLine(map.Value.GetValueOrDefault("b"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment