Skip to content

yallop/metaocaml-charmatch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

charmatch: pattern-matching generation for characters

Charmatch test

The following call:

module C = Set.Make(Char)
.< fun x -> .~(ifmem .<x>.
                [C.of_list ['a';'b';'c';'e';'f';'g';'z'], .<1>.;
                 C.of_list ['1';'2';'3';'9'], .<2>.]
                ~otherwise:.<3>.) >.

either generates compact code for matching characters:

fun x ->
  match x with
   | 'a'..'c' | 'e'..'g' | 'z' -> 1
   | '1'..'3' | '9' -> 2
   | _ -> 3

or, depending on the options passed to ifmem, generates equivalent (but typically faster) code based on a table lookup:

fun x ->
  match Char.code
          (String.unsafe_get
             "\002...\002\001\001\001\002\002...\002\002\000\000\000\002\000\000\000\002\002...\002\002"
             (Char.code x))
  with
  | 0 -> 1
  | 1 -> 2
  | _ -> 3

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages