Skip to content

Commit

Permalink
spaceship operator. 10'000 lines of code!
Browse files Browse the repository at this point in the history
  • Loading branch information
kspalaiologos committed Nov 12, 2022
1 parent 72f018c commit 7ef304d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class FunctionRegistry {
"n-prod", "Π", "unique-mask", "unique", "intersection", "union", "bernoulli", "regex-matches?",
"date:from", "date:time-from", "date:add", "date:difference", "date:hours",
"date:minutes", "date:seconds", "date:nanoseconds", "date:years", "date:months",
"date:days", "date:to-list", "time:day-of-week", "⍣"
"date:days", "date:to-list", "time:day-of-week", "⍣", "<=>", "⇔"
);

public static void registerDefault(Environment env) {
Expand Down Expand Up @@ -207,6 +207,8 @@ public static void registerDefault(Environment env) {
env.set("*", new Atom(new Star()));
env.set("/", new Atom(new Slash()));
env.set("mod", new Atom(new Mod()));
env.set("<=>", new Atom(new Spaceship()));
env.set("⇔", new Atom(new Spaceship()));

env.set("regex:matches?", new Atom(new RegexMatches()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;

public class Neq extends PrimitiveFunction implements Lambda {
private static final String name = "!=";
private static final String name = "/=";

@Override
protected String name() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package palaiologos.kamilalisp.runtime.math.cmp;

import com.google.common.collect.Streams;
import palaiologos.kamilalisp.atom.*;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

public class Spaceship extends PrimitiveFunction implements Lambda {
private static final String name = "<=>";

@Override
protected String name() {
return name;
}

private static Atom cmp2(Environment e, Atom a, Atom b) {
if (a.getType() == Type.REAL && b.getType() == Type.REAL) {
return new Atom(BigInteger.valueOf(a.getReal().compareTo(b.getReal())));
} else if (a.getType() == Type.REAL && b.getType() == Type.INTEGER) {
return new Atom(BigInteger.valueOf(a.getReal().compareTo(new BigDecimal(b.getInteger()))));
} else if (a.getType() == Type.INTEGER && b.getType() == Type.REAL) {
return new Atom(BigInteger.valueOf(new BigDecimal(a.getInteger()).compareTo(b.getReal())));
} else if (a.getType() == Type.INTEGER && b.getType() == Type.INTEGER) {
return new Atom(BigInteger.valueOf(a.getInteger().compareTo(b.getInteger())));
} else if (a.getType() == Type.STRING && b.getType() == Type.STRING) {
return new Atom(BigInteger.valueOf(a.getString().compareTo(b.getString())));
} else if (a.getType() == Type.LIST && (b.getType() == Type.REAL || b.getType() == Type.INTEGER || b.getType() == Type.STRING)) {
return new Atom(a.getList().stream().map(x -> cmp2(e, x, b)).toList());
} else if ((a.getType() == Type.REAL || a.getType() == Type.INTEGER || a.getType() == Type.STRING) && b.getType() == Type.LIST) {
return new Atom(b.getList().stream().map(x -> cmp2(e, a, x)).toList());
} else if (a.getType() == Type.LIST && b.getType() == Type.LIST) {
return new Atom(Streams.zip(a.getList().stream(), b.getList().stream(), (x, y) -> cmp2(e, x, y)).toList());
} else {
throw new UnsupportedOperationException(name + " not defined for: " + a.getType() + " and " + b.getType());
}
}

@Override
public Atom apply(Environment env, List<Atom> args) {
assertArity(args, 2);
return cmp2(env, args.get(0), args.get(1));
}
}

0 comments on commit 7ef304d

Please sign in to comment.