English 中文(简体)
F#创建和使用功能登记处
原标题:Create and use a function registry in F#
  • 时间:2010-07-03 21:57:59
  •  标签:
  • f#

我目前正在撰写的一项方案中使用现行法典模式:

let test_titles = ["a_fault"; "b_fault"; "c_fault"]
let tests: (unit ->  component option) list = [a_fault; b_fault; c_fault]
let test_registry = List.zip test_titles tests
let apply_test (title, test) = test () |> Option.map (fun s -> (title, s))
let result: (string *  component) option = test_registry |> List.tryPick apply_test

a) 测试登记处,确定错误成分和错误类型,其名称与功能名称相同。

  1. 是否有更好的办法创建这一检验标准——管理,最好是没有我手工撰写测试名称(从理论上来说,获得功能名称)?

  2. 一般说来,这是否是这种属性F。

<>Edit:该代码在最后一行有误。 计算结果时使用了test_registry。 而不是<代码>测试

最佳回答

避免在守则中明确写测试名称(作为指示)的一种办法是使用引文。 您不但没有制定一份职能清单和说明清单,反而可以编制一份“援引”职能价值清单。 然后,你可以写法,处理报价,并给你所需要的一切。

我假定,你的测试大致如下(因此而计算单位和退还一定价值的职能)。 清单如下:

let test_a () = Some 32
let test_b () = None
let tests = [ <@ test_a @>; <@ test_b @> ]

然后,你可以撰写这样的法典,以获得有关测试的信息:

open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns

let getInfo (e:Expr<unit ->  R>) = //  
  match e.Raw with 
  // This expects that the quotation contains a reference to a global
  // function (in some module) that takes unit as the parameter
  | Lambda(a, Call(_, m, _)) -> 
      // Return a tuple containing a name of the test (string) and
      // a function that invokes it (note that the invocation will be 
      // a bit slow as we use reflection here)
      m.Name, (fun () -> m.Invoke(null, [| |]) :?>  R) //   (*)
  // Ohter quotations will cause an exception
  | _ -> failwith "unexpected quotation"

下面是你将如何利用:

let e = <@ test_a @>    
let s, f = getInfo e // gives  string * (unit -> int option)

// Your original code could be written like this:
tests |> List.map getInfo |> List.tryPick (fun (title, test) ->
  test () |> Option.map (fun s -> (title, s)))

或者,您可以修改<代码>(*),以产生一种功能,将测试的名称和结果重新命名,从而不再需要<代码>。 备选案文:

// An alternative version of the marked line in the  getInfo  function
(fun () -> m.Name, m.Invoke(null, [| |]) :?>  R) //   (*)

// Then you can write just:
tests |> List.map getInfo |> List.tryPick (fun test -> test())
问题回答

这似乎不是坏的,但应考虑的另一个办法是让测试了解自己的姓名。 现在你们有(我)

type Test< comp> = unit ->  comp option

否则,你本可以这样做。

type Test< comp> = unit -> string *  comp option

方名。

我对你提供其他建议或了解这种意见是否合理并不有好感。

如果测试是某些模块的一部分,那么,如果测试是<代码>M的一部分,那么你可以使用<代码>(?)的操作者,这样,例如<代码>M?foo可使用测试功能及其名称。





相关问题
F#: Storing and mapping a list of functions

I have a number of events that happen in a game. I want to control the time and order at which these events occur. For example: Event 1: Show some text on screen for N frames & play a sound ...

Creating Silverlight 3 applications with F#

Is there an easy way to create Silverlight 3 applications with F# (October CTP)? I have seen the F# for Silverlight, but that only works with the May CTP. I am using Visual Studio Integrated Shell ...

How To Change List of Chars To String?

In F# I want to transform a list of chars into a string. Consider the following code: let lChars = [ a ; b ; c ] If I simply do lChars.ToString, I get "[ a ; b ; c ]". I m trying to get "abc". I ...

Unzipping ZLIB compressed portions of a binary file

I m reading a file(a flash swf) from .Net 3.5 that has a header which states whether the body of the file/Stream is compressed or not. However, I m having problems-after I rewrap the basic File ...

Pretty print a tree

Let s say I have a binary tree data structure defined as follows type a tree = | Node of a tree * a * a tree | Nil I have an instance of a tree as follows: let x = Node (Node (...

F# String Pattern-Matching with Wildcards

As part of a project I have assigned myself as a way of improving my knowledge of F# and functional programming in general, I am attempting to write a string pattern-matching algorithm from scratch ...

热门标签