97 people like it.

Chain of responsibility II

Unlike the previous chain of responsibility, this version use the pipeline to chain responsibilities.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
let chainTemplate processFunction canContinue s = 
    if canContinue s then 
        processFunction s
    else s

let canContinueF _ = true
let processF x = x + 1

let chainFunction = chainTemplate processF canContinueF   //combine two functions to get a chainFunction
let s = 1 |> chainFunction |> chainFunction

printfn "%A" s
val chainTemplate : processFunction:('a -> 'a) -> canContinue:('a -> bool) -> s:'a -> 'a

Full name: Script.chainTemplate
val processFunction : ('a -> 'a)
val canContinue : ('a -> bool)
val s : 'a
val canContinueF : 'a -> bool

Full name: Script.canContinueF
val processF : x:int -> int

Full name: Script.processF
val x : int
val chainFunction : (int -> int)

Full name: Script.chainFunction
val s : int

Full name: Script.s
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Raw view New version

More information

Link:http://fssnip.net/7z
Posted:5 years ago
Author:Tao Liu
Tags: design patterns