Skip to content

Commit

Permalink
Allow iterating over Julia objects from within GAP
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed Apr 12, 2023
1 parent 67d0cd9 commit 6738e87
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
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 );

0 comments on commit 6738e87

Please sign in to comment.