Skip to content

Commit

Permalink
fix monadic pervasive
Browse files Browse the repository at this point in the history
  • Loading branch information
kspalaiologos committed Jun 27, 2023
1 parent 917aa91 commit 22ce2ee
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,26 @@ public Complex apply() {
throw new RuntimeException("Expected at least one argument.");
}

@Override
public Atom apply(Environment env, List<Atom> args) {
if(args.size() == 0)
return toAtom(apply());
private Atom process(Environment env, List<Atom> args) {
ArrayList<Atom> list = new ArrayList<>(args.size());
for(Atom x : args) {
if(x.getType() == Type.LIST)
list.add(apply(env, x.getList()));
list.add(process(env, x.getList()));
else
list.add(Cmplx64AtomThunk.toAtom(apply(toComplex(x))));
}
return new Atom(list);
}

@Override
public Atom apply(Environment env, List<Atom> args) {
if(args.size() == 0)
return toAtom(apply());
else if(args.size() == 1)
return toAtom(apply(toComplex(args.get(0))));
else return process(env, args);
}

@Override
public String name() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,26 @@ public double apply() {
throw new RuntimeException("Expected at least one argument.");
}

@Override
public Atom apply(Environment env, List<Atom> args) {
if(args.size() == 0)
return toAtom(apply());
private Atom process(Environment env, List<Atom> args) {
ArrayList<Atom> list = new ArrayList<>(args.size());
for(Atom x : args) {
if(x.getType() == Type.LIST)
list.add(apply(env, x.getList()));
list.add(process(env, x.getList()));
else
list.add(toAtom(apply(toFloat(x))));
list.add(Flt64AtomThunk.toAtom(apply(toFloat(x))));
}
return new Atom(list);
}

@Override
public Atom apply(Environment env, List<Atom> args) {
if(args.size() == 0)
return toAtom(apply());
else if(args.size() == 1)
return toAtom(apply(toFloat(args.get(0))));
else return process(env, args);
}

@Override
public String name() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public static void register(Environment env) {
@Override
public double apply(double x, double y) { return Maja.mul(x, y); }
}));
env.setPrimitive("flt64:**", new Atom(new Flt64PervasiveDyadicFunction("flt64:**") {
@Override
public double apply(double x, double y) { return Maja.pow(x, y); }
}));
env.setPrimitive("flt64:/", new Atom(new Flt64PervasiveDyadicFunction("flt64:/") {
@Override
public double apply(double x, double y) { return Maja.div(x, y); }
Expand Down Expand Up @@ -681,6 +685,10 @@ public double apply() {
@Override
public Complex apply(Complex x, Complex y) { return Maja.mul(x, y); }
}));
env.setPrimitive("cmplx64:**", new Atom(new Cmplx64PervasiveDyadicFunction("cmplx64:**") {
@Override
public Complex apply(Complex x, Complex y) { return Maja.pow(x, y); }
}));
env.setPrimitive("cmplx64:/", new Atom(new Cmplx64PervasiveDyadicFunction("cmplx64:/") {
@Override
public Complex apply(Complex x, Complex y) { return Maja.div(x, y); }
Expand Down

0 comments on commit 22ce2ee

Please sign in to comment.