Snippets

Cameron Presley Examples of using F# Reduce

Created by Cameron Presley
let values = [1 .. 10]
values |> List.reduce (fun state item -> if item % 2 = 0 then state+item else state-item) |> printfn "%A"

type Card = Guard | Priest | Baron | Handmaid | Prince | King | Countess | Princess
let cardPower card = match card with | Guard -> 1 | Priest -> 2 | Baron -> 3 | Handmaid -> 4 | Prince -> 5 | King -> 6 | Countess -> 7 | Princess -> 8
let cards = [Guard; Priest; Baron; Handmaid; Prince; King; Countess; Princess]
cards |> List.rev |> List.reduce (fun state item -> if cardPower state > cardPower item then state else item) |> printfn "%A"

let baronAction cardOne cardTwo =
    [cardOne; cardTwo] |> List.reduce (fun state item -> if cardPower state > cardPower item then state else item)

baronAction Princess Guard |> printfn "%A"
baronAction King King |> printfn "%A"

Comments (0)