Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Created April 10, 2014 13:38
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 atifaziz/10383251 to your computer and use it in GitHub Desktop.
Save atifaziz/10383251 to your computer and use it in GitHub Desktop.
State swapping
using System;
using System.Reactive.Disposables;
static partial class Swapper
{
public static Func<T, T> Create<T>(Func<T> getter, Action<T> setter)
{
if (getter == null) throw new ArgumentNullException("getter");
if (setter == null) throw new ArgumentNullException("setter");
return v =>
{
var swapped = getter();
setter(v);
return swapped;
};
}
public static IDisposable Scope<T>(this Func<T, T> swapper, T value)
{
var swapped = swapper(value);
return Disposable.Create(() => swapper(swapped));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment