Created
April 10, 2014 13:38
-
-
Save atifaziz/10383251 to your computer and use it in GitHub Desktop.
State swapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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