/// <summary> /// Returns a cloud computation that will execute the given computation on every available worker /// possibly in parallel and will return when any of the supplied computations /// have returned a successful value or if all of them fail to succeed. /// If a computation succeeds the rest of them are canceled. /// The success of a computation is encoded as an option type. /// This operator may create distribution. /// </summary> /// <typeparam name="TResult">Computation return type.</typeparam> /// <param name="workflow">Input workflow to be executed nondeterministically everywhere.</param> /// <returns>The result of the first computation to complete in the positive.</returns> public static Cloud <Option <TResult> > Choice <TResult>(Cloud <Option <TResult> > workflow) { Func <Option <TResult>, MBrace.Cloud <FSharpOption <TResult> > > transform = option => Builder.Return(option.AsFSharpOption()); var fsTransform = transform.AsFSharpFunc(); var x = Builder.Bind(workflow, fsTransform); var choice = MCloud.Choice(x); Func <FSharpOption <TResult>, MBrace.Cloud <Option <TResult> > > transformRev = fsOption => Builder.Return(Option <TResult> .FromFSharpOption(fsOption)); var fsTransformRev = transformRev.AsFSharpFunc(); var result = Builder.Bind(choice, fsTransformRev); return(result); }
/// <summary> /// Performs a nondeterministic computation in parallel. /// </summary> /// <typeparam name="TResult">Computation return type.</typeparam> /// <param name="workflows">Input workflows to be executed nondeterministically.</param> /// <returns>The result of the first computation to complete in the positive.</returns> public static Cloud <Option <TResult> > Choice <TResult>(this IEnumerable <Cloud <Option <TResult> > > workflows) { Func <Option <TResult>, MBrace.Cloud <FSharpOption <TResult> > > transform = option => Builder.Return(option.AsFSharpOption()); var fsTransform = transform.AsFSharpFunc(); var wfs = workflows .Select(wf => Builder.Bind(wf, fsTransform)) .ToArray(); var choice = MCloud.Choice <TResult>(wfs); Func <FSharpOption <TResult>, MBrace.Cloud <Option <TResult> > > transformRev = fsOption => Builder.Return(Option <TResult> .FromFSharpOption(fsOption)); var fsTransformRev = transformRev.AsFSharpFunc(); var result = Builder.Bind(choice, fsTransformRev); return(result); }