रेलवे प्रोग्रामिंग F #

Sep 17 2020

मैं स्कॉट वेलस्चिन की पुस्तक 'डोमेन मॉडलिंग फंक्शनल' में वर्णित परिणाम का उपयोग करते हुए एफ # में रेलवे प्रोग्रामिंग का उपयोग करने की कोशिश कर रहा हूं। आम तौर पर एक फ़ंक्शन में संरचना होती है

let functionName parameter : Result<ResultType, ErrorType> =
    result {
        let! resultValue = someValidationAndTransformation parameter
        return resultValue
    }

लेकिन मैं ओके और एरर केस दोनों में कुछ गणना वाले फ़ील्ड भी वापस करना चाहता हूं। सबसे अच्छा मैं साथ आ सकता था

let functionName parameter : Result<ResultType, ErrorType> * CalculatedFields =
    let mutable calculatedFields = {some defaultvalue}
    let result = 
        result {
            let! resultValue = someValidationAndTransformation parameter
            let calculatedField = someCalculation resultValue
            calculatedFields <- {calculatedFields with calculatedField}
            return resultValue
        }
    result, calculatedFields

यह परस्पर क्षेत्र अच्छा नहीं लगता। क्या ओके और एरर के मामले में परिकलित फ़ील्ड प्राप्त करने का एक बेहतर तरीका है?

जवाब

2 sdgfsdh Sep 17 2020 at 17:07

मैं matchइस स्थिति में उपयोग करेगा :

let functionName parameter : Result<ResultType, ErrorType> * CalculatedFields =
  let result = someValidationAndTransformation parameter

  let calculatedFields = 
    match result with
    | Ok x -> someCalculation x
    | Error e -> { some defaultvalue }

  result, calculatedFields