Skip to content

Commit

Permalink
Method for PositionsProperty on non-dense Lists
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasguts committed Dec 13, 2017
1 parent 9ae06ac commit 38b552a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
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");

0 comments on commit 38b552a

Please sign in to comment.