public static Aff <Func <C, D, E> > Apply <A, B, C, D, E>(this Aff <Func <A, B, C, D, E> > mf, Aff <A> ma, Aff <B> mb) =>
    AffMaybe(async() =>
    {
        var(f, a, b) = await WaitAsync.WaitAll(mf.Run(), ma.Run(), mb.Run()).ConfigureAwait(false);

        if (f.IsFail || a.IsFail || b.IsFail)
        {
            var errs = Errors.None;
            if (f.IsFail)
            {
                errs += f.Error;
            }
            if (a.IsFail)
            {
                errs += a.Error;
            }
            if (b.IsFail)
            {
                errs += b.Error;
            }
            return(errs);
        }
        else
        {
            return(FinSucc((C c, D d) => f.Value(a.Value, b.Value, c, d)));
        }
    });
    public static Aff <RT, Func <C, D, E> > Apply <RT, A, B, C, D, E>(this Aff <RT, Func <A, B, C, D, E> > mf, Aff <RT, A> ma, Aff <RT, B> mb) where RT : struct, HasCancel <RT> =>
    AffMaybe <RT, Func <C, D, E> >(async rt =>
    {
        var(f, a, b) = await WaitAsync.WaitAll(mf.Run(rt), ma.Run(rt), mb.Run(rt)).ConfigureAwait(false);

        if (f.IsFail || a.IsFail || b.IsFail)
        {
            var errs = Errors.None;
            if (f.IsFail)
            {
                errs += f.Error;
            }
            if (a.IsFail)
            {
                errs += a.Error;
            }
            if (b.IsFail)
            {
                errs += b.Error;
            }
            return(errs);
        }
        else
        {
            return(FinSucc((C c, D d) => f.Value(a.Value, b.Value, c, d)));
        }
    });
    public static Aff <RT, E> Apply <RT, A, B, C, D, E>(this Func <A, B, C, D, E> f, Aff <RT, A> ma, Aff <RT, B> mb, Aff <RT, C> mc, Aff <RT, D> md) where RT : struct, HasCancel <RT> =>
    AffMaybe <RT, E>(async rt =>
    {
        var(a, b, c, d) = await WaitAsync.WaitAll(ma.Run(rt), mb.Run(rt), mc.Run(rt), md.Run(rt)).ConfigureAwait(false);

        if (a.IsFail || b.IsFail || c.IsFail || d.IsFail)
        {
            var errs = Errors.None;
            if (a.IsFail)
            {
                errs += a.Error;
            }
            if (b.IsFail)
            {
                errs += b.Error;
            }
            if (c.IsFail)
            {
                errs += c.Error;
            }
            if (d.IsFail)
            {
                errs += d.Error;
            }
            return(errs);
        }
        else
        {
            return(f(a.Value, b.Value, c.Value, d.Value));
        }
    });
    public static Aff <D> Apply <A, B, C, D>(this Aff <Func <A, B, C, D> > mf, Aff <A> ma, Aff <B> mb, Aff <C> mc) =>
    AffMaybe <D>(async() =>
    {
        var(f, a, b, c) = await WaitAsync.WaitAll(mf.Run(), ma.Run(), mb.Run(), mc.Run()).ConfigureAwait(false);

        if (f.IsFail || a.IsFail || b.IsFail || c.IsFail)
        {
            var errs = Errors.None;
            if (f.IsFail)
            {
                errs += f.Error;
            }
            if (a.IsFail)
            {
                errs += a.Error;
            }
            if (b.IsFail)
            {
                errs += b.Error;
            }
            if (c.IsFail)
            {
                errs += c.Error;
            }
            return(errs);
        }
        else
        {
            return(f.Value(a.Value, b.Value, c.Value));
        }
    });
    public static Aff <RT, D> Apply <RT, A, B, C, D>(this Aff <RT, Func <A, B, C, D> > mf, Aff <RT, A> ma, Aff <RT, B> mb, Aff <RT, C> mc) where RT : struct, HasCancel <RT> =>
    AffMaybe <RT, D>(async rt =>
    {
        var(f, a, b, c) = await WaitAsync.WaitAll(mf.Run(rt), ma.Run(rt), mb.Run(rt), mc.Run(rt)).ConfigureAwait(false);

        if (f.IsFail || a.IsFail || b.IsFail || c.IsFail)
        {
            var errs = Errors.None;
            if (f.IsFail)
            {
                errs += f.Error;
            }
            if (a.IsFail)
            {
                errs += a.Error;
            }
            if (b.IsFail)
            {
                errs += b.Error;
            }
            if (c.IsFail)
            {
                errs += c.Error;
            }
            return(errs);
        }
        else
        {
            return(f.Value(a.Value, b.Value, c.Value));
        }
    });
    public static Aff <RT, Func <B, C> > Apply <RT, A, B, C>(this Func <A, B, C> f, Aff <RT, A> ma) where RT : struct, HasCancel <RT> =>
    AffMaybe <RT, Func <B, C> >(async rt =>
    {
        var a = await ma.Run(rt).ConfigureAwait(false);

        if (a.IsFail)
        {
            return(a.Error);
        }
        else
        {
            return(FinSucc((B b) => f(a.Value, b)));
        }
    });
Beispiel #7
0
        private static string Format(Aff a)
        {
            string s = "[" + a.GetType().Name + "]";

            if (a.list == null)
            {
                return(s + " null");
            }
            s += " " + a.list.Count;
            foreach (var b in a.list)
            {
                s += "\n\t" + Format(b);
            }
            return(s);
        }
Beispiel #8
0
        public static void Deserialize(TypeModel model, byte[] b, TextWriter dest)
        {
            MemoryStream mspp = new MemoryStream(b);
            MemoryStream mspf = new MemoryStream(b);
            MemoryStream msfp = new MemoryStream(b);
            MemoryStream msff = new MemoryStream(b);
            App          app  = model.Deserialize <App>(mspp);
            Apf          apf  = model.Deserialize <Apf>(mspf);
            Afp          afp  = model.Deserialize <Afp>(msfp);
            Aff          aff  = model.Deserialize <Aff>(msff);

            dest.WriteLine(Format(app));
            dest.WriteLine(Format(apf));
            dest.WriteLine(Format(afp));
            dest.WriteLine(Format(aff));
        }
Beispiel #9
0
        private static void Deserialize(byte[] b, TextWriter dest)
        {
            MemoryStream mspp = new MemoryStream(b);
            MemoryStream mspf = new MemoryStream(b);
            MemoryStream msfp = new MemoryStream(b);
            MemoryStream msff = new MemoryStream(b);
            App          app  = Serializer.Deserialize <App>(mspp);
            Apf          apf  = Serializer.Deserialize <Apf>(mspf);
            Afp          afp  = Serializer.Deserialize <Afp>(msfp);
            Aff          aff  = Serializer.Deserialize <Aff>(msff);

            dest.WriteLine(Format(app));
            dest.WriteLine(Format(apf));
            dest.WriteLine(Format(afp));
            dest.WriteLine(Format(aff));
        }
Beispiel #10
0
 /// <summary>
 /// Creates a new activity if there are active listeners for it, using the specified name, activity kind, parent
 /// activity context, tags, optional activity link and optional start time.
 /// </summary>
 /// <param name="operation">The operation to whose activity will be traced</param>
 /// <param name="name">The operation name of the activity.</param>
 /// <param name="kind">The activity kind.</param>
 /// <param name="parentContext">The parent `ActivityContext` object to initialize the created activity object
 /// with</param>
 /// <param name="tags">The optional tags list to initialise the created activity object with.</param>
 /// <param name="links">The optional `ActivityLink` list to initialise the created activity object with.</param>
 /// <param name="startTime">The optional start timestamp to set on the created activity object.</param>
 /// <returns>The result of the `operation`</returns>
 public static Aff <RT, A> span <A>(
     string name,
     ActivityKind kind,
     ActivityContext parentContext,
     HashMap <string, object> tags,
     Seq <ActivityLink> links,
     DateTimeOffset startTime,
     Aff <RT, A> operation) =>
 use(default(RT).ActivitySourceEff.Map(rt => rt.StartActivity(
                                           name,
                                           kind,
                                           parentContext,
                                           tags,
                                           links,
                                           startTime)),
     act => localAff <RT, RT, A>(rt => rt.SetActivity(act), operation));
Beispiel #11
0
 /// <summary>
 /// Creates a new activity if there are active listeners for it, using the specified name, activity kind, parent
 /// activity context, tags, optional activity link and optional start time.
 /// </summary>
 /// <param name="operation">The operation to whose activity will be traced</param>
 /// <param name="name">The operation name of the activity.</param>
 /// <param name="kind">The activity kind.</param>
 /// <param name="tags">The optional tags list to initialise the created activity object with.</param>
 /// <param name="links">The optional `ActivityLink` list to initialise the created activity object with.</param>
 /// <param name="startTime">The optional start timestamp to set on the created activity object.</param>
 /// <returns>The result of the `operation`</returns>
 static Aff <RT, A> span <A>(
     string name,
     ActivityKind kind,
     HashMap <string, object> tags,
     Seq <ActivityLink> links,
     DateTimeOffset startTime,
     Aff <RT, A> operation) =>
 from e in runtime <RT>()
 from r in use(e.ActivitySourceEff.Map(
                   rt => e.CurrentActivity == null
                     ? rt.StartActivity(name, kind)
                     : rt.StartActivity(
                       name,
                       kind,
                       e.CurrentActivity.Context,
                       tags,
                       links,
                       startTime)),
               act => localAff <RT, RT, A>(rt => rt.SetActivity(act), operation))
 select r;
    public static Aff <Func <B, C, D> > Apply <A, B, C, D>(this Aff <Func <A, B, C, D> > mf, Aff <A> ma) =>
    AffMaybe(async() =>
    {
        var(f, a) = await WaitAsync.WaitAll(mf.Run(), ma.Run()).ConfigureAwait(false);

        if (f.IsFail || a.IsFail)
        {
            var errs = Seq <Error>();
            if (f.IsFail)
            {
                errs = errs.Add(f.Error);
            }
            if (a.IsFail)
            {
                errs = errs.Add(a.Error);
            }
            return(Error.Many(errs));
        }
        else
        {
            return(FinSucc((B b, C c) => f.Value(a.Value, b, c)));
        }
    });
    // ------------ Aff<A> ----------------------------------------------------------------------------------

    public static Aff <B> Apply <A, B>(this Aff <Func <A, B> > mf, Aff <A> ma) =>
    AffMaybe <B>(async() =>
    {
        var(f, a) = await WaitAsync.WaitAll(mf.Run(), ma.Run()).ConfigureAwait(false);

        if (f.IsFail || a.IsFail)
        {
            var errs = Errors.None;
            if (f.IsFail)
            {
                errs += f.Error;
            }
            if (a.IsFail)
            {
                errs += a.Error;
            }
            return(errs);
        }
        else
        {
            return(f.Value(a.Value));
        }
    });
    // ------------ Aff<RT, A> -----------------------------------------------------------------------------------------

    public static Aff <RT, B> Apply <RT, A, B>(this Aff <RT, Func <A, B> > mf, Aff <RT, A> ma) where RT : struct, HasCancel <RT> =>
    AffMaybe <RT, B>(async rt =>
    {
        var(f, a) = await WaitAsync.WaitAll(mf.Run(rt), ma.Run(rt)).ConfigureAwait(false);

        if (f.IsFail || a.IsFail)
        {
            var errs = Errors.None;
            if (f.IsFail)
            {
                errs += f.Error;
            }
            if (a.IsFail)
            {
                errs += a.Error;
            }
            return(errs);
        }
        else
        {
            return(f.Value(a.Value));
        }
    });
Beispiel #15
0
 public static Lift <RT, R> use <RT, R>(Aff <R> ma)
     where RT : struct, HasCancel <RT>
     where R : IDisposable =>
 Lift.Aff <RT, R>(ma);
Beispiel #16
0
 public static Lift <RT, R> lift <RT, R>(Aff <R> ma) where RT : struct, HasCancel <RT> =>
 Lift.Aff <RT, R>(ma);
Beispiel #17
0
 public Lift(Aff <RT, X> value, Func <X, ProducerLift <RT, OUT, A> > next) =>
 (Value, Next) = (value, next);
Beispiel #18
0
 public static Lift <RT, A> Aff <RT, A>(Aff <RT, A> value) where RT : struct, HasCancel <RT> =>
 new Lift <RT, A> .Do <A>(value, Pure <RT, A>);
Beispiel #19
0
 public Do(Aff <RT, X> value, Func <X, Lift <RT, A> > next) =>
 (Effect, Next) = (value, next);
Beispiel #20
0
 public static Proxy <RT, A1, A, B1, B, R> use <RT, A1, A, B1, B, R>(Aff <RT, R> ma, Func <R, Unit> dispose)
     where RT : struct, HasCancel <RT> =>
 new Use <RT, A1, A, B1, B, R, R>(() => ma, dispose, Pure <RT, A1, A, B1, B, R>);
Beispiel #21
0
 public static Consumer <RT, IN, R> use <RT, IN, R>(Aff <RT, R> ma, Func <R, Unit> dispose)
     where RT : struct, HasCancel <RT> =>
 use <RT, Unit, IN, Unit, Void, R>(ma, dispose).ToConsumer();
Beispiel #22
0
 public Lift(Aff <RT, X> value, Func <X, ConsumerLift <RT, IN, A> > next) =>
 (Value, Next) = (value, next);
Beispiel #23
0
 private static string Format(Aff a)
 {
     string s = "[" + a.GetType().Name + "]";
     if (a.list == null)
         return s + " null";
     s += " " + a.list.Count;
     foreach (var b in a.list)
         s += "\n\t" + Format(b);
     return s;
 }
Beispiel #24
0
 public static Pipe <RT, A, B, R> lift <RT, A, B, R>(Aff <RT, R> ma) where RT : struct, HasCancel <RT> =>
 lift <RT, Unit, A, Unit, B, R>(ma).ToPipe();
Beispiel #25
0
        public void Execute()
        {
            //Test Data
            App app = new App();
            Apf apf = new Apf();
            Afp afp = new Afp();
            Aff aff = new Aff();
            Bp bp1 = new Bp() { a = "b1" };
            Bp bp2 = new Bp() { a = "b2" };
            Bf bf1 = new Bf() { a = "b1" };
            Bf bf2 = new Bf() { a = "b2" };
            bp1.b = new List<string>();
            bp2.b = new List<string>();
            bf1.b = new List<string>();
            bf2.b = new List<string>();
            bp1.b.Add("a");
            bp2.b.Add("b");
            bf1.b.Add("a");
            bf2.b.Add("b");
            app.list.Add(bp1);
            app.list.Add(bp2);
            apf.list.Add(bf1);
            apf.list.Add(bf2);
            afp.list.Add(bp1);
            afp.list.Add(bp2);
            aff.list.Add(bf1);
            aff.list.Add(bf2);

            StringWriter before = new StringWriter();
            before.WriteLine(Format(app));
            before.WriteLine(Format(apf));
            before.WriteLine(Format(afp));
            before.WriteLine(Format(aff));

            //Serialize
            MemoryStream mspp = new MemoryStream();
            MemoryStream mspf = new MemoryStream();
            MemoryStream msfp = new MemoryStream();
            MemoryStream msff = new MemoryStream();
            Serializer.Serialize(mspp, app);
            Serializer.Serialize(mspf, apf);
            Serializer.Serialize(msfp, afp);
            Serializer.Serialize(msff, aff);

            //Compare binary data
            byte[] bpp = mspp.ToArray();
            byte[] bpf = mspf.ToArray();
            byte[] bfp = msfp.ToArray();
            byte[] bff = msff.ToArray();

            Assert.AreEqual(18, bpp.Length);

            if (bpp.Length != bpf.Length)
                throw new InvalidDataException("Length does not match");
            if (bpf.Length != bff.Length)
                throw new InvalidDataException("Length does not match");
            if (bff.Length != bfp.Length)
                throw new InvalidDataException("Length does not match");
            for (int n = 0; n < bpp.Length; n++)
            {
                if (bpp[n] != bpf[n] || bpf[n] != bff[n] || bff[n] != bfp[n])
                    throw new InvalidDataException("Data does not match");
            }

            //Deserialize
            StringWriter after = new StringWriter();
            Deserialize(bpp, after);

            Assert.AreEqual(before.ToString(), after.ToString());

        }
Beispiel #26
0
 public static Proxy <RT, A1, A, B1, B, R> lift <RT, A1, A, B1, B, R>(Aff <RT, R> ma) where RT : struct, HasCancel <RT> =>
 Disposable <R> .IsDisposable
         ? use <RT, A1, A, B1, B, R>(ma, anyDispose)
 : new M <RT, A1, A, B1, B, R>(ma.Map(Pure <RT, A1, A, B1, B, R>));
Beispiel #27
0
 public static Proxy <RT, A1, A, B1, B, R> use <RT, A1, A, B1, B, R>(Aff <RT, R> ma)
     where RT : struct, HasCancel <RT>
     where R : IDisposable =>
 new Use <RT, A1, A, B1, B, R, R>(() => ma, dispose, Pure <RT, A1, A, B1, B, R>);
Beispiel #28
0
 public static Consumer <RT, A, Unit> lift <RT, A>(Aff <RT, Unit> ma) where RT : struct, HasCancel <RT> =>
 lift <RT, Unit, A, Unit, Void, Unit>(ma).ToConsumer();
Beispiel #29
0
 public static Pipe <RT, IN, OUT, R> use <RT, IN, OUT, R>(Aff <RT, R> ma)
     where RT : struct, HasCancel <RT>
     where R : IDisposable =>
 use <RT, Unit, IN, Unit, OUT, R>(ma).ToPipe();
Beispiel #30
0
 public static Consumer <RT, IN, R> use <RT, IN, R>(Aff <R> ma)
     where RT : struct, HasCancel <RT>
     where R : IDisposable =>
 use <RT, Unit, IN, Unit, Void, R>(ma).ToConsumer();
Beispiel #31
0
        public void Execute()
        {
            //Test Data
            App app = new App();
            Apf apf = new Apf();
            Afp afp = new Afp();
            Aff aff = new Aff();
            Bp  bp1 = new Bp()
            {
                a = "b1"
            };
            Bp bp2 = new Bp()
            {
                a = "b2"
            };
            Bf bf1 = new Bf()
            {
                a = "b1"
            };
            Bf bf2 = new Bf()
            {
                a = "b2"
            };

            bp1.b = new List <string>();
            bp2.b = new List <string>();
            bf1.b = new List <string>();
            bf2.b = new List <string>();
            bp1.b.Add("a");
            bp2.b.Add("b");
            bf1.b.Add("a");
            bf2.b.Add("b");
            app.list.Add(bp1);
            app.list.Add(bp2);
            apf.list.Add(bf1);
            apf.list.Add(bf2);
            afp.list.Add(bp1);
            afp.list.Add(bp2);
            aff.list.Add(bf1);
            aff.list.Add(bf2);

            using StringWriter before = new StringWriter();
            before.WriteLine(Format(app));
            before.WriteLine(Format(apf));
            before.WriteLine(Format(afp));
            before.WriteLine(Format(aff));

            //Serialize
            MemoryStream mspp = new MemoryStream();
            MemoryStream mspf = new MemoryStream();
            MemoryStream msfp = new MemoryStream();
            MemoryStream msff = new MemoryStream();

            Serializer.Serialize(mspp, app);
            Serializer.Serialize(mspf, apf);
            Serializer.Serialize(msfp, afp);
            Serializer.Serialize(msff, aff);

            //Compare binary data
            byte[] bpp = mspp.ToArray();
            byte[] bpf = mspf.ToArray();
            byte[] bfp = msfp.ToArray();
            byte[] bff = msff.ToArray();

            Assert.Equal(18, bpp.Length);

            if (bpp.Length != bpf.Length)
            {
                throw new InvalidDataException("Length does not match");
            }
            if (bpf.Length != bff.Length)
            {
                throw new InvalidDataException("Length does not match");
            }
            if (bff.Length != bfp.Length)
            {
                throw new InvalidDataException("Length does not match");
            }
            for (int n = 0; n < bpp.Length; n++)
            {
                if (bpp[n] != bpf[n] || bpf[n] != bff[n] || bff[n] != bfp[n])
                {
                    throw new InvalidDataException("Data does not match");
                }
            }

            //Deserialize
            StringWriter after = new StringWriter();

            Deserialize(bpp, after);

            Assert.Equal(before.ToString(), after.ToString());
        }
Beispiel #32
0
 public static Pipe <RT, IN, OUT, R> use <RT, IN, OUT, R>(Aff <RT, R> ma, Func <R, Unit> dispose)
     where RT : struct, HasCancel <RT> =>
 use <RT, Unit, IN, Unit, OUT, R>(ma, dispose).ToPipe();