2009-11-29 13 views
0

Je reçois l'erreur suivante dans diff avec un gribouillage rouge sous Sous-ensemble. Y a-t-il une sorte d'annotation de type que je peux ajouter à la correspondance SubSet, donc je n'ai pas besoin d'utiliser fst et snd? Si non, y a-t-il une intention de supporter cette syntaxe?F # correspond au modèle actif en tant que tuple étendu

type Range = {min : int64; max : int64} 

let (|Before|After|BeforeOverlap|AfterOverlap|SuperSet|SubSet|) (x, y) = 
    if x.min > y.max then After 
    elif x.min >= y.min then 
     if x.max <= y.max then SubSet 
     else AfterOverlap 
    elif x.max < y.min then Before 
    elif x.max <= y.max then BeforeOverlap 
    else SuperSet 

let useOldx x xe ye =() 

let diff (xe:IEnumerator<Range>) (ye:IEnumerator<Range>) = 
    match xe.Current, ye.Current with 
    | After as tuple ->() 
    | Before as t -> if xe.MoveNext() then useOldx (fst t) xe ye 
    | SuperSet as t -> 
     let x, y = t 
     if xe.MoveNext() then useOldx x xe ye 
    | SubSet as x, y -> if xe.MoveNext() then useOldx x xe ye 
    | _ ->() 

Répondre

6

Vous pouvez le faire:

| (x,y) & SubSet -> if xe.MoveNext() then useOldx x xe ye 

ou vous pouvez faire ceci:

open System.Collections.Generic 

type Range = {min : int64; max : int64} 

let (|Before|After|BeforeOverlap|AfterOverlap|SuperSet|SubSet|) (x, y) = 
    if x.min > y.max then After 
    elif x.min >= y.min then 
     if x.max <= y.max then SubSet(x,y) 
     else AfterOverlap 
    elif x.max < y.min then Before 
    elif x.max <= y.max then BeforeOverlap 
    else SuperSet 

let useOldx x xe ye =() 

let diff (xe:IEnumerator<Range>) (ye:IEnumerator<Range>) = 
    match xe.Current, ye.Current with 
    | After as tuple ->() 
    | Before as t -> if xe.MoveNext() then useOldx (fst t) xe ye 
    | SuperSet as t -> 
     let x, y = t 
     if xe.MoveNext() then useOldx x xe ye 
    | SubSet(x, y) -> if xe.MoveNext() then useOldx x xe ye 
    | _ ->() 
+0

Impressionnant, Merci! – gradbot

+1

Le motif actif est un concept si simple, mais il facilite vraiment la décomposition d'un problème complexe en parties de plus en plus petites et il est lisible! – gradbot