-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spaceship operator. 10'000 lines of code!
- Loading branch information
1 parent
72f018c
commit 7ef304d
Showing
3 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/palaiologos/kamilalisp/runtime/math/cmp/Spaceship.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |