示例#1
0
 static MaybeLens()
 {
     ExpectSome = new Lens <IMaybe <T>, T>(
         maybe => maybe.OrElseThrow(
             () => new InvalidOperationException("Some expected")),
         value => maybe => value.ToSome());
 }
 ByKey <TKey, TValue>(TKey key)
 {
     return
         (Lens.Create <IImmutableDictionary <TKey, TValue>, TValue>(
              dictionary => dictionary[key],
              value => dictionary => dictionary.SetItem(key, value)));
 }
示例#3
0
 public static TType Set <TType, TValue>(
     this TType instance,
     Lens <TType, TValue> lens,
     TValue value)
 {
     return(lens.Set(value)(instance));
 }
 public static TEntity RemoveByKey <TEntity, TKey, TValue>(
     this TEntity instance,
     Lens <TEntity, IImmutableDictionary <TKey, TValue> > lens,
     TKey key)
 {
     return(instance.Set(lens.Lookup(key), Maybe <TValue> .None));
 }
示例#5
0
 Location <TValue>(int x, int y)
 {
     return(Lens
            .Create <IImmutableGrid <TValue>, TValue>(
                grid => grid[x, y],
                value => grid => grid.Set(x, y, value)));
 }
 Lookup <TKey, TValue>(TKey key, Func <TKey, TValue> defaultFactory)
 {
     return
         (Lens.Create <IImmutableDictionary <TKey, TValue>, TValue>(
              dictionary => dictionary.Lookup(key).OrElse(defaultFactory(key)),
              // ReSharper disable once ImplicitlyCapturedClosure
              value => dictionary => dictionary.SetItem(key, value)));
 }
 Lookup <TKey, TValue>(TKey key)
 {
     return
         (Lens.Create <IImmutableDictionary <TKey, TValue>, IMaybe <TValue> >(
              dictionary => dictionary.Lookup(key),
              value => dictionary =>
     {
         return value
         .Select(x => dictionary.SetItem(key, x))
         .OrElse(() => dictionary.Remove(key));
     }));
 }
示例#8
0
 public static TValue Get <TType, TValue>(
     this TType instance,
     Lens <TType, TValue> lens)
 {
     return(lens.Get(instance));
 }
 Lookup <TEntity, TKey, TValue>(
     this Lens <TEntity, IImmutableDictionary <TKey, TValue> > lens,
     TKey key, Func <TKey, TValue> defaultFactory)
 {
     return(lens.With(Lookup(key, defaultFactory)));
 }
 Lookup <TType, TKey, TValue>(
     this Lens <TType, IImmutableDictionary <TKey, TValue> > lens,
     TKey key)
 {
     return(lens.With(Lookup <TKey, TValue>(key)));
 }
示例#11
0
 public static Lens <TEntity, TValue> Location <TEntity, TValue>(
     this Lens <TEntity, IImmutableGrid <TValue> > self, int x, int y)
 {
     return(self.With(Location <TValue>(x, y)));
 }
示例#12
0
 public static Lens <TEntity, TValue> Location <TEntity, TValue>(
     this Lens <TEntity, IImmutableGrid <TValue> > self, Vector location)
 {
     return(self.With(Location <TValue>(location.X, location.Y)));
 }