Skip to content

Instantly share code, notes, and snippets.

@sebkraemer
Last active October 9, 2022 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebkraemer/ae2b3cdad8d92f67be6df30685c20e1b to your computer and use it in GitHub Desktop.
Save sebkraemer/ae2b3cdad8d92f67be6df30685c20e1b to your computer and use it in GitHub Desktop.
mic lower_bound
import 'package:collection/collection.dart';
class Mitarbeiter {
int gehalt;
String name;
Mitarbeiter(this.gehalt, this.name);
}
int micLowerBound<E, V>(List<E> sortedList, V value, {int compare(E, V)?}) {
if (compare == null) {
return 0; // todo call standard compare
} else {
return compare(sortedList.first, value); // todo implement lower_bound properly
}
}
void runLowerBounds() {
var mList = [Mitarbeiter(50000, "karl"), Mitarbeiter(100000, "boss")];
var m3 = Mitarbeiter(65000, "lenny");
var i = mList.lowerBound(m3, (p0, p1) => p0.gehalt - p1.gehalt);
// : Invalid argument: Instance of '_Type'Error: Invalid argument: Instance of '_Type'
var i2 = micLowerBound(mList, 65000,
compare: (mitarbeiter, gehalt) => mitarbeiter.gehalt - gehalt);
print('lowerBound: $i');
print('lowerBoundBy: $i2');
}
void main() {
runLowerBounds();
}
@sebkraemer
Copy link
Author

sebkraemer commented Oct 9, 2022

The gist shows how a lower_bound could be implemented that does not need to create an object of type E just for comparison.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment