Skip to content

Commit

Permalink
Make Random for a finite collection take an optional source
Browse files Browse the repository at this point in the history
This requires moving the function to random.gi, as we want to use
InstallMethodWithRandomSource.
  • Loading branch information
ChrisJefferson committed Mar 6, 2018
1 parent 40bc9ac commit 4add79c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 2 additions & 4 deletions lib/coll.gi
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,10 @@ InstallGlobalFunction( RandomList, function(args...)

return list[Random(source, 1, Length(list))];
end );
InstallMethod( Random, "for a (finite) collection",
[ IsCollection and IsFinite ],
C -> RandomList( Enumerator( C ) ) );

RedispatchOnCondition(Random,true,[IsCollection],[IsFinite],0);

RedispatchOnCondition(Random,true,[IsCollection],[IsFinite],0);
RedispatchOnCondition(Random,true,[IsRandomSource,IsCollection],[IsFinite],0);

#############################################################################
##
Expand Down
6 changes: 6 additions & 0 deletions lib/random.gi
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,9 @@ end );
InstallGlobalFunction("InstallMethodWithRandomSource", func(InstallMethod));
InstallGlobalFunction("InstallOtherMethodWithRandomSource", func(InstallOtherMethod));
end)();

# This method must rank below Random(SomeRandomSource, IsList)
# for any random source SomeRandomSource, to avoid an infinite loop.
InstallMethodWithRandomSource( Random, "for a random source and a (finite) collection",
[ IsRandomSource, IsCollection and IsFinite ], -8,
{rs, C} -> RandomList(rs, Enumerator( C ) ) );

0 comments on commit 4add79c

Please sign in to comment.