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: 
30: 
31: 
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]]
        |> String.concat "\n"
    ]

let zeroToNine = getDigits zeroToNineText 10

let toNumber text =
    (getDigits text 9, (0,1))
    ||> List.foldBack (fun digit (result,tens) ->
        let n = zeroToNine |> List.findIndex ((=) digit)
        result + n * tens, tens * 10
    )
    |> fst
        
let accountText = """
    _  _  _  _  _  _     _ 
|_||_|| || ||_   |  |  ||_ 
  | _||_||_||_|  |  |  | _| 
"""

let n = toNumber accountText
val zeroToNineText : string

Full name: Script.zeroToNineText
val getDigits : source:string -> len:int -> string 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
module String

from Microsoft.FSharp.Core
val concat : sep:string -> strings:seq<string> -> string

Full name: Microsoft.FSharp.Core.String.concat
val zeroToNine : string 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 foldBack : folder:('T -> 'State -> 'State) -> list:'T list -> state:'State -> 'State

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

Full name: Microsoft.FSharp.Collections.List.findIndex
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
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