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

Allow iterating over Julia objects from within GAP #861

Merged
merged 1 commit into from
Apr 12, 2023
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes in GAP.jl

## Version 0.9.5-DEV (released YYYY-MM-DD)

- Allow iterating over Julia objects from within GAP

## Version 0.9.4 (released 2023-03-04)

- Tweak @gapattribute to not require `import GAP: @gapwrap`
Expand Down
38 changes: 38 additions & 0 deletions pkg/JuliaInterface/gap/adapter.gi
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,44 @@ InstallOtherMethod( \.\:\=,
end );


#############################################################################
##
## Iterating over Julia objects
##
BindGlobal("NextIterator_Julia", function(it)
local res;
res := it!.state[1];
it!.state := Julia.iterate(it!.obj, it!.state[2]);
return res;
end);

BindGlobal("IsDoneIterator_Julia", function(it)
return Julia.isnothing(it!.state);
end);

BindGlobal("ShallowCopy_Julia", function(it)
return rec(NextIterator := it!.NextIterator,
IsDoneIterator := it!.IsDoneIterator,
ShallowCopy := it!.ShallowCopy,
state := Julia.Base.deepcopy(it!.state),
obj := it!.obj,
);
end);

InstallOtherMethod( Iterator,
"for a Julia object",
[ IsJuliaObject ],
function(obj)
return IteratorByFunctions(rec(
NextIterator := NextIterator_Julia,
IsDoneIterator := IsDoneIterator_Julia,
ShallowCopy := ShallowCopy_Julia,
state := Julia.iterate(obj),
obj := obj,
));
end);


#############################################################################
##
## Create random numbers via Julia random number generators.
Expand Down
9 changes: 9 additions & 0 deletions pkg/JuliaInterface/tst/adapter.tst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ gap> TestBinOp({a,b} -> a = b);
#
gap> l := GAPToJulia([1, 2, 3]);
<Julia: Any[1, 2, 3]>
gap> for i in l do Display(i); od;
1
2
3
gap> l[1];
1
gap> l[2] := false;
Expand All @@ -101,6 +105,11 @@ gap> l;
#
gap> m:=JuliaEvalString("[1 2; 3 4]");
<Julia: [1 2; 3 4]>
gap> for i in m do Display(i); od;
1
3
2
4
gap> m[3];
2
gap> m[1,2];
Expand Down
7 changes: 7 additions & 0 deletions pkg/JuliaInterface/tst/convert.tst
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,12 @@ gap> dict:= GAPToJulia( rec( juliafunc:= Julia.Base.map,
gap> JuliaToGAP( IsRecord, dict );
rec( juliafunc := <Julia: map> )

# iterating over dict gives key-value pairs
gap> dict:= GAPToJulia( rec( a := 1, b := 2 ) );
<Julia: Dict{Symbol, Any}(:a => 1, :b => 2)>
gap> for i in dict do Display(i); od;
Pair{Symbol, Any}(:a, 1)
Pair{Symbol, Any}(:b, 2)

##
gap> STOP_TEST( "convert.tst", 1 );