Created
September 25, 2023 22:13
-
-
Save mfelleisen/15a4591462463b9c3ff6d388246de92a to your computer and use it in GitHub Desktop.
the most conventional variant of classify in 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
// this classify uses conventional lists or wrapped lists as much as possible so that | |
// 1. the spirit of the exercise is preserved | |
// 2. the type system checks as many (and as few) claims about the code as Typed Racket | |
// 3. the performance characteritics are Java-ish. | |
// [Java's list are O-faster than Racket's for many operations but not all.] | |
import java.util.*; | |
class Main { | |
public static void main(String[] argv) { | |
if (argv.length==0) | |
check(); | |
else | |
stress_test(argv); | |
} | |
// stress test | |
static void stress_test(String[] argv) { | |
int size_of_list = new Integer(argv[1]).intValue(); | |
int number_of_uses = new Integer(argv[3]).intValue(); | |
System.out.println("stress testing a list of " + size_of_list + " elements, " + number_of_uses + "times"); | |
List<FMap1> l0 = FMap1.build(size_of_list); | |
long start = System.currentTimeMillis(); | |
run(l0,number_of_uses); | |
long end = System.currentTimeMillis(); | |
System.out.println("cpu time for just run: " + (end - start)); | |
} | |
static void run(List<FMap1> l0, int number_of_uses) { | |
FMap fake = new FMap("oops"); | |
for(int uses = 0; uses < number_of_uses; uses++) { | |
List<FMap> result = classify(l0); | |
fake = result.get(0); | |
} | |
} | |
// does it work? manually inspect output | |
static void check() { | |
List<FMap1> l0 = FMap1.build(2); | |
for(FMap fm : l0) { | |
System.out.println(fm); | |
} | |
System.out.println(classify(l0)); | |
} | |
// ----------------------------------------------------------------------------- | |
// NOT CHECKED contiguous sequences of identical keeys, uniqueness of keys | |
static public List<FMap> classify(List<FMap1> l0) { | |
if (l0.size() == 0) | |
throw new RuntimeException("non-empty list expected"); | |
return classify_workhorse(l0); | |
} | |
// KNOWN `l0` is not empty | |
// KNOWN each FMap in `l0` contains (at least) one number | |
static private List<FMap> classify_workhorse(List<FMap1> l0) { | |
List<FMap> global = new ArrayList<FMap>(); | |
String last_key = l0.get(0).key; | |
FMap local = new FMap(last_key); | |
for(FMap1 key_value_pair : l0) { | |
String current_key = key_value_pair.key; | |
Integer current_val = key_value_pair.value(); | |
if (current_key.equals(last_key)) { | |
local = local.extend(current_val); | |
} | |
else { | |
global.add(local); | |
last_key = current_key; | |
local = new FMap(last_key); | |
local = local.extend(current_val); | |
}; | |
}; | |
global.add(local); | |
return global; | |
} | |
// ----------------------------------------------------------------------------- | |
} | |
// ============================================================================= | |
// DIFFERENT PACKAGE | |
// ============================================================================= | |
class FMap { | |
String key; | |
protected List<Integer> values = new ArrayList<Integer>(); | |
FMap(String key) { | |
this.key = key; | |
} | |
FMap extend(Integer nu) { | |
this.values.add(nu); | |
return this; | |
} | |
public String toString() { | |
String array = this.values.toString(); | |
return "[" + this.key + " : " + array.substring(1,array.length()-1) + "]"; | |
} | |
} | |
// guarantee exactly one number in `values` | |
class FMap1 extends FMap { | |
FMap1(String key, int value) { | |
super(key); | |
this.values.add(new Integer(value)); | |
} | |
Integer value() { | |
return this.values.get(0); | |
} | |
// build the test case | |
static List<FMap1> build(int n) { | |
ArrayList<FMap1> As = make("A",2); multiply(n,As); | |
ArrayList<FMap1> Bs = make("B",4); multiply(n,Bs); | |
ArrayList<FMap1> Cs = make("C",3); multiply(n,Cs); | |
ArrayList<FMap1> result = new ArrayList<FMap1>(); | |
result.addAll(As); | |
result.addAll(Bs); | |
result.addAll(Cs); | |
return result; | |
} | |
// make a list of FMap1s like this: [key,1], ..., [key,n] | |
static ArrayList<FMap1> make(String key, int k) { | |
ArrayList<FMap1> result = new ArrayList<FMap1> (); | |
for(int i = 0; i < k; i++) { | |
FMap1 A = new FMap1(key,i+1); | |
result.add(A); | |
} | |
return result; | |
} | |
// add `n` copies of `result` to itself | |
static void multiply(int n, ArrayList<FMap1> result) { | |
ArrayList<FMap1> result2 = (ArrayList<FMap1>)result.clone(); | |
for(int i = 1; i < n; i++) { | |
result.addAll(result2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment