Skip to content

Instantly share code, notes, and snippets.

@ruxo
Last active February 9, 2023 23:14
Show Gist options
  • Select an option

  • Save ruxo/9b79350eb2676bad1fb5 to your computer and use it in GitHub Desktop.

Select an option

Save ruxo/9b79350eb2676bad1fb5 to your computer and use it in GitHub Desktop.
F# String Reference
type StrRef =
{ str: string
start: int
length: int }
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module StrRef =
let substring n s = { s with length=(min s.length n) }
let lastPos sr = sr.start + sr.length - 1
let iterate sr =
seq {
for i = sr.start to (lastPos sr) do
yield sr.str.[i]
}
let equals (s: string) sr = s.Length = sr.length && (Seq.forall id ((=) <!> iterate sr <*> s))
let skip n sr = { sr with start = (min (lastPos sr) (sr.start+n))
length= (max 0 (sr.length - n))}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment