Guest User

Untitled

a guest
Jan 22nd, 2026
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.28 KB | None | 0 0
  1. // Type Classes Demo: Ad-hoc polymorphism without inheritance!
  2.  
  3. interface Serializable<T> {
  4.     String serialize(T value);
  5.     T deserialize(String data);
  6. }
  7.  
  8. interface Numeric<T> {
  9.     T zero();
  10.     T add(T a, T b);
  11.     T multiply(T a, T b);
  12. }
  13.  
  14. // Money type with serialization
  15. record Money(int dollars, int cents) {
  16.     __witness Serializable<Money> SERIALIZER = new Serializable<>() {
  17.         public String serialize(Money m) {
  18.             return m.dollars() + "." + String.format("%02d", m.cents());
  19.         }
  20.         public Money deserialize(String s) {
  21.             String[] parts = s.split("\\.");
  22.             return new Money(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
  23.         }
  24.     };
  25.  
  26.     __witness Numeric<Money> MATH = new Numeric<>() {
  27.         public Money zero() { return new Money(0, 0); }
  28.         public Money add(Money a, Money b) {
  29.             int totalCents = (a.dollars() * 100 + a.cents()) + (b.dollars() * 100 + b.cents());
  30.             return new Money(totalCents / 100, totalCents % 100);
  31.         }
  32.         public Money multiply(Money a, Money b) {
  33.             int totalCents = ((a.dollars() * 100 + a.cents()) * (b.dollars() * 100 + b.cents())) / 100;
  34.             return new Money(totalCents / 100, totalCents % 100);
  35.         }
  36.     };
  37. }
  38.  
  39. // Point type with serialization
  40. record Point(int x, int y) {
  41.     __witness Serializable<Point> SERIALIZER = new Serializable<>() {
  42.         public String serialize(Point p) {
  43.             return "(" + p.x() + "," + p.y() + ")";
  44.         }
  45.         public Point deserialize(String s) {
  46.             String[] parts = s.replace("(", "").replace(")", "").split(",");
  47.             return new Point(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
  48.         }
  49.     };
  50.  
  51.     __witness Numeric<Point> MATH = new Numeric<>() {
  52.         public Point zero() { return new Point(0, 0); }
  53.         public Point add(Point a, Point b) {
  54.             return new Point(a.x() + b.x(), a.y() + b.y());
  55.         }
  56.         public Point multiply(Point a, Point b) {
  57.             return new Point(a.x() * b.x(), a.y() * b.y());
  58.         }
  59.     };
  60. }
  61.  
  62. public class TypeClassDemo {
  63.  
  64.     // Demo: Money operations
  65.     static void moneyDemo() {
  66.         System.out.println("💰 MONEY OPERATIONS:");
  67.  
  68.         Serializable<Money> ser = Serializable<Money>.__witness;
  69.         Numeric<Money> math = Numeric<Money>.__witness;
  70.  
  71.         Money price1 = new Money(10, 50);
  72.         Money price2 = new Money(5, 75);
  73.  
  74.         System.out.println("  Price 1: $" + ser.serialize(price1));
  75.         System.out.println("  Price 2: $" + ser.serialize(price2));
  76.  
  77.         Money total = math.add(price1, price2);
  78.         System.out.println("  Total:   $" + ser.serialize(total));
  79.  
  80.         // Serialize and deserialize
  81.         String saved = ser.serialize(total);
  82.         Money loaded = ser.deserialize(saved);
  83.         System.out.println("  Saved & loaded: $" + ser.serialize(loaded));
  84.     }
  85.  
  86.     // Demo: Point operations
  87.     static void pointDemo() {
  88.         System.out.println("\n📍 POINT OPERATIONS:");
  89.  
  90.         Serializable<Point> ser = Serializable<Point>.__witness;
  91.         Numeric<Point> math = Numeric<Point>.__witness;
  92.  
  93.         Point p1 = new Point(3, 4);
  94.         Point p2 = new Point(5, 6);
  95.  
  96.         System.out.println("  Point 1: " + ser.serialize(p1));
  97.         System.out.println("  Point 2: " + ser.serialize(p2));
  98.  
  99.         Point sum = math.add(p1, p2);
  100.         System.out.println("  Sum:     " + ser.serialize(sum));
  101.  
  102.         Point product = math.multiply(p1, p2);
  103.         System.out.println("  Product: " + ser.serialize(product));
  104.  
  105.         // Serialize and deserialize
  106.         String saved = ser.serialize(sum);
  107.         Point loaded = ser.deserialize(saved);
  108.         System.out.println("  Saved & loaded: " + ser.serialize(loaded));
  109.     }
  110.  
  111.     // Demo: Sum multiple values
  112.     static void sumDemo() {
  113.         System.out.println("\n➕ SUM MULTIPLE VALUES:");
  114.  
  115.         Numeric<Money> moneyMath = Numeric<Money>.__witness;
  116.         Serializable<Money> moneySer = Serializable<Money>.__witness;
  117.  
  118.         Money[] prices = {
  119.             new Money(10, 99),
  120.             new Money(5, 50),
  121.             new Money(3, 25),
  122.             new Money(7, 10)
  123.         };
  124.  
  125.         Money total = moneyMath.zero();
  126.         for (Money price : prices) {
  127.             System.out.println("  + $" + moneySer.serialize(price));
  128.             total = moneyMath.add(total, price);
  129.         }
  130.         System.out.println("  ──────────");
  131.         System.out.println("  = $" + moneySer.serialize(total));
  132.     }
  133.  
  134.     public static void main(String[] args) {
  135.         System.out.println("╔══════════════════════════════════════╗");
  136.         System.out.println("║   TYPE CLASSES DEMO - VALHALLA JDK   ║");
  137.         System.out.println("╚══════════════════════════════════════╝");
  138.         System.out.println("\nType classes enable ad-hoc polymorphism!");
  139.         System.out.println("Same interface, different implementations per type.\n");
  140.  
  141.         moneyDemo();
  142.         pointDemo();
  143.         sumDemo();
  144.  
  145.         System.out.println("\n✨ No inheritance required! Type classes FTW! ✨");
  146.     }
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment