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

  • SEARCH
  • Home
  • 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 861657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:00:48+00:00 2026-05-15T09:00:48+00:00

I’m just starting to learn F#. I wrote this F#/ADO.NET code last night. In

  • 0

I’m just starting to learn F#. I wrote this F#/ADO.NET code last night.
In what ways would you improve the syntax – make it feel like idiomatic F#?

    let cn = new OleDbConnection(cnstr)
    let sql = "SELECT * FROM People"
    let da = new OleDbDataAdapter(new OleDbCommand(sql, cn))
    let ds = new DataSet()
    cn.Open()
    let i = da.Fill(ds)
    let rowCol = ds.Tables.[0].Rows
    let rowCount = rowCol.Count
    printfn "%A" rowCount

    for i in 0 .. (rowCount - 1) do
        let row:DataRow = rowCol.[i]
        printfn "%A" row.["LastName"]

Note: I did find the syntax checker did not like
rowCol.[i].[“LastName”]
What is the proper way to handle dual-indexers? I had to break up the code over two lines.

Also If I hadn’t gone down the DataSet route and used a SqlDataReader that loaded its data into F# records. What collection structure should I use for containing the records?
The standard .NET List<>?

  • 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-15T09:00:49+00:00Added an answer on May 15, 2026 at 9:00 am

    The key part of your code deals with .NET API that is not functional, so there is no way to make this part of the code particularly more idiomatic or nicer. However, the key thing in functional programming is abstraction, so you can hide this (ugly) code into some idiomatic and reusable function.

    For representing collections of data in F#, you can either use standard F# list type (which is good for functional data processing) or seq<'a> (which is standard .NET IEnumerable<'a> under the cover), which works nicely when working with other .NET libraries.

    Depending on how you access database elsewhere in your code, the following could work:

    // Runs the specified query 'sql' and formats rows using function 'f'
    let query sql f = 
      // Return a sequence of values formatted using function 'f'
      seq { use cn = new OleDbConnection(cnstr) // will be disposed 
            let da = new OleDbDataAdapter(new OleDbCommand(sql, cn)) 
            let ds = new DataSet() 
            cn.Open() 
            let i = da.Fill(ds) 
            // Iterate over rows and format each row
            let rowCol = ds.Tables.[0].Rows 
            for i in 0 .. (rowCount - 1) do 
                yield f (rowCol.[i]) }
    

    Now you can use the query function to write your original code roughly like this:

    let names = query "SELECT * FROM People" (fun row -> row.["LastName"])
    printfn "count = %d" (Seq.count names)
    for name in names do printfn "%A" name
    
    // Using 'Seq.iter' makes the code maybe nicer 
    // (but that's a personal preference):
    names |> Seq.iter (printfn "%A")
    

    Another example you could write is:

    // Using records to store the data
    type Person { LastName : string; FirstName : string }
    let ppl = query "SELECT * FROM People" (fun row -> 
      { FirstName = row.["FirstName"]; LastName = row.["LastName"]; })
    
    let johns = ppl |> Seq.filter (fun p -> p.FirstName = "John")
    

    BTW: Regarding the suggestion by Mau I wouldn’t use higher-order functions excessively if there is a more direct way to write the code using language constructs such as for. The example with iter above is simple enough and some people will find it more readable, but there is no general rule…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words

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.