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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:05:07+00:00 2026-05-19T05:05:07+00:00

I am looking for a way to create a sequence consisting of every nth

  • 0

I am looking for a way to create a sequence consisting of every nth element of another sequence, but don’t seem to find a way to do that in an elegant way. I can of course hack something, but I wonder if there is a library function that I’m not seeing.

The sequence functions whose names end in -i seem to be quite good for the purpose of figuring out when an element is the nth one or (multiple of n)th one, but I can only see iteri and mapi, none of which really lends itself to the task.

Example:

let someseq = [1;2;3;4;5;6]
let partial = Seq.magicfunction 3 someseq

Then partial should be [3;6]. Is there anything like it out there?

Edit:

If I am not quite as ambitious and allow for the n to be constant/known, then I’ve just found that the following should work:

let rec thirds lst =
    match lst with
    | _::_::x::t -> x::thirds t // corrected after Tomas' comment
    | _ -> []

Would there be a way to write this shorter?

  • 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-19T05:05:08+00:00Added an answer on May 19, 2026 at 5:05 am

    You can get the behavior by composing mapi with other functions:

    let everyNth n seq = 
      seq |> Seq.mapi (fun i el -> el, i)              // Add index to element
          |> Seq.filter (fun (el, i) -> i % n = n - 1) // Take every nth element
          |> Seq.map fst                               // Drop index from the result
    

    The solution using options and choose as suggested by Annon would use only two functions, but the body of the first one would be slightly more complicated (but the principle is essentially the same).

    A more efficient version using the IEnumerator object directly isn’t too difficult to write:

    let everyNth n (input:seq<_>) = 
      seq { use en = input.GetEnumerator()
            // Call MoveNext at most 'n' times (or return false earlier)
            let rec nextN n = 
              if n = 0 then true
              else en.MoveNext() && (nextN (n - 1)) 
            // While we can move n elements forward...
            while nextN n do
              // Retrun each nth element
              yield en.Current }
    

    EDIT: The snippet is also available here: http://fssnip.net/1R

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

Sidebar

Related Questions

I'm looking for a way to create valid UTF-16 JavaScript escape sequence characters (including
I am looking for a way to create a program in unmanaged c++ that
I was looking for a way to create an IP filter, so that I
I'm looking for a way to create a graphics file (I don't really mind
I am looking for a way to create a HashMap that maps to mixed
I was looking for a way to create modules that could easily be included
I'm looking for a way to create an online form that will update an
I am looking for a way to create dynamic preferences where I don't need
I am looking for a way to create an nssearchfield that behaves as follows:
I looking for a way to create Delta Diff Patches of Large Binary Files

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.