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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:56:35+00:00 2026-05-28T07:56:35+00:00

Here is my attempt which is NOT tail call optimized because I need to

  • 0

Here is my attempt which is NOT tail call optimized because I need to dispose the enumerator:

let Group func seed (items : seq<'t>) = 
    let rec some (i : IEnumerator<'t>) state = seq {
        try
            if i.MoveNext()
            then
                let newstate, iscomplete = func (i.Current) state
                if iscomplete
                then 
                    yield newstate
                    yield! some i newstate
            else
                yield state
        finally
            i.Dispose() }

    some (items.GetEnumerator ()) seed

And here is example usage:

let buffer maxBufferSize items =
    Group (fun item state ->
        let newstate = [ item ] |> List.append state
        if newstate.Length >= maxBufferSize
        then (newstate, true)
        else (newstate, false)) List.empty items

If I can avoid using the enumerator (i.e. Seq.head AND Seq.tail) I could make it work but without Seq.tail I am at a loss. I am really hoping to make this work with generic sequences..

And FYI I know this code won’t work in its current state because I would end up disposing the enumerator multiple times.

  • 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-28T07:56:35+00:00Added an answer on May 28, 2026 at 7:56 am

    You can move the try .. finally block from the inner some function (where it is entered during every iteration) to the main function. Then the inner recursive function some becomes tail-recursive:

    let Group func seed (items : seq<'t>) =  
        // The handling of exceptions is done by the caller, 
        // so 'some' does not need to handle exceptions...
        let rec some (i : IEnumerator<'t>) state = seq { 
            if i.MoveNext() 
            then 
                let newstate, iscomplete = func (i.Current) state 
                if iscomplete then  
                    yield newstate 
                    // Recursive call in tail-call position
                    yield! some i newstate 
            else 
                yield state }
    
        // Return a sequence that wraps the obtains the IEnumerator
        // and guarantees that it gets disposed if 'some' fails
        seq {
          let i = items.GetEnumerator ()
          try 
              // This is still not-tail recursive
              yield! some i seed 
          finally
              i.Dispose() }
    

    Or even better, you can implement the sequence that is returned from Group using the use construct:

        seq {
            use i = items.GetEnumerator ()
            // This is still not-tail recursive
            yield! some i seed } 
    

    Actually, I think this is more correct than your original code, because it calls the Dispose method only once. In your version, it would be called once for every time the execution enters some (which would depend on the number of elements processed before an exception happens).

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

Sidebar

Related Questions

How to dynamically change the timeout of the cycle plugin here is my attempt
I'm trying to calculate time blocks from given date range. Here's my attempt: <?php
Here is my first attempt at validating XML with XSD. The XML file to
Here's the flow I'm looking for for authentication: Attempt to pull in the user's
Here in stackoverflow, if you started to make changes then you attempt to navigate
I'm getting this error: -[NSCFArray insertObject:atIndex:]: attempt to insert nil Here is the .m
I'm after a simple stored procedure to drop tables. Here's my first attempt: CREATE
After my last, failed, attempt at asking a question here I'm trying a more
Here's a basic regex technique that I've never managed to remember. Let's say I'm
I'm trying to install Plone-4.1.2 on a RH Enterprise server which I am not

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.