Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/DataFrame/Operations/Permutation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
import qualified Data.Text as T
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM

import Control.Exception (throw)
import Control.Monad.ST (runST)
import DataFrame.Errors (DataFrameException (..))
import DataFrame.Internal.Column
import DataFrame.Internal.Column (Columnable, atIndicesStable)
import DataFrame.Internal.DataFrame (DataFrame (..))
import DataFrame.Internal.Expression
import DataFrame.Internal.Row
import DataFrame.Operations.Core
import System.Random
import System.Random.Shuffle (shuffle')
import DataFrame.Internal.Expression (Expr (Col))
import DataFrame.Internal.Row (sortedIndexes', toRowVector)
import DataFrame.Operations.Core (columnNames, dimensions)
import System.Random (Random (randomR), RandomGen)

-- | Sort order taken as a parameter by the 'sortBy' function.
data SortOrder where
Expand Down Expand Up @@ -76,4 +77,17 @@
df{columns = V.map (atIndicesStable indexes) (columns df)}

shuffledIndices :: (RandomGen g) => g -> Int -> VU.Vector Int
shuffledIndices pureGen k = VU.fromList (shuffle' [0 .. (k - 1)] k pureGen)
shuffledIndices pureGen k = shuffleVec pureGen (VU.fromList [0 .. (k - 1)])
where
shuffleVec :: (RandomGen g) => g -> VU.Vector Int -> VU.Vector Int
shuffleVec g v = runST $ do
vm <- VU.thaw v
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of declaring the vector from list just to thaw it we can just create a new one in the shuffle vec function.

let (n, nGen) = randomR (0, (k - 1)) g

Check warning on line 85 in src/DataFrame/Operations/Permutation.hs

View workflow job for this annotation

GitHub Actions / hlint

Suggestion in shuffledIndices in module DataFrame.Operations.Permutation: Redundant bracket ▫︎ Found: "(0, (k - 1))" ▫︎ Perhaps: "(0, k - 1)"
go vm n nGen
VU.unsafeFreeze vm

go v (-1) _ = pure ()
go v 0 _ = pure ()
go v maxInd gen =
let (n, nextGen) = randomR (0, maxInd) gen
in VUM.swap v 0 n *> go (VUM.tail v) (maxInd - 1) nextGen
Loading