1 people like it.

Incremental auto-completion

Returns subsets of strings which match a specified prefix. The prefix is built incrementally by repeatedly calling a search function.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
type AutoCompleteFunction = AutoCompleteFunction of Lazy<string[]> * (string -> AutoCompleteFunction)
with
    member x.Call s =
        let (AutoCompleteFunction (_, f)) = x
        f s
    
    member x.Strings =
        let (AutoCompleteFunction (s, _)) = x
        s.Force()

let initAutoComplete (names : string seq) =
    let names =
        names
        |> Seq.sort
        |> Array.ofSeq

    let rec getByPrefix range prefix =
        let range =
            match range with
            | Some (first, count) ->
                let first' =
                    seq { first .. first + count - 1 }
                    |> Seq.tryFindIndex (fun pos -> names.[pos].StartsWith prefix)
            
                match first' with
                | None -> None
                | Some first' ->
                    let after =
                        seq { first' .. first + count - 1 }
                        |> Seq.tryFindIndex (fun pos -> not <| names.[pos].StartsWith prefix)
                    match after with
                    | None -> Some (first', first + count - first')
                    | Some after -> Some (first', after - first')
            | None -> None

        let matched_names =
            lazy
                match range with
                | None -> Array.empty
                | Some (first, count) ->
                    Array.sub names first count
        
        let cont next =
            getByPrefix range (prefix + next)

        (matched_names, cont) |> AutoCompleteFunction

    let all_range = Some(0, Array.length names)
    getByPrefix all_range ""
Multiple items
union case AutoCompleteFunction.AutoCompleteFunction: Lazy<string []> * (string -> AutoCompleteFunction) -> AutoCompleteFunction

--------------------
type AutoCompleteFunction =
  | AutoCompleteFunction of Lazy<string []> * (string -> AutoCompleteFunction)
  member Call : s:string -> AutoCompleteFunction
  member Strings : string []

Full name: Script.AutoCompleteFunction
Multiple items
active recognizer Lazy: Lazy<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.( |Lazy| )

--------------------
type Lazy<'T> = System.Lazy<'T>

Full name: Microsoft.FSharp.Control.Lazy<_>
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val x : AutoCompleteFunction
member AutoCompleteFunction.Call : s:string -> AutoCompleteFunction

Full name: Script.AutoCompleteFunction.Call
val s : string
val f : (string -> AutoCompleteFunction)
member AutoCompleteFunction.Strings : string []

Full name: Script.AutoCompleteFunction.Strings
val s : Lazy<string []>
member System.Lazy.Force : unit -> 'T
val initAutoComplete : names:seq<string> -> AutoCompleteFunction

Full name: Script.initAutoComplete
val names : seq<string>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val names : string []
module Seq

from Microsoft.FSharp.Collections
val sort : source:seq<'T> -> seq<'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Seq.sort
module Array

from Microsoft.FSharp.Collections
val ofSeq : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Array.ofSeq
val getByPrefix : ((int * int) option -> string -> AutoCompleteFunction)
val range : (int * int) option
val prefix : string
union case Option.Some: Value: 'T -> Option<'T>
val first : int
val count : int
val first' : int option
val tryFindIndex : predicate:('T -> bool) -> source:seq<'T> -> int option

Full name: Microsoft.FSharp.Collections.Seq.tryFindIndex
val pos : int
union case Option.None: Option<'T>
val first' : int
val after : int option
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val after : int
val matched_names : Lazy<string []>
val empty<'T> : 'T []

Full name: Microsoft.FSharp.Collections.Array.empty
val sub : array:'T [] -> startIndex:int -> count:int -> 'T []

Full name: Microsoft.FSharp.Collections.Array.sub
val cont : (string -> AutoCompleteFunction)
val next : string
val all_range : (int * int) option
val length : array:'T [] -> int

Full name: Microsoft.FSharp.Collections.Array.length
Raw view Test code New version

More information

Link:http://fssnip.net/5G
Posted:13 years ago
Author:Johann Deneux
Tags: search