Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method for PositionsProperty on non-dense Lists #2021

Merged
merged 1 commit into from
Dec 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions lib/list.gd
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,8 @@ DeclareGlobalFunction( "PositionMinimum" );
## <Oper Name="PositionsProperty" Arg='list, func'/>
##
## <Description>
## returns the list of all those positions in the dense list <A>list</A>
## returns the list of all those positions in the list <A>list</A>
## which are bound and
## for which the property tester function <A>func</A> returns <K>true</K>.
## <P/>
## <Example><![CDATA[
Expand All @@ -972,7 +973,7 @@ DeclareGlobalFunction( "PositionMinimum" );
## </ManSection>
## <#/GAPDoc>
##
DeclareOperation( "PositionsProperty", [ IsDenseList, IsFunction ] );
DeclareOperation( "PositionsProperty", [ IsList, IsFunction ] );


#############################################################################
Expand Down
16 changes: 16 additions & 0 deletions lib/list.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,22 @@ InstallGlobalFunction( PositionMinimum,
##
#M PositionsProperty(<list>,<func>) . positions of elements with a property
##
InstallMethod( PositionsProperty,
"for list and function",
[ IsList, IsFunction ],
function( list, func )
local result, i;

result:= [];
for i in [ 1 .. Length( list ) ] do
if IsBound( list[ i ] ) and func( list[i] ) then
Add( result, i );
fi;
od;

return result;
end );

InstallMethod( PositionsProperty,
"for dense list and function",
[ IsDenseList, IsFunction ],
Expand Down
12 changes: 12 additions & 0 deletions tst/testinstall/list.tst
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,17 @@ gap> l := [40, 39 .. 10];;
gap> RepresentativeSmallest(l);
10

# PositionsProperty
gap> ll := [ 1, , "s" ];;
gap> PositionsProperty( ll, ReturnTrue );
[ 1, 3 ]
gap> PositionsProperty( ll, IsInt );
[ 1 ]
gap> ll := [ 1, 2, 3 ];;
gap> PositionsProperty( ll, ReturnTrue );
[ 1, 2, 3 ]
gap> PositionsProperty( ll, IsInt );
[ 1, 2, 3 ]

#
gap> STOP_TEST("list.tst");