Last active
March 8, 2020 23:14
-
-
Save TheFo2sh/07b3def9dfa6023202a517abdbaea504 to your computer and use it in GitHub Desktop.
Lenses
This file contains hidden or 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
public partial class FunctionalObject<T> where T: class, ICloneable, new() | |
{ | |
private readonly T _object; | |
private readonly Queue<Action<T>> _modifiers; | |
public FunctionalObject(T @object) | |
{ | |
_object = @object; | |
_modifiers = new Queue<Action<T>>(); | |
} | |
public FunctionalObject<T> With(Action<T> selector) | |
{ | |
this._modifiers.Enqueue(selector); | |
return this; | |
} | |
public T GetValue() | |
{ | |
var result = _object.Clone() as T; | |
if (!ApplyModifiers(this)) | |
return null; | |
return result; | |
} | |
} |
This file contains hidden or 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
public partial FunctionaObject2 where T: class, ICloneable, new() | |
{ | |
public static Func<FunctionalObject<T>, P> NewGetLens<P>(Func<T, P> func) | |
{ | |
return (o => func.Invoke(o.GetValue())); | |
} | |
public static Func<FunctionalObject<T>, FunctionalObject<T>> NewSetLens(Action<T> func) | |
{ | |
return o => o.With(func); | |
} | |
} |
This file contains hidden or 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
class Model:ICloneable | |
{ | |
public string Item { get; set; } | |
public int Price { get; set; } | |
public object Clone() | |
{ | |
return new Model() | |
{ | |
Price = this.Price, | |
Item = this.Item | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment