Press CTRL+C or CMD+C to copy the selected text and close this dialog.
Tweet
47 people like it. Like the snippet!
Command pattern for redo-undo scenario.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
type Command = { Redo: unit->unit; Undo: unit->unit } let result = ref 7 let add n = { Redo = (fun _ -> result:= !result + n); Undo = (fun _ -> result := !result - n) } let minus n = { Redo = (fun _ -> result:= !result - n); Undo = (fun _ -> result := !result + n) } let cmd = (add 3) printfn "current state = %d" !result cmd.Redo() printfn "after redo: %d" !result cmd.Undo() printfn "after undo: %d" !result