Inventory Ledger
inventory_ledger.nr179 lines
inventory_ledger.nr· neuro
// A ledger on the standard collections, alongside earlier features. Integration
// example: a `Vec` of `Copy` structs, a `Vec<string>`, a `HashMap<string, i32>` index, a
// key-ordered `BTreeMap` report, an enum with `match`, `Option` and `??` reads, and `for`
// in all three receiver modes over the `Vec` and the maps. `for x in v` consumes the names,
// `for x in &v` reads the items in place, and `for x in &mut v` restocks them; a map head
// binds each entry as a `(key, value)` pair, and `.keys()` / `.values()` lend one half.
@derive(Copy, Clone)
struct Item {
unit_weight: i32,
quantity: i32
}
impl Item {
// Total shelf weight of this line item.
func weight(&self) -> i32 {
self.unit_weight * self.quantity
}
func is_bulk(&self) -> bool {
self.quantity >= 10
}
// Classify this line item by the two properties above.
func shelf(&self) -> Shelf {
if self.is_bulk() {
return Shelf::Bulk
}
if self.weight() >= 40 {
return Shelf::Heavy
}
Shelf::Light
}
}
enum Shelf {
Light,
Heavy,
Bulk
}
// Shelves carry different handling surcharges.
func surcharge(shelf: Shelf) -> i32 {
match shelf {
Shelf::Light => 1
Shelf::Heavy => 5
Shelf::Bulk => 3
}
}
func main() -> i32 {
// The ledger itself: line items in arrival order, plus a name index into it.
mut items: Vec<Item> = Vec::new()
mut names: Vec<string> = Vec::new()
mut index: HashMap<string, i32> = HashMap::new()
names.push("bolt")
names.push("girder")
names.push("washer")
val weights: [i32; 3] = [2, 45, 1]
val quantities: [i32; 3] = [12, 1, 4]
mut slot: i32 = 0
for name in names {
val item = Item {
unit_weight: weights[slot],
quantity: quantities[slot]
}
items.push(item)
index.insert(name, slot)
slot += 1
}
// Total weight, and a weight-keyed report the ordered map keeps sorted.
mut by_weight: BTreeMap<i32, i32> = BTreeMap::new()
mut total_weight: i32 = 0
for item in &items {
val w = item.weight()
total_weight += w
by_weight.insert(w, item.quantity)
}
// bolt 24, girder 45, washer 4 -> total 73
// Look a line item up by name, through the index.
mut girder_weight: i32 = 0
match index.get("girder") {
Option::Some(at) => { girder_weight = items[*at].weight() }
Option::None => { girder_weight = -1 }
}
// girder_weight == 45
// A miss is an ordinary value, not a trap.
mut missing: i32 = 0
match index.get("rivet") {
Option::Some(_) => { missing = -1 }
Option::None => { missing = 1 }
}
// `??` reads the same fallible lookups without a `match` when the only thing the
// absent case needs is a default. It chains right-to-left, so the second lookup
// only runs after the first has come up empty.
val bolt_at = index.get("bolt") ?? -1 // 0
val rivet_at = index.get("rivet") ?? index.get("washer") ?? -1 // 2
// Surcharges, driven by the enum classifier.
mut fees: i32 = 0
for item in &items {
fees += surcharge(item.shelf())
}
// bulk(3) + heavy(5) + light(1) = 9
// The ordered map reports ascending weights, so the last key is the heaviest.
mut heaviest: i32 = 0
for w in by_weight.keys() {
heaviest = *w
}
// heaviest == 45
// Restock in place: `&mut items` lends each slot, so the write lands in the Vec
// itself, and `&items` reads the result back without consuming it.
for item in &mut items {
if !item.is_bulk() {
item.quantity += 2
}
}
mut restocked: i32 = 0
for item in &items {
restocked += item.weight()
}
// bolt 24, girder 3 x 45 = 135, washer 6 x 1 = 6 -> 165
val lines = items.len() as i32 // 3
val indexed = index.len() as i32 // 3
// Map heads. `&index` lends each entry, `&mut by_weight` doubles the quantities in
// place, `.values()` reads them back through a head filter, and a by-value walk
// consumes the index, moving each name out.
mut name_bytes: i32 = 0
for (name, at) in &index {
name_bytes += name.len() as i32 * (*at + 1)
}
// bolt 4 x 1 + girder 6 x 2 + washer 6 x 3 = 34
for (_, quantity) in &mut by_weight {
*quantity *= 2
}
mut doubled: i32 = 0
for quantity in by_weight.values().filter(|q| q > 2) {
doubled += *quantity
}
// 12, 1, 4 doubled to 24, 2, 8; the filter keeps 24 + 8 = 32
mut long_names: i32 = 0
for (name, _) in index {
if name.len() > 4 {
long_names += 1
}
}
// girder and washer: 2
println("Vec<Item> lines = {lines}")
println("HashMap index entries = {indexed}")
println("total weight = {total_weight}")
println("index.get(girder) -> weight= {girder_weight}")
println("index.get(rivet) -> miss = {missing}")
println("?? bolt = {bolt_at}")
println("?? rivet, then washer = {rivet_at}")
println("surcharges = {fees}")
println("BTreeMap heaviest key = {heaviest}")
println("restocked weight = {restocked}")
println("index name bytes x slot = {name_bytes}")
println("doubled quantities over 2 = {doubled}")
println("names longer than 4 = {long_names}")
// 73 + 45 + 1 + 9 + 45 + 3 + 3 + 0 + 2 + 34 + 32 + 2 = 249
val total = total_weight + girder_weight + missing + fees + heaviest + lines +
indexed + bolt_at + rivet_at + name_bytes + doubled + long_names
println("total = {total}")
total
}
neuroc run inventory_ledger.nr
Vec<Item> lines = 3
HashMap index entries = 3
total weight = 73
index.get(girder) -> weight= 45
index.get(rivet) -> miss = 1
?? bolt = 0
?? rivet, then washer = 2
surcharges = 9
BTreeMap heaviest key = 45
restocked weight = 165
index name bytes x slot = 34
doubled quantities over 2 = 32
names longer than 4 = 2
total = 249