Range types, a function that takes any number of items, and failures.
inventory.zg
# An inventory: range types, a function that takes any number of items, and failures.structItem{name:str.utf8,grams:int32[min1,max50_000]}fnheaviest[...'items':Item]->Itemorfail[none]{iflist.length[lend'items']equal_to0{failnone;}letmut'best':=list.remove[lend.mut'items',pos1];for'it'inlend'items'{if'it'.grams>'best'.grams{change'best'=copy'it';}}returngive'best';}fnkilos['grams':int32]->int32orfail[not_whole, out_of_range]{let'k':bin64=to.bin64['grams']div.near1000.0;returnto.int32['k']otherwisefail;}fninitial[lent'name':str.utf8]->char.point{let'first':=std::str.at.point[lend'name',pos1]otherwise`?`;let'code':=char.code['first'];if'code'at_least97and'code'at_most122{returnchar.point.from_code['code'minus32]otherwise`?`;}return'first';}MAIN{letmut'shelf':list[Item][min0,max4]=list.empty;list.append[lend.mut'shelf',valueItem[name="rope",grams=1_250]];list.append[lend.mut'shelf',valueItem[name="helmet",grams=900]];list.append[lend.mut'shelf',valueItem[name="tent",grams=4_000]];let'count':=list.length[lend'shelf'];let'left':int8=4minus'count';std::print.useful['count'," items, room for ",'left'," more",newln];let'top':=heaviest[...copy'shelf']otherwise{none{std::print.useful["empty shelf",newln];std::exit[0];}};std::print.useful["heaviest: ",initial[lend'top'.name]," ",'top'.name,newln];let'kg':=kilos['top'.grams]otherwise-1;std::print.useful['top'.name," is ",'kg'," kg",newln];let'gone':=list.remove[lend.mut'shelf',pos2];std::print.useful["removed ",'gone'.name,", ",list.length[lend'shelf']," left",newln];}
Output
3 items, room for 1 more
heaviest: T tent
tent is 4 kg
removed helmet, 2 left