<commit_before>﻿namespace MBrace.Continuation

open System
open System.Threading

open MBrace

/// Local, non-distributable continuation execution context.
[<AutoSerializable(false)>]
type ExecutionContext =
    {
        /// Cloud cancellation token of the current context
        CancellationToken : ICloudCancellationToken

        /// Runtime cloud resource resolver
        Resources : ResourceRegistry
    }
with
    /// <summary>
    ///     Initializes an empty execution context.  
    /// </summary>
    /// <param name="cancellationToken">Optional cancellation token.</param>
    static member Empty(cancellationToken : ICloudCancellationToken) =
        {
            Resources = ResourceRegistry.Empty
            CancellationToken = cancellationToken
        }

/// Distributable continuation context.
[<AutoSerializable(true)>]
type Continuation<'T> =
    {
        /// Success continuation
        Success : ExecutionContext -> 'T -> unit

        /// Exception continuation
        Exception : ExecutionContext -> ExceptionDispatchInfo -> unit

        /// Cancellation continuation
        Cancellation : ExecutionContext -> OperationCanceledException -> unit
    }

/// Continuation utility functions
[<RequireQualifiedAccess>]
module Continuation =
    
    /// <summary>
    ///     Contravariant Continuation map combinator.
    /// </summary>
    /// <param name="f">Mapper function.</param>
    /// <param name="tcont">Initial continuation.</param>
    let inline map (f : 'S -> 'T) (tcont : Continuation<'T>) : Continuation<'S> =
        {
            Success = fun ctx s -> tcont.Success ctx (f s)
            Exception = tcont.Exception
            Cancellation = tcont.Cancellation<commit_msg>fix Continuation.map<commit_after>﻿namespace MBrace.Continuation

open System
open System.Threading

open MBrace

/// Local, non-distributable continuation execution context.
[<AutoSerializable(false)>]
type ExecutionContext =
    {
        /// Cloud cancellation token of the current context
        CancellationToken : ICloudCancellationToken

        /// Runtime cloud resource resolver
        Resources : ResourceRegistry
    }
with
    /// <summary>
    ///     Initializes an empty execution context.  
    /// </summary>
    /// <param name="cancellationToken">Optional cancellation token.</param>
    static member Empty(cancellationToken : ICloudCancellationToken) =
        {
            Resources = ResourceRegistry.Empty
            CancellationToken = cancellationToken
        }

/// Distributable continuation context.
[<AutoSerializable(true)>]
type Continuation<'T> =
    {
        /// Success continuation
        Success : ExecutionContext -> 'T -> unit

        /// Exception continuation
        Exception : ExecutionContext -> ExceptionDispatchInfo -> unit

        /// Cancellation continuation
        Cancellation : ExecutionContext -> OperationCanceledException -> unit
    }

/// Continuation utility functions
[<RequireQualifiedAccess>]
module Continuation =
    
    /// <summary>
    ///     Contravariant Continuation map combinator.
    /// </summary>
    /// <param name="f">Mapper function.</param>
    /// <param name="tcont">Initial continuation.</param>
    let inline map (f : 'S -> 'T) (tcont : Continuation<'T>) : Continuation<'S> =
        {
            Success = 
                fun ctx s -> 
                    match (try f s |> Choice1Of2 with e -> Choice2Of2 e) with
                    | Choice1Of2 t -> tcont.Success ctx t
                    | Choice2Of2 e -> tcont.Exception ctx (ExceptionDispatchInfo.Capture e) 

            Exception = tcont.Exception
            Cancellation = tcont.Cancellation
{'fraction_non_alphanumeric': 0.10318540702423087, 'fraction_numerical': 0.0021780560849441874, 'mean_word_length': 2.3737373737373737, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4662553', 'n_tokens_mistral': 916, 'n_tokens_neox': 919, 'n_words': 245}