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

Ask A Question

Stats

  • Questions 534k
  • Answers 534k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is the way I create Javascript objects in most… May 17, 2026 at 12:46 am
  • Editorial Team
    Editorial Team added an answer Be creative... function coffee($elm, $cur_key=NULL, $level=0, &$push_arr=NULL){ if(!is_array($push_arr)) $pusH_arr =… May 17, 2026 at 12:46 am
  • Editorial Team
    Editorial Team added an answer If you want to do everything programmatically (that is, if… May 17, 2026 at 12:46 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

At my current workplace, I run across a lot of code that looks similar
I am using this to read the current long time pattern in Windows: System.Globalization.DateTimeFormatInfo.CurrentInfo.LongTimePattern
Our current web application is using SQL Server, we have a requirement for support
We're developing a large desktop application using Windows Presentation Foundation. Currently, the application is
I'm currently writing an AI for a game that is written in c++. The
We currently have a custom inventory system used to track different assets. The items
I'm continuing to struggle with the MVVM pattern and, in attempting to create a
I am trying to figure out how to correctly and efficiently unit test my
I am exploring a pattern where you can save a global reference to the
In a current (C#) project we have a 3rd party assembly that contains a

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.