Created
August 9, 2019 02:45
-
-
Save krainboltgreene/4c71407f4b6c3631d777f240d2f26450 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {upTo} = require("@unction/complete") | |
const {length} = require("@unction/complete") | |
const {reverse} = require("@unction/complete") | |
const {shuffle} = require("@unction/complete") | |
const {sample} = require("@unction/complete") | |
const {reduceKeys} = require("@unction/complete") | |
const {reduceWithValueKey} = require("@unction/complete") | |
const {mapValues} = require("@unction/complete") | |
const {last} = require("@unction/complete") | |
const {first} = require("@unction/complete") | |
const chunk = | |
(maximum) => | |
(list) => | |
reduceWithValueKey( | |
(accumulated) => | |
(item) => | |
(index) => { | |
const [latestChunk, ...previousChunks] = accumulated; | |
if ((length(latestChunk) !== maximum) && (length(list) === index + 1)) { | |
return reverse([[...latestChunk, item], ...previousChunks]); | |
} | |
if (length(list) === index + 1) { | |
return reverse([[item], ...accumulated]); | |
} | |
if (length(latestChunk) !== maximum) { | |
return [[...latestChunk, item], ...previousChunks] | |
} | |
return [[item], ...accumulated]; | |
} | |
)( | |
[[]] | |
)( | |
list | |
) | |
// Pairings = Array<Match> | |
// ReadyPlayers = Array<Player> | |
// RoundList => ReadyPlayers | |
function discoverWinners (rounds) { | |
return mapValues(battle)(last(rounds)) | |
} | |
// ReadyPlayers => Pairings | |
function generateRound (players) { | |
return chunk(2)(shuffle(players)) | |
} | |
// Match = [Player, Player] | |
// Match => Winner | |
function battle([left, right]) { | |
if (right) { | |
return sample([left, right]) | |
} | |
// A buy round | |
return left | |
} | |
// ReadyPlayers => Player | |
function fight(players) { | |
return reduceKeys((accumulated) => (index) => { | |
if (accumulated.length == index - 1) { | |
return first(accumulated) | |
} | |
return [...accumulated, generateRound(discoverWinners(accumulated))] | |
})([generateRound(players)])(upTo(Math.floor(players.length / 2))) | |
} | |
console.log( | |
fight(["Jesus", "Salino", "Richard", "Hong", "Janus", "Alejandro", "Cortez", "Yun", "Inhani"]) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment