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

change iteration over GAP objects #1057

Merged
merged 2 commits into from
Oct 21, 2024
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
60 changes: 60 additions & 0 deletions docs/src/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,66 @@ in order to support special GAP syntax beyond function calls with arguments.
- Access components of a component object via [`getbangproperty`](@ref),
equivalent to GAP's `!.` operator.

- Iterate over a GAP list or collection (such as a GAP domain) `obj`
via `for x in obj`,
or `map(f, obj)` for a unary function `f`,
or `[f(x) for x in obj]`.
If `obj` is not a list then a GAP iterator for `obj` gets constructed.

```jldoctest
julia> l = [1, 2, 3]; gl = GapObj(l)
GAP: [ 1, 2, 3 ]

julia> ll = []; for x in gl push!(ll, x); end; ll == l
true

julia> map(x -> x, gl) == l
true

julia> [x for x in gl] == l
true
```

- Iterating in Julia over a GAP list `obj` skips the unbound entries in `obj`,
like iterating in GAP does.

```jldoctest
julia> gl = GAP.evalstr("[1,, 3]")
GAP: [ 1,, 3 ]

julia> ll = []; for x in gl push!(ll, x); end; ll
2-element Vector{Any}:
1
3
```

- Note that iterating in Julia over a GAP iterator object `obj` does *not*
change `obj`, whereas iterating in GAP over `obj` changes `obj`.

```jldoctest
julia> g = GAP.Globals.SymmetricGroup(3)
GAP: Sym( [ 1 .. 3 ] )

julia> iter = GAP.Globals.Iterator(g)
GAP: <iterator>

julia> [x for x in iter] == collect(g)
true

julia> [x for x in iter] == collect(g)
true

julia> f = GAP.evalstr("function(itr) local res, i; res:= [];" *
"for i in itr do Add( res, i ); od; return res; end")
GAP: function( itr ) ... end

julia> f( iter )
GAP: [ (), (2,3), (1,3), (1,3,2), (1,2,3), (1,2) ]

julia> f( iter )
GAP: [ ]
```

```@docs
call_gap_func
call_with_catch
Expand Down
2 changes: 1 addition & 1 deletion pkg/JuliaInterface/gap/JuliaInterface.gd
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ DeclareGlobalFunction( "JuliaImportPackage" );

#! @Description
#! This global variable represents the &Julia; module <C>Main</C>,
#! see <Ref Filt="IsJuliaModule" Label="for IsJuliaWrapper and IsRecord"/>.
#! see <Ref Filt="IsJuliaModule" Label="for IsJuliaObject and IsRecord"/>.
#!
#! The variables from the underlying &Julia; session can be accessed via
#! <Ref Var="Julia"/>, as follows.
Expand Down
8 changes: 6 additions & 2 deletions src/adapter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,6 @@ Random.Sampler(::Type{<:AbstractGAPRNG}, x::AbstractVector, ::Random.Repetition)
Base.literal_pow(::typeof(^), x::GapObj, ::Val{-1}) = Wrappers.InverseSameMutability(x)

# iteration

function Base.iterate(obj::GapObj)
if Wrappers.IsList(obj)
len = Wrappers.Length(obj)
Expand All @@ -496,7 +495,12 @@ end

function Base.iterate(obj::GapObj, (i, len)::Tuple{Int,Int})
i > len && return nothing
ElmList(obj, i), (i+1, len)
res = ElmList(obj, i)
while res === nothing # dangerous if `len` is *larger* than the length
Copy link
Member

Choose a reason for hiding this comment

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

is it possible for a GAP iterator to have holes at the end? If yes, this may be an infinite loop.

Copy link
Member Author

Choose a reason for hiding this comment

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

The length is the position of the last bound element. Thus it is safe to call the function with the correct len.

However, if one deliberately enters a too big len and i is not larger than len but already larger than the length then one enters the while loop, and this loop will be infinite.

i = i+1
res = ElmList(obj, i)
end
return res, (i+1, len)
end

function Base.iterate(obj::GapObj, iter::GapObj)
Expand Down
6 changes: 6 additions & 0 deletions test/adapter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
vs = Vector{Int}.(gap_iter)
@test vs == [[], [1], [1, 1], [1, 1, 1]]
end

# skip holes when iterating over GAP lists
@test xs == [x for x in s]
@test xs == map(x -> x, s)
l = GAP.evalstr("[1, 2,,,, 6]")
@test collect(l) == [1, 2, 6]
end

@testset "deepcopy" begin
Expand Down
Loading