1 people like it.
Like the snippet!
Kata Six: Anagrams - solution in F#
Solution to Kata Six: Anagrams from http://codekata.pragprog.com/2007/01/kata_six_anagra.html in F#
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:
|
open System
open System.Collections.Generic
open System.IO
//create sorted dictionary with each value a set of strings
let dictWords = new SortedDictionary<string, SortedSet<string>>()
//create word key - lower case, sorted characters
let createWordKey (word : string) =
(word.ToLower()).ToCharArray()
|> Array.sort
|> String.Concat
let addWordToDict word =
let u = createWordKey word
if dictWords.ContainsKey u then
if (dictWords.[u].Contains word) = false then
dictWords.[u].Add (word) |> ignore
else
let set = new SortedSet<string>()
set.Add word |> ignore
dictWords.[u] <- set
//create sorted dictionary
File.ReadLines "wordlist.txt"
|> Seq.iter (fun w -> addWordToDict w)
//Print anagrams
for key in dictWords.Keys do
if dictWords.[key].Count > 1 then
printf "%s : %d " key dictWords.[key].Count
for l in dictWords.[key] do
printf "%s " l
printf "\n "
|
namespace System
namespace System.Collections
namespace System.Collections.Generic
namespace System.IO
val dictWords : SortedDictionary<string,SortedSet<string>>
Full name: Script.dictWords
Multiple items
type SortedDictionary<'TKey,'TValue> =
new : unit -> SortedDictionary<'TKey, 'TValue> + 3 overloads
member Add : key:'TKey * value:'TValue -> unit
member Clear : unit -> unit
member Comparer : IComparer<'TKey>
member ContainsKey : key:'TKey -> bool
member ContainsValue : value:'TValue -> bool
member CopyTo : array:KeyValuePair<'TKey, 'TValue>[] * index:int -> unit
member Count : int
member GetEnumerator : unit -> Enumerator<'TKey, 'TValue>
member Item : 'TKey -> 'TValue with get, set
...
nested type Enumerator
nested type KeyCollection
nested type ValueCollection
Full name: System.Collections.Generic.SortedDictionary<_,_>
--------------------
SortedDictionary() : unit
SortedDictionary(dictionary: IDictionary<'TKey,'TValue>) : unit
SortedDictionary(comparer: IComparer<'TKey>) : unit
SortedDictionary(dictionary: IDictionary<'TKey,'TValue>, comparer: IComparer<'TKey>) : unit
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = String
Full name: Microsoft.FSharp.Core.string
Multiple items
type SortedSet<'T> =
new : unit -> SortedSet<'T> + 3 overloads
member Add : item:'T -> bool
member Clear : unit -> unit
member Comparer : IComparer<'T>
member Contains : item:'T -> bool
member CopyTo : array:'T[] -> unit + 2 overloads
member Count : int
member ExceptWith : other:IEnumerable<'T> -> unit
member GetEnumerator : unit -> Enumerator<'T>
member GetViewBetween : lowerValue:'T * upperValue:'T -> SortedSet<'T>
...
nested type Enumerator
Full name: System.Collections.Generic.SortedSet<_>
--------------------
SortedSet() : unit
SortedSet(comparer: IComparer<'T>) : unit
SortedSet(collection: IEnumerable<'T>) : unit
SortedSet(collection: IEnumerable<'T>, comparer: IComparer<'T>) : unit
val createWordKey : word:string -> string
Full name: Script.createWordKey
val word : string
String.ToLower() : string
String.ToLower(culture: Globalization.CultureInfo) : string
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 sort : array:'T [] -> 'T [] (requires comparison)
Full name: Microsoft.FSharp.Collections.Array.sort
Multiple items
type String =
new : value:char -> string + 7 overloads
member Chars : int -> char
member Clone : unit -> obj
member CompareTo : value:obj -> int + 1 overload
member Contains : value:string -> bool
member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
member EndsWith : value:string -> bool + 2 overloads
member Equals : obj:obj -> bool + 2 overloads
member GetEnumerator : unit -> CharEnumerator
member GetHashCode : unit -> int
...
Full name: System.String
--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
String.Concat([<ParamArray>] values: string []) : string
(+0 other overloads)
String.Concat(values: IEnumerable<string>) : string
(+0 other overloads)
String.Concat<'T>(values: IEnumerable<'T>) : string
(+0 other overloads)
String.Concat([<ParamArray>] args: obj []) : string
(+0 other overloads)
String.Concat(arg0: obj) : string
(+0 other overloads)
String.Concat(str0: string, str1: string) : string
(+0 other overloads)
String.Concat(arg0: obj, arg1: obj) : string
(+0 other overloads)
String.Concat(str0: string, str1: string, str2: string) : string
(+0 other overloads)
String.Concat(arg0: obj, arg1: obj, arg2: obj) : string
(+0 other overloads)
String.Concat(str0: string, str1: string, str2: string, str3: string) : string
(+0 other overloads)
val addWordToDict : word:string -> unit
Full name: Script.addWordToDict
val u : string
SortedDictionary.ContainsKey(key: string) : bool
val ignore : value:'T -> unit
Full name: Microsoft.FSharp.Core.Operators.ignore
val set : SortedSet<string>
SortedSet.Add(item: string) : bool
type File =
static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
static member AppendAllText : path:string * contents:string -> unit + 1 overload
static member AppendText : path:string -> StreamWriter
static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
static member Create : path:string -> FileStream + 3 overloads
static member CreateText : path:string -> StreamWriter
static member Decrypt : path:string -> unit
static member Delete : path:string -> unit
static member Encrypt : path:string -> unit
static member Exists : path:string -> bool
...
Full name: System.IO.File
File.ReadLines(path: string) : IEnumerable<string>
File.ReadLines(path: string, encoding: Text.Encoding) : IEnumerable<string>
module Seq
from Microsoft.FSharp.Collections
val iter : action:('T -> unit) -> source:seq<'T> -> unit
Full name: Microsoft.FSharp.Collections.Seq.iter
val w : string
val key : string
property SortedDictionary.Keys: SortedDictionary`2.KeyCollection<string,SortedSet<string>>
val printf : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
val l : string
More information