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 4592902
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:36:23+00:00 2026-05-21T22:36:23+00:00

Writing code in F# in most cases results in very condense an intuitive work.

  • 0

Writing code in F# in most cases results in very condense an intuitive work. This piece of code looks somehow imperative and inconvenient to me.

  • times is an array of float values

Lines inside the file times.csv always look like that:

Mai 06 2011 05:43:45 nachm.,00:22.99
Mai 04 2011 08:59:12 nachm.,00:22.73
Mai 04 2011 08:58:27 nachm.,00:19.38
Mai 04 2011 08:57:54 nachm.,00:18.00
  • average generates an average of the values, dropping the lowest and highest time
  • getAllSubsetsOfLengthN creates a sequence of all consecutive subsets of length n. Is there a ‘nicer’ solution to that? Or does already exist something like that inside the F# core?
  • bestAverageOfN finds the lowest average of all the subsets

let times =
    File.ReadAllLines "times.csv"
    |> Array.map (fun l -> float (l.Substring((l.LastIndexOf ':') + 1)))
let average set =
    (Array.sum set - Array.min set - Array.max set) / float (set.Length - 2)
let getAllSubsetsOfLengthN n (set:float list) =
    seq { for i in [0 .. set.Length - n] -> set
                                            |> Seq.skip i
                                            |> Seq.take n }
let bestAverageOfN n =
    times
    |> Array.toList
    |> getAllSubsetsOfLengthN n
    |> Seq.map (fun t -> t
                         |> Seq.toArray
                         |> average)
    |> Seq.min

What I am looking for are nicer, shorter or easier solutions. Every useful post will be upvoted, of course 🙂

  • 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-21T22:36:24+00:00Added an answer on May 21, 2026 at 10:36 pm

    Without much thinking, there are some basic functional refactorings you can make. For example, in the calculation of bestAverageOfN, you can use function composition:

    let bestAverageOfN n =
        times
        |> Array.toList
        |> getAllSubsetsOfLengthN n
        |> Seq.map (Seq.toArray >> average)
        |> Seq.min
    

    Other than this and the suggestion by desco, I don’t think there is anything I would change. If you don’t use your special average function anywhere in the code, you could write it inline as a lambda function, but that really depends on your personal preferences.

    Just for the sake of generality, I would probably make times an argument of bestAverageOfN:

    let bestAverageOfN n times =
        times
        |> Seq.windowed n
        |> Seq.map (fun set ->
               (Array.sum set - Array.min set - Array.max set) / float (set.Length - 2))
        |> Seq.min
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing code that looks similar to this: public IEnumerable<T> Unfold<T>(this T seed) {
In most of the videos, I see expert JQuery developers writing complete code for
I frequently find myself writing code like this: List<int> list = new List<int> {
I'm writing code like this, doing a little quick and dirty timing: var sw
Writing Scala code, I regularly encounter cases where I have processor functions that operate
When writing code do you consciously program defensively to ensure high program quality and
After writing code to populate textboxes from an object, such as: txtFirstName.Text = customer.FirstName;
While writing code in a file that would comprise of PHP, HTML, CSS &
While writing code, it is pretty common to request a page with an appended
When writing code in an Eclipse project, I'm usually quite messy and undisciplined in

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.