RosettaCodeData/Task/Priority-queue/Arturo/priority-queue.arturo

52 lines
963 B
Text
Raw Permalink Normal View History

2026-02-01 16:33:20 -08:00
define :item [
init: method [priority, value][
this\priority: priority
this\value: value
]
string: method [][
2023-07-01 11:58:00 -04:00
~"(|this\priority|, |this\value|)"
]
]
2026-02-01 16:33:20 -08:00
define :queue [
init: method [items][
this\items: arrange items 'it -> it\priority
2023-07-01 11:58:00 -04:00
]
2026-02-01 16:33:20 -08:00
empty?: method [][
zero? this\items
]
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
push: method [item][
this\items: this\items ++ item
this\items: arrange this\items 'it -> it\priority
]
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
pop: method [][
ensure [not? this\empty?]
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
result: this\items\0
this\items: remove.index this\items 0
return result
]
2023-07-01 11:58:00 -04:00
]
Q: to :queue @[to [:item] [
[3 "Clear drains"]
[4 "Feed cat"]
[5 "Make tea"]
[1 "Solve RC tasks"]
]]
2026-02-01 16:33:20 -08:00
do ::
Q\push to :item [2 "Tax return"]
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
print ["queue is empty?" Q\empty?]
print ""
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
while [not? Q\empty?]->
print ["task:" Q\pop]
2023-07-01 11:58:00 -04:00
2026-02-01 16:33:20 -08:00
print ""
print ["queue is empty?" Q\empty?]