Exemplo n.º 1
0
        /// <summary>
        /// "Applicative" instance for HaxlFetch.
        /// </summary>
        /// <remarks>
        /// This isn't a true applicative instance; we don't have:
        ///
        /// > (<*>) :: f (a -> b) -> f a -> f b
        ///
        /// In Haskell Haxl, the applicative instance is used to keep fetched values in scope:
        ///
        /// > (a, b) <- (,) <$> fetch1 <*> fetch2
        ///
        /// C# can't do nested lambda scoping, and uses transparent identifers instead.
        /// Because the transparent identifers aren't accessible to us, we use our own scoping system.
        ///
        /// This means our (a -> b) function is *always* (Scope -> Scope);
        /// we therefore can write our "Applicative" instance as simply a function that takes two Fetches.
        /// </remarks>
        public static Haxl Applicative(this Haxl fetch1, Haxl fetch2)
        {
            return(Haxl.FromFunc((cache, logger) =>
            {
                var result1 = fetch1.Result(cache, logger);
                var result2 = fetch2.Result(cache, logger);
                return result1.Match
                (
                    done1 => result2.Match <Result>
                    (
                        done2 => Done.New(compose(done2.AddToScope, done1.AddToScope)),
                        blocked2 => Blocked.New(blocked2.BlockedRequests, blocked2.Continue.Map(done1.AddToScope))
                    ),

                    blocked1 => result2.Match <Result>
                    (
                        done2 => Blocked.New(blocked1.BlockedRequests, blocked1.Continue.Map(done2.AddToScope)),
                        blocked2 => Blocked.New(
                            blocked1.BlockedRequests.Concat(blocked2.BlockedRequests),
                            blocked1.Continue.Applicative(blocked2.Continue)
                            )
                    )
                );
            }));
        }
Exemplo n.º 2
0
 /// <summary>
 /// Monad instance for HaxlFetch.
 /// </summary>
 public static Haxl Bind(this Haxl fetch, Func <Scope, Haxl> bind)
 {
     return(Haxl.FromFunc((cache, logger) =>
     {
         var result = fetch.Result(cache, logger);
         return result.Match(
             done => bind(done.AddToScope(Scope.New())).Result(cache, logger),
             blocked => Blocked.New(blocked.BlockedRequests, blocked.Continue.Bind(bind))
             );
     }));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Folds an applicative group into a Haxl monad.
        /// </summary>
        public static Func <Scope, Haxl> ApplicativeToHaxl(ApplicativeGroup applicative, string parentBind)
        {
            var expressions = applicative.Expressions;

            if (applicative.Expressions.Count == 1)
            {
                return(StatementToHaxl(expressions.First(), parentBind));
            }
            return(scope => applicative.Expressions.Aggregate
                   (
                       Haxl.FromFunc((c, l) => Done.New(s => s)),
                       (group, be) =>
            {
                var haxl = StatementToHaxl(be, parentBind)(scope);
                return group.Applicative(haxl);
            }
                   ));
        }
Exemplo n.º 4
0
 /// <summary>
 /// Converts a project expression to Haxl monad.
 /// </summary>
 public static Func <Scope, Haxl> ProjectToHaxl(ProjectStatement project, string parentBind)
 {
     return(scope => Haxl.FromFunc((cache, logger) =>
     {
         var rewritten = RebindToScope.Rebind(project.Expression);
         var result = rewritten.Compile().DynamicInvoke(scope);
         return Done.New(_ =>
         {
             if (project.Expression.BindVariable == HAXL_RESULT_NAME &&
                 !scope.IsRoot &&
                 parentBind != null)
             {
                 return scope.WriteParent(parentBind, result);
             }
             return scope.Add(project.Expression.BindVariable, result);
         });
     }));
 }