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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:38:31+00:00 2026-06-17T19:38:31+00:00

I cannot find any simple (compared to the try catch method, which is rather

  • 0

I cannot find any simple (compared to the try catch method, which is rather straightforward) way to do this:

try
    Some (line.Split delimiter |> Array.map Int32.Parse)
with
    |_ -> None

A bad (call Parse twice) approach is like this:

let array = line.Split delimiter
let parseSucceed =
    array |> Array.exist (Int32.TryParse >> fst >> not)
          |> not
if parseSucceed then Some (array |> Array.map Int32.Parse) else None

Is there any standard way of doing such tasks? Do I have to write up a recursive function to handle this?

What if the input is not an array but a stream/sequence?

Update:

@Daniel’s method is great.

module Seq =
    let tryCatch myFun (x: seq<_>) =
        let rec loop acc (e: IEnumerator<_>) =
            if e.MoveNext() then
                match myFun e.Current with
                | Some v -> loop (v::acc) e
                | None   -> (false, acc |> List.rev |> List.toSeq)
            else
                (true, acc |> List.rev |> List.toSeq)
        use e = x.GetEnumerator()
        loop [] e

let parse x =
    printf "%d " x // log when the function is called.
    if x > 3 then None else Some x

let tt myFun x =
    let y = Seq.tryCatch myFun x
    if fst y then
        printfn "yes %A" (y |> snd |> Seq.toArray)
    else
        printfn "no %A" (y |> snd |> Seq.toArray)

tt parse [| 0;2;1;2;4;1;2 |]
tt parse [| 0;2;1;2 |]

> 
0 2 1 2 4 no [|0; 2; 1; 2|]  // parse is called minimum number of times
0 2 1 2 yes [|0; 2; 1; 2|]
  • 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-17T19:38:33+00:00Added an answer on June 17, 2026 at 7:38 pm

    You could do:

    let fields = line.Split(delimiter) 
    let parsedFields = 
      fields
        |> Seq.map Int32.TryParse
        |> Seq.takeWhile fst
        |> Seq.map snd
        |> Seq.toArray
    if parsedFields.Length = fields.Length 
    then Some parsedFields
    else None
    

    Or, if you want something a bit more reusable:

    module Seq =
      let tryMap f (s: seq<_>) =
        use e = s.GetEnumerator()
        let res = ResizeArray()
        let rec loop() =
          if e.MoveNext() then
            match f e.Current with
            | Some x -> res.Add(x); loop()
            | None -> None
          else Some(seq res)
        loop()
    
    type System.Int32 with
      static member tryParse s =
        match Int32.TryParse(s) with
        | true, i -> Some i
        | _ -> None
    
    //Usage
    line.Split(delimiter) |> Seq.tryMap Int32.tryParse
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As simple as it seems, I simply CANNOT find ANY way to do that
This is a simple problem but cannot find any working solution for it. I
This is something simple yet I cannot find any answer searching google ! I
This is a really simple question, but I cannot find any mention of this,
I have looked everywhere and cannot find any simple solution to this. It must
i cannot find any documentation of this project
This is but a curious question. I cannot find any useful links from Google
This might be a dummy question, but I cannot find any clue in all
I've searched all over for an answer to this, but cannot find any clear
I have a very simple task, but I cannot find any solution. I have

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.