F#编码实践 [英] Coding Practice for F#

查看:137
本文介绍了F#编码实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Visual Studio 2010中使用F#。我是一位开发人员,他在C#和Java等面向对象语言中拥有更多的代码/架构设计经验。

为扩展我的技能并帮助做出更好的决策,我正在尝试使用不同的语言来完成不同的事情。特别是使用函数式语言(在这种情况下为F#)来正确编码。



一个简单的例子是生成一些XML,然后添加一些过滤器来消除一些元素。



这是我的代码:

 打开系统
打开System.Xml.Linq


let ppl:(string * string)list = [
(1,Jerry);
(2,Max);
(3,Andrew);
]

///给定一个元组生成一个Person XML元素。
let createPerson(id:string,name:string)= new XElement(XName.Get(Person),
new XAttribute(XName.Get(ID),id),
新的XElement(XName.Get(Name),name)


///通过拥有奇数ID的
来过滤人们oddFilter = fun(id:string, name:string) - > (System.Int32.Parse(id)%2).Equals(1)

///打开将返回所有人的过滤器
let allFilter = fun(id:string,name:字符串) - > true

///生成人员XML元素。
let createPeople filter = new XElement(XName.Get(People),
ppl |> List.filter(filter)|> List.map createPerson


///第一个XML对象
让XmlA = createPeople oddFilter

///第二个XML对象
让XmlB = createPeople allFilter


printf%A \\\
\\\
%AXmlA XmlB


///等待按键
让pauseKey = fun() - > ; System.Console.ReadKey()|>忽略


pauseKey()

我的问题是:在这种情况下我做得很好吗?哪些部分可以做得更好?



我非常期待一些想法,并且我也非常期待熟悉功能范例! :)



在此先感谢 原则上,您的代码是的。



从句法的角度来看,只需简单点就可以了。

  let ppl:(string * string)list = [
(1,Jerry);
(2,Max);
(3,Andrew);
]

编译器能够自行推断大多数类型:

  let ppl = [1,Jerry; 
2,Max;
3,Andrew]

当然,您可以重新编写您的由于 currying 这样的过滤器:

  let oddFilter(id:string,name:string)=(int id)%2 = 1 
let allFilter(id:string,name:string)= true

最大的改进是将索引与名称分开,并让程序完成编号。您不必使用字符串而不是数字,并且可以使用更多惯用的无元组函数:

  let ppl = [ 杰瑞; 最大; Andrew] 

让oddFilter id name = id%2 = 1
let allFilter id name = true

let createPerson id name = ...

零件

  ppl |> List.filter(filter)|> List.map createPerson 

会被重写为

 对于List.mapi(fun ix  - >(i,x))中的(index,name)do 
if filter index name then
yield createPerson (字符串索引)名称]


I have been dabbling with F# in Visual Studio 2010. I am a developer with more code/architecture design experience in object-oriented languages such as C# and Java.

To expand my skill set and help make better decisions I am trying different languages to do different things. In particular get the hang of coding "correctly" using functional languages (in this case F#).

A simple example is generating some XML, then adding some filters to eliminate some elements.

Here is my code:

open System
open System.Xml.Linq


let ppl:(string * string) list = [
    ("1", "Jerry"); 
    ("2", "Max"); 
    ("3", "Andrew");
]

/// Generates a Person XML Element, given a tuple.
let createPerson (id:string, name:string) = new XElement(XName.Get("Person"),
                                                new XAttribute(XName.Get("ID"), id),
                                                new XElement(XName.Get("Name"), name)
)

/// Filter People by having odd ID's
let oddFilter = fun (id:string, name:string) -> (System.Int32.Parse(id) % 2).Equals(1) 

/// Open filter which will return all people
let allFilter = fun (id:string, name:string) -> true

/// Generates a People XML Element.
let createPeople filter = new XElement(XName.Get("People"), 
                                ppl |> List.filter(filter)  |> List.map createPerson
)

/// First XML Object
let XmlA = createPeople oddFilter

/// Second XML Object
let XmlB = createPeople allFilter


printf "%A\n\n%A" XmlA XmlB


/// Waits for a keypress
let pauseKey = fun () -> System.Console.ReadKey() |> ignore


pauseKey()

My questions are: What things have I done well in this scenario? What parts could be done better?

I am really looking forward to some ideas and I am quite excited about becoming familiar with functional paradigms too! :)

Thanks in advance

解决方案

In principle, your code is all right.

There are just some points that can be simplified from a syntactical point of view.

let ppl:(string * string) list = [
    ("1", "Jerry"); 
    ("2", "Max"); 
    ("3", "Andrew");
]

The compiler is able to deduce most types by himself:

let ppl = [ "1", "Jerry";
            "2", "Max";
            "3", "Andrew" ]

And of course you can re-write your filters like this due to currying:

let oddFilter (id:string, name:string) = (int id) % 2 = 1
let allFilter (id:string, name:string) = true

The biggest improvement would be separating the indices from the names and let the programm do the numbering. You don't have to work with strings instead of numbers and can use more idiomatic tuple-free functions:

let ppl = [ "Jerry"; "Max"; "Andrew" ]

let oddFilter id name = id % 2 = 1
let allFilter id name = true

let createPerson id name = ...

The part

ppl |> List.filter(filter)  |> List.map createPerson

would be rewritten to

[ for (index, name) in List.mapi (fun i x -> (i, x)) do
      if filter index name then
          yield createPerson (string index) name ]

这篇关于F#编码实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆