F# 将c#逻辑转换为f的最佳实践#
因此,我将此作为脚本:F# 将c#逻辑转换为f的最佳实践#,f#,f#-interactive,c#-to-f#,f#-data,F#,F# Interactive,C# To F#,F# Data,因此,我将此作为脚本: #r @"..\packages\FSharp.Data.2.4.2\lib\net45\FSharp.Data.dll" #r "System.Xml.Linq.dll" open FSharp.Data // provide some URL, file path, or raw JSON sample // that can be used at compile time to generate types type Json = JsonProvider<
#r @"..\packages\FSharp.Data.2.4.2\lib\net45\FSharp.Data.dll"
#r "System.Xml.Linq.dll"
open FSharp.Data
// provide some URL, file path, or raw JSON sample
// that can be used at compile time to generate types
type Json = JsonProvider<"example.json">
// at runtime, load up new sample matching that schema
let response = Http.Request("https://world.openfoodfacts.org/api/v0/product/737628064502.json")
let samples = Json.Parse(response.Body.ToString())
f#的逻辑是什么
目前我有这个(还不正确/完整)
namespace LookupByBarcode.openfoodfacts.fSharp
打开FSharp.Data
类型模型(语言、条形码)=
成员:this.Language=Language
此成员。条形码=条形码
模块getRemote=
类型json=JsonProvider
让response=json.Load(“https://world.openfoodfacts.org/api/v0/product/737628064502.json")
让Title=response.Product.ProductNameEn
如果您有一个无法修改的外部DLL,该DLL定义了一个类型产品
,则没有简单的方法向产品添加新字段。我认为最优雅的F#方法是将其包装在一个记录中,添加额外的信息:
type MyProduct =
{ Product : Product
Language : string
Barcode : string }
接下来,如果要更新MyProduct
值以使其反映新获取的标题,则它取决于基础Product
类型的外观。它是可变的还是不变的
要获得惯用的F#代码,您需要创建一个返回克隆的函数。这对于F#记录很容易做到,但对于C#对象可能很难做到。它可能看起来像这样:
public static Models.Product UpdateFromRemote(this Models.Product Product, string Language, string Barcode)
{
//the f# code included above to fetch the remote source
//only the url is fetched from:
// string.Format("https://{0}.openfoodfacts.org/api/v0/product/{1}.json",Language,Barcode);
Product.Title = samples.Product.GenericName; //samples is the fetched resource from the f# code
return Product;
}
let updateProduct newTitle newLanguage myproduct =
// Create a new `Product` with changed title. If you want to avoid mutation,
// you need to create a new instance, which might be tedious for C# objects
let op = myproduct.Product
let np =
Product
(Title = newTitle, OtherThing = op.OtherThing,
YetAnotherThing = op.YetAnotherThing)
// Now create a new updated F# record - this is much easier
{ myprodcut with Product = np; Language = newLanguage }
在F#中习惯性地处理C#可变对象将是一件棘手的事情,因此我建议在F#中定义域模型,这将使您可以使用漂亮的F#
和
关键字。这就是说,如果你真的想做的话,只改变C对象也是有意义的。如果你有一个无法修改的外部DLL,它定义了一个类型产品
,那么就没有简单的方法向产品中添加新字段。我认为最优雅的F#方法是将其包装在一个记录中,添加额外的信息:
type MyProduct =
{ Product : Product
Language : string
Barcode : string }
接下来,如果要更新MyProduct
值以使其反映新获取的标题,则它取决于基础Product
类型的外观。它是可变的还是不变的
要获得惯用的F#代码,您需要创建一个返回克隆的函数。这对于F#记录很容易做到,但对于C#对象可能很难做到。它可能看起来像这样:
public static Models.Product UpdateFromRemote(this Models.Product Product, string Language, string Barcode)
{
//the f# code included above to fetch the remote source
//only the url is fetched from:
// string.Format("https://{0}.openfoodfacts.org/api/v0/product/{1}.json",Language,Barcode);
Product.Title = samples.Product.GenericName; //samples is the fetched resource from the f# code
return Product;
}
let updateProduct newTitle newLanguage myproduct =
// Create a new `Product` with changed title. If you want to avoid mutation,
// you need to create a new instance, which might be tedious for C# objects
let op = myproduct.Product
let np =
Product
(Title = newTitle, OtherThing = op.OtherThing,
YetAnotherThing = op.YetAnotherThing)
// Now create a new updated F# record - this is much easier
{ myprodcut with Product = np; Language = newLanguage }
在F#中习惯性地处理C#可变对象将是一件棘手的事情,因此我建议在F#中定义域模型,这将使您可以使用漂亮的F#
和
关键字。这就是说,如果你真的想做的话,只改变C对象也是有意义的。你能把f代码放在适当的地方吗?如果我知道什么是正确/错误的逻辑,我将不胜感激。也许有一点逻辑错误,f#代码是用来查找标题的,它使用了一个带有两个参数(语言和条形码)的函数。该代码在f#类中是“部分”代码。这些属性已经包含在产品模型中(我只修改Title属性)。对不起,如果不清楚,你能把f代码放在适当的地方吗?如果我知道什么是正确/错误的逻辑,我将不胜感激。也许有一点逻辑错误,f#代码是用来查找标题的,它使用了一个带有两个参数(语言和条形码)的函数。该代码在f#类中是“部分”代码。这些属性已经包含在产品模型中(我只修改Title属性)。对不起,如果不清楚的话