Reversi Kata at ProgFsharp

The Official Solution.

Copy Source
Copy Link
Tools:
 1: 
 2: let input = """
 3:   B.......
 4:   ........
 5:   ........
 6:   ...BW...
 7:   ...WB...
 8:   ........
 9:   ........
10:   ........"""
11: 
12: type color = | White | Black | Nothing
13: 
14: open System
15: 
16: let chartoColor s = 
17:     s |> Seq.map(function 
18:                    | 'W' -> White 
19:                    | 'B' -> Black
20:                    | '.' -> Nothing )
21:       |> Seq.toArray
22: 
23: let board = input.Split('\n') 
24:               |> Array.map(fun s -> s.Trim())
25:               |> Array.filter(fun s -> s <> "")
26:               |> Array.map chartoColor 
27:               |> array2D
28: 
29: let rec move (dx,dy) (x,y)  = seq{ 
30:     if not(x < 0 || y < 0 || x >= 8 || y >=8) then 
31:        yield (x,y), (board.[x,y])
32:        yield! move (dx,dy) (x+dx, y+dy) 
33:       }
34: 
35: let up = move (-1,0)
36: up (4,4)         
37: 
38: 
39: 
40: let otherColor = function
41:   | Black -> White
42:   | White -> Black
43:   | _ -> Nothing
44: 
45: let legalMove color seqmove = 
46:   let restlist= 
47:      seqmove 
48:       |> Seq.skip 1 
49:       |> Seq.skipWhile(fun ((x,y),c) -> c = otherColor color)
50:       |> Seq.toList
51: 
52:   match restlist, restlist.Length < (Seq.length seqmove)-1 with 
53:   | ((x,y), Nothing)::xs, true -> Some (x,y)
54:   | _ -> None
55: 
56: let directions = [(-1,0);(1,0);(0,-1);(0,1);(1,1);(-1,1);(1,-1);(-1,-1)] 
57: 
58: let moves color pos =
59:   directions |> List.choose (fun d -> move d pos |> legalMove color)
60:              |> Set.ofSeq
61: 
62: let generateMoves color = 
63:     seq {
64:       for x in 0 .. 7 do
65:         for y in 0 .. 7 do
66:           if color = board.[x,y] then
67:             yield moves color (x,y)
68:     }
69:     |> Seq.fold (Set.union) Set.empty
70: 
71: 
72: // ---------------------------------
73: 
74: [Black; White; White; Nothing ]
75: |> Seq.mapi (fun i c -> (0, i), c)
76: |> legalMove Black 
77: 
78: [Black; White; White ]
79: |> Seq.mapi (fun i c -> (0, i), c)
80: |> legalMove Black 
81: 
82: [Black; Nothing ]
83: |> Seq.mapi (fun i c -> (0, i), c)
84: |> legalMove Black 
85: 
86: [Black; White; Black; Nothing ]
87: |> Seq.mapi (fun i c -> (0, i), c)
88: |> legalMove Black 
89: 
90: 
91: moves Black (4, 4)
92: moves Black (3, 3)
93: moves Black (0, 0)  
94: 
95: generateMoves Black
96: 
97: 
98: 
val input : 'a

Full name: Snippet.input
type color =
  | White
  | Black
  | Nothing

Full name: Snippet.color

  type: color
  implements: System.IEquatable<color>
  implements: System.Collections.IStructuralEquatable
  implements: System.IComparable<color>
  implements: System.IComparable
  implements: System.Collections.IStructuralComparable
union case color.White: color
union case color.Black: color
union case color.Nothing: color
namespace System
val chartoColor : seq<char> -> color array

Full name: Snippet.chartoColor
val s : seq<char>

  type: seq<char>
  inherits: Collections.IEnumerable
module Seq

from Microsoft.FSharp.Collections
val map : ('T -> 'U) -> seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val toArray : seq<'T> -> 'T array

Full name: Microsoft.FSharp.Collections.Seq.toArray
val board : color [,]

Full name: Snippet.board

  type: color [,]
  implements: ICloneable
  implements: Collections.IList
  implements: Collections.ICollection
  implements: Collections.IEnumerable
  implements: Collections.IStructuralComparable
  implements: Collections.IStructuralEquatable
  inherits: Array
type Array =
  class
    member Clone : unit -> obj
    member CopyTo : System.Array * int -> unit
    member CopyTo : System.Array * int64 -> unit
    member GetEnumerator : unit -> System.Collections.IEnumerator
    member GetLength : int -> int
    member GetLongLength : int -> int64
    member GetLowerBound : int -> int
    member GetUpperBound : int -> int
    member GetValue : int [] -> obj
    member GetValue : int -> obj
    member GetValue : int64 -> obj
    member GetValue : int64 [] -> obj
    member GetValue : int * int -> obj
    member GetValue : int64 * int64 -> obj
    member GetValue : int * int * int -> obj
    member GetValue : int64 * int64 * int64 -> obj
    member Initialize : unit -> unit
    member IsFixedSize : bool
    member IsReadOnly : bool
    member IsSynchronized : bool
    member Length : int
    member LongLength : int64
    member Rank : int
    member SetValue : obj * int -> unit
    member SetValue : obj * int [] -> unit
    member SetValue : obj * int64 -> unit
    member SetValue : obj * int64 [] -> unit
    member SetValue : obj * int * int -> unit
    member SetValue : obj * int64 * int64 -> unit
    member SetValue : obj * int * int * int -> unit
    member SetValue : obj * int64 * int64 * int64 -> unit
    member SyncRoot : obj
    static member AsReadOnly<'T> : 'T [] -> System.Collections.ObjectModel.ReadOnlyCollection<'T>
    static member BinarySearch : System.Array * obj -> int
    static member BinarySearch<'T> : 'T [] * 'T -> int
    static member BinarySearch : System.Array * obj * System.Collections.IComparer -> int
    static member BinarySearch<'T> : 'T [] * 'T * System.Collections.Generic.IComparer<'T> -> int
    static member BinarySearch : System.Array * int * int * obj -> int
    static member BinarySearch<'T> : 'T [] * int * int * 'T -> int
    static member BinarySearch : System.Array * int * int * obj * System.Collections.IComparer -> int
    static member BinarySearch<'T> : 'T [] * int * int * 'T * System.Collections.Generic.IComparer<'T> -> int
    static member Clear : System.Array * int * int -> unit
    static member ConstrainedCopy : System.Array * int * System.Array * int * int -> unit
    static member ConvertAll<'TInput,'TOutput> : 'TInput [] * System.Converter<'TInput,'TOutput> -> 'TOutput []
    static member Copy : System.Array * System.Array * int -> unit
    static member Copy : System.Array * System.Array * int64 -> unit
    static member Copy : System.Array * int * System.Array * int * int -> unit
    static member Copy : System.Array * int64 * System.Array * int64 * int64 -> unit
    static member CreateInstance : System.Type * int -> System.Array
    static member CreateInstance : System.Type * int [] -> System.Array
    static member CreateInstance : System.Type * int64 [] -> System.Array
    static member CreateInstance : System.Type * int * int -> System.Array
    static member CreateInstance : System.Type * int [] * int [] -> System.Array
    static member CreateInstance : System.Type * int * int * int -> System.Array
    static member Exists<'T> : 'T [] * System.Predicate<'T> -> bool
    static member Find<'T> : 'T [] * System.Predicate<'T> -> 'T
    static member FindAll<'T> : 'T [] * System.Predicate<'T> -> 'T []
    static member FindIndex<'T> : 'T [] * System.Predicate<'T> -> int
    static member FindIndex<'T> : 'T [] * int * System.Predicate<'T> -> int
    static member FindIndex<'T> : 'T [] * int * int * System.Predicate<'T> -> int
    static member FindLast<'T> : 'T [] * System.Predicate<'T> -> 'T
    static member FindLastIndex<'T> : 'T [] * System.Predicate<'T> -> int
    static member FindLastIndex<'T> : 'T [] * int * System.Predicate<'T> -> int
    static member FindLastIndex<'T> : 'T [] * int * int * System.Predicate<'T> -> int
    static member ForEach<'T> : 'T [] * System.Action<'T> -> unit
    static member IndexOf : System.Array * obj -> int
    static member IndexOf<'T> : 'T [] * 'T -> int
    static member IndexOf : System.Array * obj * int -> int
    static member IndexOf<'T> : 'T [] * 'T * int -> int
    static member IndexOf : System.Array * obj * int * int -> int
    static member IndexOf<'T> : 'T [] * 'T * int * int -> int
    static member LastIndexOf : System.Array * obj -> int
    static member LastIndexOf<'T> : 'T [] * 'T -> int
    static member LastIndexOf : System.Array * obj * int -> int
    static member LastIndexOf<'T> : 'T [] * 'T * int -> int
    static member LastIndexOf : System.Array * obj * int * int -> int
    static member LastIndexOf<'T> : 'T [] * 'T * int * int -> int
    static member Resize<'T> : 'T [] * int -> unit
    static member Reverse : System.Array -> unit
    static member Reverse : System.Array * int * int -> unit
    static member Sort : System.Array -> unit
    static member Sort<'T> : 'T [] -> unit
    static member Sort : System.Array * System.Array -> unit
    static member Sort : System.Array * System.Collections.IComparer -> unit
    static member Sort<'TKey,'TValue> : 'TKey [] * 'TValue [] -> unit
    static member Sort<'T> : 'T [] * System.Collections.Generic.IComparer<'T> -> unit
    static member Sort<'T> : 'T [] * System.Comparison<'T> -> unit
    static member Sort : System.Array * int * int -> unit
    static member Sort : System.Array * System.Array * System.Collections.IComparer -> unit
    static member Sort<'T> : 'T [] * int * int -> unit
    static member Sort<'TKey,'TValue> : 'TKey [] * 'TValue [] * System.Collections.Generic.IComparer<'TKey> -> unit
    static member Sort : System.Array * System.Array * int * int -> unit
    static member Sort : System.Array * int * int * System.Collections.IComparer -> unit
    static member Sort<'TKey,'TValue> : 'TKey [] * 'TValue [] * int * int -> unit
    static member Sort<'T> : 'T [] * int * int * System.Collections.Generic.IComparer<'T> -> unit
    static member Sort : System.Array * System.Array * int * int * System.Collections.IComparer -> unit
    static member Sort<'TKey,'TValue> : 'TKey [] * 'TValue [] * int * int * System.Collections.Generic.IComparer<'TKey> -> unit
    static member TrueForAll<'T> : 'T [] * System.Predicate<'T> -> bool
  end

Full name: System.Array

  type: Array
  implements: ICloneable
  implements: Collections.IList
  implements: Collections.ICollection
  implements: Collections.IEnumerable
  implements: Collections.IStructuralComparable
  implements: Collections.IStructuralEquatable
val map : ('T -> 'U) -> 'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val s : obj
val filter : ('T -> bool) -> 'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.filter
val s : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
val array2D : seq<#seq<'T>> -> 'T [,]

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.array2D
val move : int * int -> int * int -> seq<(int * int) * color>

Full name: Snippet.move
val dx : int

  type: int
  implements: IComparable
  implements: IFormattable
  implements: IConvertible
  implements: IComparable<int>
  implements: IEquatable<int>
  inherits: ValueType
val dy : int

  type: int
  implements: IComparable
  implements: IFormattable
  implements: IConvertible
  implements: IComparable<int>
  implements: IEquatable<int>
  inherits: ValueType
val x : int

  type: int
  implements: IComparable
  implements: IFormattable
  implements: IConvertible
  implements: IComparable<int>
  implements: IEquatable<int>
  inherits: ValueType
val y : int

  type: int
  implements: IComparable
  implements: IFormattable
  implements: IConvertible
  implements: IComparable<int>
  implements: IEquatable<int>
  inherits: ValueType
Multiple items
val seq : seq<'T> -> seq<'T>

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

--------------------

type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>

  type: seq<'T>
  inherits: Collections.IEnumerable
val not : bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val up : (int * int -> seq<(int * int) * color>)

Full name: Snippet.up
val otherColor : color -> color

Full name: Snippet.otherColor
val legalMove : color -> seq<('a * 'b) * color> -> ('a * 'b) option

Full name: Snippet.legalMove
Multiple items
val color : color

  type: color
  implements: IEquatable<color>
  implements: Collections.IStructuralEquatable
  implements: IComparable<color>
  implements: IComparable
  implements: Collections.IStructuralComparable


--------------------

type color =
  | White
  | Black
  | Nothing

Full name: Snippet.color

  type: color
  implements: IEquatable<color>
  implements: Collections.IStructuralEquatable
  implements: IComparable<color>
  implements: IComparable
  implements: Collections.IStructuralComparable
val seqmove : seq<('a * 'b) * color>

  type: seq<('a * 'b) * color>
  inherits: Collections.IEnumerable
val restlist : (('a * 'b) * color) list

  type: (('a * 'b) * color) list
  implements: Collections.IStructuralEquatable
  implements: IComparable<List<('a * 'b) * color>>
  implements: IComparable
  implements: Collections.IStructuralComparable
  implements: Collections.Generic.IEnumerable<('a * 'b) * color>
  implements: Collections.IEnumerable
val skip : int -> seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.skip
val skipWhile : ('T -> bool) -> seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.skipWhile
val x : 'a
val y : 'b
val c : color

  type: color
  implements: IEquatable<color>
  implements: Collections.IStructuralEquatable
  implements: IComparable<color>
  implements: IComparable
  implements: Collections.IStructuralComparable
val toList : seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.Seq.toList
property List.Length: int
val length : seq<'T> -> int

Full name: Microsoft.FSharp.Collections.Seq.length
val xs : (('a * 'b) * color) list

  type: (('a * 'b) * color) list
  implements: Collections.IStructuralEquatable
  implements: IComparable<List<('a * 'b) * color>>
  implements: IComparable
  implements: Collections.IStructuralComparable
  implements: Collections.Generic.IEnumerable<('a * 'b) * color>
  implements: Collections.IEnumerable
union case Option.Some: 'T -> Option<'T>
union case Option.None: Option<'T>
val directions : (int * int) list

Full name: Snippet.directions

  type: (int * int) list
  implements: Collections.IStructuralEquatable
  implements: IComparable<List<int * int>>
  implements: IComparable
  implements: Collections.IStructuralComparable
  implements: Collections.Generic.IEnumerable<int * int>
  implements: Collections.IEnumerable
val moves : color -> int * int -> Set<int * int>

Full name: Snippet.moves
val pos : int * int
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------

type List<'T> =
  | ( [] )
  | ( :: ) of 'T * 'T list
  with
    interface Collections.IEnumerable
    interface Collections.Generic.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
  end

Full name: Microsoft.FSharp.Collections.List<_>

  type: List<'T>
  implements: Collections.IStructuralEquatable
  implements: IComparable<List<'T>>
  implements: IComparable
  implements: Collections.IStructuralComparable
  implements: Collections.Generic.IEnumerable<'T>
  implements: Collections.IEnumerable
val choose : ('T -> 'U option) -> 'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.choose
val d : int * int
Multiple items
module Set

from Microsoft.FSharp.Collections

--------------------

type Set<'T (requires comparison)> =
  class
    interface IComparable
    interface Collections.IEnumerable
    interface Collections.Generic.IEnumerable<'T>
    interface Collections.Generic.ICollection<'T>
    new : elements:seq<'T> -> Set<'T>
    member Add : value:'T -> Set<'T>
    member Contains : value:'T -> bool
    override Equals : obj -> bool
    member IsProperSubsetOf : otherSet:Set<'T> -> bool
    member IsProperSupersetOf : otherSet:Set<'T> -> bool
    member IsSubsetOf : otherSet:Set<'T> -> bool
    member IsSupersetOf : otherSet:Set<'T> -> bool
    member Remove : value:'T -> Set<'T>
    member Count : int
    member IsEmpty : bool
    member MaximumElement : 'T
    member MinimumElement : 'T
    static member ( + ) : set1:Set<'T> * set2:Set<'T> -> Set<'T>
    static member ( - ) : set1:Set<'T> * set2:Set<'T> -> Set<'T>
  end

Full name: Microsoft.FSharp.Collections.Set<_>

  type: Set<'T>
  implements: IComparable
  implements: Collections.Generic.ICollection<'T>
  implements: seq<'T>
  implements: Collections.IEnumerable
val ofSeq : seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Set.ofSeq
val generateMoves : color -> Set<int * int>

Full name: Snippet.generateMoves
val fold : ('State -> 'T -> 'State) -> 'State -> seq<'T> -> 'State

Full name: Microsoft.FSharp.Collections.Seq.fold
val union : Set<'T> -> Set<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Set.union
val empty<'T (requires comparison)> : Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Set.empty
val mapi : (int -> 'T -> 'U) -> seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.mapi
val i : int

  type: int
  implements: IComparable
  implements: IFormattable
  implements: IConvertible
  implements: IComparable<int>
  implements: IEquatable<int>
  inherits: ValueType

More information

Link: http://fssnip.net/eY
Posted: 1 years ago
Author: Nick
Tags: reversi