1 people like it.

Bank OCR

User Story 1 from the Bank OCR Kata: http://codingdojo.org/cgi-bin/wiki.pl?KataBankOCR

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
let zeroToNineText = """
 _     _  _     _  _  _  _  _ 
| |  | _| _||_||_ |_   ||_||_|
|_|  ||_  _|  | _||_|  ||_| _|                          
"""

let getDigits (source:string) len =
    let lines = source.Split([|'\n'|]).[1..3]
    [for digit in 0..len-1 ->
        let index = digit*3
        [for line in lines -> line.[index..index+2]]
    ]

let zeroToNine = getDigits zeroToNineText 10

let toNumber text =
    getDigits text 9
    |> List.fold (fun acc digit ->
        let n = zeroToNine |> List.findIndex ((=) digit)
        acc * 10 + n
    ) 0
        
let accountText = """
    _  _  _  _  _  _     _ 
|_||_|| || ||_   |  |  ||_ 
  | _||_||_||_|  |  |  | _| 
"""

let n = toNumber accountText
val zeroToNineText : string

Full name: Script.zeroToNineText
val getDigits : source:string -> len:int -> string list list

Full name: Script.getDigits
val source : string
Multiple items
val string : value:'T -> string

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

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val len : int
val lines : string []
System.String.Split([<System.ParamArray>] separator: char []) : string []
System.String.Split(separator: string [], options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], count: int) : string []
System.String.Split(separator: string [], count: int, options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], count: int, options: System.StringSplitOptions) : string []
val digit : int
val index : int
val line : string
val zeroToNine : string list list

Full name: Script.zeroToNine
val toNumber : text:string -> int

Full name: Script.toNumber
val text : string
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val fold : folder:('State -> 'T -> 'State) -> state:'State -> list:'T list -> 'State

Full name: Microsoft.FSharp.Collections.List.fold
val acc : int
val digit : string list
val n : int
val findIndex : predicate:('T -> bool) -> list:'T list -> int

Full name: Microsoft.FSharp.Collections.List.findIndex
val accountText : string

Full name: Script.accountText
val n : int

Full name: Script.n

More information

Link:http://fssnip.net/kC
Posted:10 years ago
Author:Phillip Trelford
Tags: kata