2 people like it.

Eurovision - Some(points)

The Eurovision final scoring system using records and some higher order functions. (Results are fictional - no-one seems to publish the full voting live online.)

 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: 
type Country =
    | Albania
    | Montenegro
    | Romania
    | Austria
    | Ukraine
    | Belarus
    | Belgium
    | Azerbaijan
    | Malta
    | SanMarino
    | France
    | UnitedKingdom
    | Turkey
    | Greece
    | BosniaHerzogovina
    | Moldova
    | Bulgaria
    | Switzerland
    | Slovenia
    | Cyprus
    | Croatia
    | Solvakia
    | Macedonia
    | Netherlands
    | Portugal
    | Iceland
    | Sweden
    | Norway
    | Lithuania
    | Estonia
    | Denmark
    | Ireland
    | Latvia
    | Spain
    | Finland
    | Georgia
    | Italy
    | Serbia
    | Germany
    | Russia
    | Hungary
    | Israel

type Awards = {Giver : Country; Recipients : List<Country>}

type Received = {Recipient : Country; Score : int}

let AwardPoints (results : List<Awards>) =
    results
    |> List.map (fun result -> result.Recipients
                               |> List.mapi (fun i recipient -> {Recipient = recipient; Score = 12-i}))
    |> List.concat
    |> Seq.ofList
    |> Seq.groupBy (fun received -> received.Recipient)
    |> Seq.map (fun (country, awards) -> {Recipient = country;
                                          Score = awards |> Seq.sumBy (fun award -> award.Score)})
    |> List.ofSeq

let resultsAwarded =
    [
        {Giver = Albania; Recipients = [Spain; France; Sweden]}
        {Giver = Montenegro; Recipients = [Sweden; Greece; Albania]}
        {Giver = Romania; Recipients = [Germany; Russia; Serbia]}
        {Giver = Austria; Recipients = [Georgia; Spain; Sweden]}
        {Giver = Ukraine; Recipients = [Finland; Latvia; Lithuania]}
        {Giver = Belarus; Recipients = [Iceland; Portugal; Netherlands]}
        {Giver = Belgium; Recipients = [Iceland; France; Israel]}
        {Giver = Azerbaijan; Recipients = [Azerbaijan; France; Georgia]}
        {Giver = Malta; Recipients = [Albania; Portugal; Cyprus]}
        // etc etc.
    ]

let Outcomes =
    AwardPoints resultsAwarded
    |> List.sortBy (fun received -> -received.Score)
union case Country.Albania: Country
union case Country.Montenegro: Country
union case Country.Romania: Country
union case Country.Austria: Country
union case Country.Ukraine: Country
union case Country.Belarus: Country
union case Country.Belgium: Country
union case Country.Azerbaijan: Country
union case Country.Malta: Country
union case Country.SanMarino: Country
union case Country.France: Country
union case Country.UnitedKingdom: Country
union case Country.Turkey: Country
union case Country.Greece: Country
union case Country.BosniaHerzogovina: Country
union case Country.Moldova: Country
union case Country.Bulgaria: Country
union case Country.Switzerland: Country
union case Country.Slovenia: Country
union case Country.Cyprus: Country
union case Country.Croatia: Country
union case Country.Solvakia: Country
union case Country.Macedonia: Country
union case Country.Netherlands: Country
union case Country.Portugal: Country
union case Country.Iceland: Country
union case Country.Sweden: Country
union case Country.Norway: Country
union case Country.Lithuania: Country
union case Country.Estonia: Country
union case Country.Denmark: Country
union case Country.Ireland: Country
union case Country.Latvia: Country
union case Country.Spain: Country
union case Country.Finland: Country
union case Country.Georgia: Country
union case Country.Italy: Country
union case Country.Serbia: Country
union case Country.Germany: Country
union case Country.Russia: Country
union case Country.Hungary: Country
union case Country.Israel: Country
type Awards =
  {Giver: Country;
   Recipients: List<Country>;}

Full name: Script.Awards
Awards.Giver: Country
type Country =
  | Albania
  | Montenegro
  | Romania
  | Austria
  | Ukraine
  | Belarus
  | Belgium
  | Azerbaijan
  | Malta
  | SanMarino
  ...

Full name: Script.Country
Awards.Recipients: List<Country>
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<_>
type Received =
  {Recipient: Country;
   Score: int;}

Full name: Script.Received
Received.Recipient: Country
Received.Score: 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 AwardPoints : results:List<Awards> -> Received list

Full name: Script.AwardPoints
val results : List<Awards>
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val result : Awards
val mapi : mapping:(int -> 'T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.mapi
val i : int
val recipient : Country
val concat : lists:seq<'T list> -> 'T list

Full name: Microsoft.FSharp.Collections.List.concat
module Seq

from Microsoft.FSharp.Collections
val ofList : source:'T list -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.ofList
val groupBy : projection:('T -> 'Key) -> source:seq<'T> -> seq<'Key * seq<'T>> (requires equality)

Full name: Microsoft.FSharp.Collections.Seq.groupBy
val received : Received
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val country : Country
val awards : seq<Received>
val sumBy : projection:('T -> 'U) -> source:seq<'T> -> 'U (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.sumBy
val award : Received
val ofSeq : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.List.ofSeq
val resultsAwarded : Awards list

Full name: Script.resultsAwarded
val Outcomes : Received list

Full name: Script.Outcomes
val sortBy : projection:('T -> 'Key) -> list:'T list -> 'T list (requires comparison)

Full name: Microsoft.FSharp.Collections.List.sortBy
Raw view Test code New version

More information

Link:http://fssnip.net/cg
Posted:11 years ago
Author:Kit Eason
Tags: learning f# , sequences , records