1 people like it.

Pattern Matching.fsx

Single Case Active Patterns

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
open System

let (|UpperCaseCount|) (str: string) =
    str |> Seq.filter (fun c -> c = Char.ToUpper(c)) |> Seq.length

let (|LowerCaseCount|) (str: string) =
    str |> Seq.filter (fun c -> c = Char.ToLower(c)) |> Seq.length

let (|SpecialCharacterCount|) (str: string) =
    let specialCharacters = "!£$%^"
    str |> Seq.filter (fun c -> specialCharacters.Contains(c.ToString())) |> Seq.length
    
    
let isValid (str: string) =
    match str with
    | UpperCaseCount 0 -> (false, "Must have at least 1 upper case character")
    | LowerCaseCount 0 -> (false, "Must have at least 1 lower case character")
    | SpecialCharacterCount 0 -> (false, "Must have at least 1 of !£$%^")
    | _ -> (true, "you are good to go")
    
printf "%A" (isValid "foo")
printf "%A" (isValid "Foo")
printf "%A" (isValid "Foo!")    
namespace System
val str : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
module Seq

from Microsoft.FSharp.Collections
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val c : char
type Char =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 1 overload
    static val MaxValue : char
    static val MinValue : char
    static member ConvertFromUtf32 : utf32:int -> string
    static member ConvertToUtf32 : highSurrogate:char * lowSurrogate:char -> int + 1 overload
    static member GetNumericValue : c:char -> float + 1 overload
    ...
  end

Full name: System.Char
Char.ToUpper(c: char) : char
Char.ToUpper(c: char, culture: Globalization.CultureInfo) : char
val length : source:seq<'T> -> int

Full name: Microsoft.FSharp.Collections.Seq.length
Char.ToLower(c: char) : char
Char.ToLower(c: char, culture: Globalization.CultureInfo) : char
val specialCharacters : string
String.Contains(value: string) : bool
Char.ToString() : string
Char.ToString(provider: IFormatProvider) : string
val isValid : str:string -> bool * string

Full name: Script.isValid
active recognizer UpperCaseCount: string -> int

Full name: Script.( |UpperCaseCount| )
active recognizer LowerCaseCount: string -> int

Full name: Script.( |LowerCaseCount| )
active recognizer SpecialCharacterCount: string -> int

Full name: Script.( |SpecialCharacterCount| )
val printf : format:Printf.TextWriterFormat<'T> -> 'T

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

More information

Link:http://fssnip.net/o6
Posted:2 years ago
Author:tamizhvendan
Tags: active-patterns , tryfsharp