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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:52:32+00:00 2026-06-06T18:52:32+00:00

I have a function witch is written in an imperative style and cant get

  • 0

I have a function witch is written in an imperative style and cant get my head around on how to convert it to a more robust functional approach.

The function takes a seq of strings and returns a seq of tuples where each tuple consists of the 2,7,12,.. and 5,10,15,.. item from the input.

Example:

Input = { “Lorem”, “ipsum”, “dolor”, “set”, “amet”, “consectetuer”, “adipiscing”, “elit”, “Aenean”, “commodo”, “ligula”, “eget”, “dolor”, “Aenean”, “massa” }

Ouput = { (“ipsum”, “amet”), (“adipiscing”, “commodo”), (“eget”, “massa”) }

let convert (input : seq<string>) : seq<(string * string)> =
    let enum = input.GetEnumerator()
    let index = ref 0
    let first = ref ""
    let second = ref ""

    seq {
        while enum.MoveNext() do
            let modIndex = !index % 5
            index := !index + 1

            if (modIndex % 2 = 0 && !first = "") then first := enum.Current
            if (modIndex % 5 = 0 && !second = "") then second := enum.Current

            if modIndex = 0  then
                let result = (!first, !second)
                first := ""
                second := ""
                yield result
    }

Any help or tip for a starting point is appreciated.

  • 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-06-06T18:52:34+00:00Added an answer on June 6, 2026 at 6:52 pm

    I do not completely understand the behaviour you want – what is the algorithm for generating indices that you want to pair? Anyway, one nice functional solution is to take the elements you want to pair separately and then combine them using Seq.zip.

    You can use Seq.mapi to add indices to the values and then use Seq.choose to get the values with the right index (and skip all other values). For hardcoded indices, you can write something like:

    let indexed = input |> Seq.mapi (fun i s -> i, s)
    Seq.zip 
      (indexed |> Seq.choose (fun (i, v) -> if i=1 || i=6 || i=11 then Some v else None))
      (indexed |> Seq.choose (fun (i, v) -> if i=4 || i=9 || i=14 then Some v else None))
    

    I used your numbers -1 because the indices are from 0 – so the above gives you the results you wanted. The second series looks like multiples of 5, so perhaps you wanted i%5 = 4 to generate second elements:

    let indexed = input |> Seq.mapi (fun i s -> i, s)
    Seq.zip 
      (indexed |> Seq.choose (fun (i, v) -> if i=1 || i=6 || i=11 then Some v else None))
      (indexed |> Seq.choose (fun (i, v) -> if i%5 = 4 then Some v else None))
    

    I still don’t see the general mechanism for generating the first elements though!

    EDIT One more idea – is the first sequence generated by i*5 + 2 and the second by i*5? In that case, your example is wrong, but you could write it like this:

    let indexed = input |> Seq.mapi (fun i s -> i, s)
    Seq.zip 
      (indexed |> Seq.choose (fun (i, v) -> if i%5 = 2 then Some v else None))
      (indexed |> Seq.choose (fun (i, v) -> if i%5 = 0 then Some v else None))
    

    … or if you want to make the code shroter, you can refactor:

    let filterNthElements div rem = 
      input |> Seq.mapi (fun i s -> i, s)
            |> Seq.choose (fun (i, v) -> if i%div = rem then Some v else None)
    
    Seq.zip (filterNthElements 5 2) (filterNthElements 5 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a quick C++ DLL in Visual Studio, with a single function
I'm trying to get a simple delete every pointer in my vector/list/... function written
I have written a function in my program that allows me to retrieve strings
I have written this ajaxform function with a success function which is being called
I have written a function that changes texts like &rsaquo; , &laquo; to symbols
I have a function I've written that was initially supposed to take a string
I have a function written in pl/pgsql which works in the same way as
I have a function in java which is written with a recursive algorithm that
I have written the following function to search an multi-dimensional array by key, but
I have a function written in PostgreSQL, to go over a large table and

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.