70 people like it.
Like the snippet!
Chain of responsibility
The following sample wants to make sure the person’s age is between 18 and 65, weight is no more than 200 and tall enough (>120).
1: type Record = { 2: Name : string; 3: Age : int; 4: Weight: float; 5: Height: float; 6: } 7: 8: let ChainOfResponsibility() = 9: let validAge (record:Record) = 10: record.Age < 65 && record.Age > 18 11: let validWeight (record:Record) = 12: record.Weight < 200. 13: let validHeight (record:Record) = 14: record.Height > 120. 15: 16: let check (f:Record->bool) (record:Record, result:bool) = 17: if result=false then (record, false) 18: else (record, f(record)) 19: 20: let chainOfResponsibility = check(validAge) >> check(validWeight) >> check(validHeight) 21: 22: let john = { Name = "John"; Age = 80; Weight = 180.; Height=180. } 23: let dan = { Name = "Dan"; Age = 20; Weight = 160.; Height=190. } 24: 25: printfn "john result = %b" ((chainOfResponsibility (john, true)) |> snd) 26: printfn "dan result = %b" ((chainOfResponsibility (dan, true)) |> snd) 27:
Record.Name: string
Multiple items
val string : 'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable<string>
implements: seq<char>
implements: System.Collections.IEnumerable
implements: System.IEquatable<string>
val string : 'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable<string>
implements: seq<char>
implements: System.Collections.IEnumerable
implements: System.IEquatable<string>
Record.Age: int
Multiple items
val int : 'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
type: int<'Measure>
implements: System.IComparable
implements: System.IConvertible
implements: System.IFormattable
implements: System.IComparable<int<'Measure>>
implements: System.IEquatable<int<'Measure>>
inherits: System.ValueType
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
type: int
implements: System.IComparable
implements: System.IFormattable
implements: System.IConvertible
implements: System.IComparable<int>
implements: System.IEquatable<int>
inherits: System.ValueType
val int : 'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
type: int<'Measure>
implements: System.IComparable
implements: System.IConvertible
implements: System.IFormattable
implements: System.IComparable<int<'Measure>>
implements: System.IEquatable<int<'Measure>>
inherits: System.ValueType
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
type: int
implements: System.IComparable
implements: System.IFormattable
implements: System.IConvertible
implements: System.IComparable<int>
implements: System.IEquatable<int>
inherits: System.ValueType
Record.Weight: float
Multiple items
val float : 'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float
--------------------
type float<'Measure> = float
Full name: Microsoft.FSharp.Core.float<_>
type: float<'Measure>
implements: System.IComparable
implements: System.IConvertible
implements: System.IFormattable
implements: System.IComparable<float<'Measure>>
implements: System.IEquatable<float<'Measure>>
inherits: System.ValueType
--------------------
type float = System.Double
Full name: Microsoft.FSharp.Core.float
type: float
implements: System.IComparable
implements: System.IFormattable
implements: System.IConvertible
implements: System.IComparable<float>
implements: System.IEquatable<float>
inherits: System.ValueType
val float : 'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float
--------------------
type float<'Measure> = float
Full name: Microsoft.FSharp.Core.float<_>
type: float<'Measure>
implements: System.IComparable
implements: System.IConvertible
implements: System.IFormattable
implements: System.IComparable<float<'Measure>>
implements: System.IEquatable<float<'Measure>>
inherits: System.ValueType
--------------------
type float = System.Double
Full name: Microsoft.FSharp.Core.float
type: float
implements: System.IComparable
implements: System.IFormattable
implements: System.IConvertible
implements: System.IComparable<float>
implements: System.IEquatable<float>
inherits: System.ValueType
Record.Height: float
val ChainOfResponsibility : unit -> unit
Full name: Snippet.ChainOfResponsibility
Full name: Snippet.ChainOfResponsibility
val validAge : (Record -> bool)
val record : Record
type: Record
implements: System.IEquatable<Record>
implements: System.Collections.IStructuralEquatable
implements: System.IComparable<Record>
implements: System.IComparable
implements: System.Collections.IStructuralComparable
type: Record
implements: System.IEquatable<Record>
implements: System.Collections.IStructuralEquatable
implements: System.IComparable<Record>
implements: System.IComparable
implements: System.Collections.IStructuralComparable
type Record =
{Name: string;
Age: int;
Weight: float;
Height: float;}
Full name: Snippet.Record
type: Record
implements: System.IEquatable<Record>
implements: System.Collections.IStructuralEquatable
implements: System.IComparable<Record>
implements: System.IComparable
implements: System.Collections.IStructuralComparable
{Name: string;
Age: int;
Weight: float;
Height: float;}
Full name: Snippet.Record
type: Record
implements: System.IEquatable<Record>
implements: System.Collections.IStructuralEquatable
implements: System.IComparable<Record>
implements: System.IComparable
implements: System.Collections.IStructuralComparable
val validWeight : (Record -> bool)
val validHeight : (Record -> bool)
val check : ((Record -> bool) -> Record * bool -> Record * bool)
val f : (Record -> bool)
type bool = System.Boolean
Full name: Microsoft.FSharp.Core.bool
type: bool
implements: System.IComparable
implements: System.IConvertible
implements: System.IComparable<bool>
implements: System.IEquatable<bool>
inherits: System.ValueType
Full name: Microsoft.FSharp.Core.bool
type: bool
implements: System.IComparable
implements: System.IConvertible
implements: System.IComparable<bool>
implements: System.IEquatable<bool>
inherits: System.ValueType
val result : bool
type: bool
implements: System.IComparable
implements: System.IConvertible
implements: System.IComparable<bool>
implements: System.IEquatable<bool>
inherits: System.ValueType
type: bool
implements: System.IComparable
implements: System.IConvertible
implements: System.IComparable<bool>
implements: System.IEquatable<bool>
inherits: System.ValueType
val chainOfResponsibility : (Record * bool -> Record * bool)
val john : Record
type: Record
implements: System.IEquatable<Record>
implements: System.Collections.IStructuralEquatable
implements: System.IComparable<Record>
implements: System.IComparable
implements: System.Collections.IStructuralComparable
type: Record
implements: System.IEquatable<Record>
implements: System.Collections.IStructuralEquatable
implements: System.IComparable<Record>
implements: System.IComparable
implements: System.Collections.IStructuralComparable
val dan : Record
type: Record
implements: System.IEquatable<Record>
implements: System.Collections.IStructuralEquatable
implements: System.IComparable<Record>
implements: System.IComparable
implements: System.Collections.IStructuralComparable
type: Record
implements: System.IEquatable<Record>
implements: System.Collections.IStructuralEquatable
implements: System.IComparable<Record>
implements: System.IComparable
implements: System.Collections.IStructuralComparable
val printfn : Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val snd : ('T1 * 'T2) -> 'T2
Full name: Microsoft.FSharp.Core.Operators.snd
Full name: Microsoft.FSharp.Core.Operators.snd
More information
| Link: | http://fssnip.net/7h |
| Posted: | 1 years ago |
| Author: | Tao Liu (website) |
| Tags: | Design Patterns |