1 people like it.

Official SkillsMatter 2013 F# Tutorials Programming with the Stars Code Snippet

SkillsMatter 2013 F# Tutorials Programming with the Stars Code Snippet

 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: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
open System

// Defs
let zero = """
 _ 
| |
|_|"""

let one = """
   
  |
  | 
"""
let two = """
 _ 
 _|
|_ """ 

let three = """
 _ 
 _|
 _|"""
   
let four = """
   
|_|
  |"""

let five = """
 _ 
|_ 
 _|"""
let six = """
 _ 
|_ 
|_|"""
let seven = """
 _ 
  |
  |"""
let eight = """
 _ 
|_|
|_|"""
let nine = """
 _ 
|_|
 _|"""

// Part 1
let inline numberMap (n: string) =
    let n' = n.[1..].Split(Environment.NewLine.ToCharArray(), StringSplitOptions.None)
    Array2D.init 3 3 (fun i j -> n'.[i].[j])
    
let numbers = 
    [|zero; one; two; three; four; five; six; seven; eight; nine|]
    |> Array.mapi (fun i n -> numberMap n, i) |> dict
    
let usercase1 = """
    _  _     _  _  _  _  _ 
  | _| _||_||_ |_   ||_||_|
  ||_  _|  | _||_|  ||_| _|""".[1..]


let parseNumbers (numberStr: string) =
    let charArr = numberStr.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                  |> Array.map (fun i -> i.ToCharArray())
    let charArr2d = Array2D.init (charArr.Length) (charArr.[0].Length)
                        (fun i j -> charArr.[i].[j])
    [|  for j = 0 to (charArr.[1].Length - 1) / 3 do
            let idx = j*3 in yield charArr2d.[0..2, idx.. idx+2] 
    |] |> Array.map (fun v -> numbers.[v])

// Part 2 
let doesChecksum (acc: int []) =
    let summed = acc |> Array.rev |> Array.mapi (fun i v -> (i+1) * v) |> Array.sum
    summed % 11 = 0
namespace System
val zero : string

Full name: Script.zero
val one : string

Full name: Script.one
val two : string

Full name: Script.two
val three : string

Full name: Script.three
val four : string

Full name: Script.four
val five : string

Full name: Script.five
val six : string

Full name: Script.six
val seven : string

Full name: Script.seven
val eight : string

Full name: Script.eight
val nine : string

Full name: Script.nine
val numberMap : n:string -> char [,]

Full name: Script.numberMap
val n : string
Multiple items
val string : value:'T -> string

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

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

Full name: Microsoft.FSharp.Core.string
val n' : string []
type Environment =
  static member CommandLine : string
  static member CurrentDirectory : string with get, set
  static member Exit : exitCode:int -> unit
  static member ExitCode : int with get, set
  static member ExpandEnvironmentVariables : name:string -> string
  static member FailFast : message:string -> unit + 1 overload
  static member GetCommandLineArgs : unit -> string[]
  static member GetEnvironmentVariable : variable:string -> string + 1 overload
  static member GetEnvironmentVariables : unit -> IDictionary + 1 overload
  static member GetFolderPath : folder:SpecialFolder -> string + 1 overload
  ...
  nested type SpecialFolder
  nested type SpecialFolderOption

Full name: System.Environment
property Environment.NewLine: string
String.ToCharArray() : char []
String.ToCharArray(startIndex: int, length: int) : char []
type StringSplitOptions =
  | None = 0
  | RemoveEmptyEntries = 1

Full name: System.StringSplitOptions
field StringSplitOptions.None = 0
module Array2D

from Microsoft.FSharp.Collections
val init : length1:int -> length2:int -> initializer:(int -> int -> 'T) -> 'T [,]

Full name: Microsoft.FSharp.Collections.Array2D.init
val i : int
val j : int
val numbers : Collections.Generic.IDictionary<char [,],int>

Full name: Script.numbers
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val mapi : mapping:(int -> 'T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.mapi
val dict : keyValuePairs:seq<'Key * 'Value> -> Collections.Generic.IDictionary<'Key,'Value> (requires equality)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.dict
val usercase1 : string

Full name: Script.usercase1
val parseNumbers : numberStr:string -> int []

Full name: Script.parseNumbers
val numberStr : string
val charArr : char [] []
String.Split([<ParamArray>] separator: char []) : string []
String.Split(separator: string [], options: StringSplitOptions) : string []
String.Split(separator: char [], options: StringSplitOptions) : string []
String.Split(separator: char [], count: int) : string []
String.Split(separator: string [], count: int, options: StringSplitOptions) : string []
String.Split(separator: char [], count: int, options: StringSplitOptions) : string []
field StringSplitOptions.RemoveEmptyEntries = 1
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val i : string
val charArr2d : char [,]
property Array.Length: int
val idx : int
val v : char [,]
val doesChecksum : acc:int [] -> bool

Full name: Script.doesChecksum
val acc : int []
Multiple items
val int : value:'T -> int (requires member op_Explicit)

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

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val summed : int
val rev : array:'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.rev
val v : int
val sum : array:'T [] -> 'T (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.Array.sum

More information

Link:http://fssnip.net/kE
Posted:10 years ago
Author:Rickasaurus
Tags: kata