Skip to content

Instantly share code, notes, and snippets.

@TheFo2sh
Last active March 8, 2020 23:14
Show Gist options
  • Save TheFo2sh/07b3def9dfa6023202a517abdbaea504 to your computer and use it in GitHub Desktop.
Save TheFo2sh/07b3def9dfa6023202a517abdbaea504 to your computer and use it in GitHub Desktop.
Lenses
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;
}
}
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);
}
}
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