Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 905699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:15:43+00:00 2026-05-15T16:15:43+00:00

I am using the current pattern of code a lot in a program I

  • 0

I am using the current pattern of code a lot in a program I am currently writing:

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

to have a test registry of tests that identify faulty components and the error type which happens to be the same name as the name of the function.

  1. Is there a better way to create this test_registry, preferably without me writing the test names manually (dynamically getting the function name)?

  2. Generally, is this idiomatic F#?

Edit: The code had a mistake in the last line. The result is computed using test_registry rather than tests.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:15:44+00:00Added an answer on May 15, 2026 at 4:15 pm

    One way to avoid the need to write the name of the test explicitly in the code (as a string) would be to use quotations. Instead of creating a list of functions and a list of strings, you can create a list of “quoted” function values. Then you can write code that processes the quotations and gives you everything you need.

    I assume that your tests look roughly as below (functions taking unit and returning some value as a result). The list would be constructed as follows:

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

    Then you can write code like this to get information about test:

    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"
    

    Here is an example how you would use that:

    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)))
    

    Alternatively you could modify the line (*) to produce a function that returns the name of the test and the result, which removes the need for Option.map:

    // 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())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself using the current pattern quite often in my code nowadays var
I'm writing code to emulate a unit of work pattern in my winforms over
At my current workplace, I run across a lot of code that looks similar
I'm concerned that I might be using a code pattern that leaks memory. Here's
I'm currently writing my own PHP framework as a learning exercise using the HMVC
Im trying to implement a UnitofWork pattern using this Scott Allen tutorial My current
Suppose I am building an app using current mysql database tables. I have this
I'm just getting started with the Play Framework 2.0 (using current trunk 2.1-SNAPSHOT, Scala)
I have been building a new application using my current understanding of domain driven
I'm in a ASP.NET application using Windows Authentication. I'm using HttpContext.Current.User.Identity.Name to get the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.