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

  • Home
  • SEARCH
  • 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 176923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:57:48+00:00 2026-05-11T13:57:48+00:00

( This question about refactoring F# code got me one down vote, but also

  • 0

(This question about refactoring F# code got me one down vote, but also some interesting and useful answers. And 62 F# questions out of the 32,000+ on SO seems pitiful, so I’m going to take the risk of more disapproval!)

I was trying to post a bit of code on a blogger blog yesterday, and turned to this site, which I had found useful in the past. However, the blogger editor ate all the style declarations, so that turned out to be a dead end.

So (like any hacker), I thought ‘how hard can it be?’ and rolled my own in <100 lines of F#.

Here is the ‘meat’ of the code, which turns an input string into a list of ‘tokens’. Note that these tokens aren’t to be confused with the lexing/parsing-style tokens. I did look at those briefly, and though I hardly understood anything, I did understand that they would give me only tokens, whereas I want to keep my original string.

The question is: is there a more elegant way of doing this? I don’t like the n re-definitions of s required to remove each token string from the input string, but it’s difficult to split the string into potential tokens in advance, because of things like comments, strings and the #region directive (which contains a non-word character).

//Types of tokens we are going to detect type Token =      | Whitespace of string     | Comment of string     | Strng of string     | Keyword of string     | Text of string     | EOF  //turn a string into a list of recognised tokens let tokenize (s:String) =      //this is the 'parser' - should we look at compiling the regexs in advance?     let nexttoken (st:String) =          match st with         | st when Regex.IsMatch(st, '^\s+') -> Whitespace(Regex.Match(st, '^\s+').Value)         | st when Regex.IsMatch(st, '^//.*?\r?\n') -> Comment(Regex.Match(st, '^//.*?\r?\n').Value) //this is double slash-style comments         | st when Regex.IsMatch(st, '^/\*(.|[\r?\n])*?\*/') -> Comment(Regex.Match(st, '^/\*(.|[\r?\n])*?\*/').Value) // /* */ style comments http://ostermiller.org/findcomment.html         | st when Regex.IsMatch(st, @'^''([^''\\]|\\.|'''')*''') -> Strng(Regex.Match(st, @'^''([^''\\]|\\.|'''')*''').Value) // unescaped = '([^'\\]|\\.|'')*' http://wordaligned.org/articles/string-literals-and-regular-expressions         | st when Regex.IsMatch(st, '^#(end)?region') -> Keyword(Regex.Match(st, '^#(end)?region').Value)         | st when st <> '' ->                  match Regex.Match(st, @'^[^''\s]*').Value with //all text until next whitespace or quote (this may be wrong)                 | x when iskeyword x -> Keyword(x)  //iskeyword uses Microsoft.CSharp.CSharpCodeProvider.IsValidIdentifier - a bit fragile...                 | x -> Text(x)         | _ -> EOF      //tail-recursive use of next token to transform string into token list     let tokeneater s =          let rec loop s acc =              let t = nexttoken s             match t with             | EOF -> List.rev acc //return accumulator (have to reverse it because built backwards with tail recursion)             | Whitespace(x) | Comment(x)              | Keyword(x) | Text(x) | Strng(x) ->                  loop (s.Remove(0, x.Length)) (t::acc)  //tail recursive         loop s []      tokeneater s 

(If anyone is really interested, I am happy to post the rest of the code)

EDIT Using the excellent suggestion of active patterns by kvb, the central bit looks like this, much better!

let nexttoken (st:String) =      match st with     | Matches '^\s+' s -> Whitespace(s)     | Matches '^//.*?\r?(\n|$)' s -> Comment(s) //this is double slash-style comments     | Matches '^/\*(.|[\r?\n])*?\*/' s -> Comment(s)  // /* */ style comments http://ostermiller.org/findcomment.html     | Matches @'^@?''([^''\\]|\\.|'''')*''' s -> Strng(s) // unescaped regexp = ^@?'([^'\\]|\\.|'')*' http://wordaligned.org/articles/string-literals-and-regular-expressions     | Matches '^#(end)?region' s -> Keyword(s)      | Matches @'^[^''\s]+' s ->   //all text until next whitespace or quote (this may be wrong)             match s with             | IsKeyword x -> Keyword(s)             | _ -> Text(s)     | _ -> EOF 
  • 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. 2026-05-11T13:57:49+00:00Added an answer on May 11, 2026 at 1:57 pm

    I’d use an active pattern to encapsulate the Regex.IsMatch and Regex.Match pairs, like so:

    let (|Matches|_|) re s =   let m = Regex(re).Match(s)   if m.Success then     Some(Matches (m.Value))   else     None 

    Then your nexttoken function can look like:

    let nexttoken (st:String) =            match st with           | Matches '^s+' s -> Whitespace(s)           | Matches '^//.*?\r?\n' s -> Comment(s)   ... 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 190k
  • Answers 190k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is one of those things where Cocoa does all… May 12, 2026 at 6:04 pm
  • Editorial Team
    Editorial Team added an answer Check out the accepted answer to this similar question: How… May 12, 2026 at 6:04 pm
  • Editorial Team
    Editorial Team added an answer To lookup a Remote Interface of a Session Bean with… May 12, 2026 at 6:03 pm

Related Questions

I was refactoring some code in a web application today and came across something
My studio has a large codebase that has been developed over 10+ years. The
I have an existing VS 2005 Std .NET Compact Framework application that I want
I'm writing a microformats parser in C# and am looking for some refactoring advice.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.