-- | Tirages.
module Reloto.Draw where

import Data.Bool
import Prelude (Integer, Integral(..), Num(..))
import Data.List (length, map)

import Reloto.Bits
import Reloto.Combin
import Reloto.Sequence

-- | @unorderedDraw k xs bs@ retourne 'k' choix (sans ordre ni répétition) parmi 'xs'
-- déterminés par l’entropie 'bs'.
unorderedDraw :: Integer -> [a] -> [Bool] -> [a]
unorderedDraw :: Integer -> [a] -> [Bool] -> [a]
unorderedDraw Integer
k [a]
xs [Bool]
bs = (Integer -> Int) -> [Integer] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer -> [Integer]
forall i. Integral i => i -> i -> i -> [i]
combinOfRank Integer
n Integer
k Integer
r) [Int] -> [a] -> [a]
forall a. [Int] -> [a] -> [a]
`permute` [a]
xs
  where n :: Integer
n = Int -> Integer
forall a. Integral a => a -> Integer
toInteger ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs)
        r :: Integer
r = Integer -> [Bool] -> Integer
randomIntegerOfBits (Integer
nInteger -> Integer -> Integer
forall i. Integral i => i -> i -> i
`nCk`Integer
k) [Bool]
bs

-- | @orderedDraw k xs bs@ retourne 'k' choix (avec ordre mais sans répétition) parmi 'xs'
-- déterminés par l’entropie 'bs'.
orderedDraw :: Integer -> [a] -> [Bool] -> [a]
orderedDraw :: Integer -> [a] -> [Bool] -> [a]
orderedDraw Integer
k [a]
xs [Bool]
bs = (Integer -> Int) -> [Integer] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer -> [Integer]
forall i. Integral i => i -> i -> i -> [i]
sequenceOfRank Integer
n Integer
k Integer
r) [Int] -> [a] -> [a]
forall a. [Int] -> [a] -> [a]
`permute` [a]
xs
  where n :: Integer
n = Int -> Integer
forall a. Integral a => a -> Integer
toInteger ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs)
        r :: Integer
r = Integer -> [Bool] -> Integer
randomIntegerOfBits (Integer
nInteger -> Integer -> Integer
forall i. Integral i => i -> i -> i
`nAk`Integer
k) [Bool]
bs