Skip to content

Commit

Permalink
Use FunctionalInterface for Python function.
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Nov 15, 2024
1 parent 718c0c8 commit 0ef5304
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@Singleton
public class PyGalServiceMixed implements PyGalService {
private final Value pythonFunctionXY;
private final RenderXYFunction pythonFunctionXY;

PyGalServiceMixed(GraalPyContext graalPyContext) {
pythonFunctionXY = graalPyContext.eval(
Expand All @@ -29,12 +29,17 @@ def render_xy(title, label_datapoint_entries):
xy_chart.add(entry.label(), entry.dataPoints())
return xy_chart.render().decode()
render_xy""");
render_xy""").as(RenderXYFunction.class);
}

public record Entry(String label, double[][] dataPoints) {
}

@FunctionalInterface
public interface RenderXYFunction {
String apply(String title, List<Entry> labelDatapointEntries);
}

@Override
public String renderXYChart() {
String title = "XY Cosinus";
Expand All @@ -46,6 +51,6 @@ public String renderXYChart() {
new Entry("y = 1", new double[][]{{-5, 1}, {5, 1}}),
new Entry("y = -1", new double[][]{{-5, -1}, {5, -1}})
);
return pythonFunctionXY.execute(title, labelDatapointEntries).asString();
return pythonFunctionXY.apply(title, labelDatapointEntries);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@Service
public class PyGalServiceMixed implements PyGalService {
private final Value pythonFunctionXY;
private final RenderXYFunction pythonFunctionXY;

PyGalServiceMixed(GraalPyContext graalPyContext) {
pythonFunctionXY = graalPyContext.eval(
Expand All @@ -30,12 +30,18 @@ def render_xy(title, label_datapoint_entries):
xy_chart.add(entry.label(), entry.dataPoints())
return xy_chart.render().decode()
render_xy""");
render_xy""").as(RenderXYFunction.class);
}

public record Entry(String label, double[][] dataPoints) {
}


@FunctionalInterface
public interface RenderXYFunction {
String apply(String title, List<Entry> labelDatapointEntries);
}

@Override
public String renderXYChart() {
String title = "XY Cosinus";
Expand All @@ -47,6 +53,6 @@ public String renderXYChart() {
new Entry("y = 1", new double[][]{{-5, 1}, {5, 1}}),
new Entry("y = -1", new double[][]{{-5, -1}, {5, -1}})
);
return pythonFunctionXY.execute(title, labelDatapointEntries).asString();
return pythonFunctionXY.apply(title, labelDatapointEntries);
}
}

0 comments on commit 0ef5304

Please sign in to comment.