RosettaCodeData/Task/Read-a-configuration-file/Pluto/read-a-configuration-file.pluto
2026-04-30 12:34:36 -04:00

56 lines
2.1 KiB
Text

class configuration
function __construct(map)
self.full_name = map["full_name"]
self.favourite_fruit = map["favourite_fruit"]
self.needs_peeling = map["needs_peeling"]
self.seeds_removed = map["seeds_removed"]
self.other_family = map["other_family"]
end
function __tostring()
return {
$"Full name = {self.full_name}",
$"Favourite fruit = {self.favourite_fruit}",
$"Needs peeling = {self.needs_peeling}",
$"Seeds removed = {self.seeds_removed}",
$"Other family = {self.other_family:concat()}"
}:concat("\n")
end
end
class map_entry
function __construct(public key, public value) end
end
local commented_out = |line| -> line:startswith("#") or line:startswith(";")
local function to_map_entry(line)
local ix = line:find(" ", 1, true)
if !ix then return new map_entry(line, "") end
return new map_entry(line:sub(1, ix - 1), line:sub(ix + 1))
end
local filename = "configuration.txt"
local nl = os.platform == "windows" ? "\r\n" : "\n"
local lines = io.contents(filename):rstrip():split(nl)
local map_entries = lines:map(|line| -> line:strip())
:filter(|line| -> line != ""):reorder()
:filter(|line| -> !commented_out(line)):reorder()
:map(|line| -> to_map_entry(line))
local configuration_map = { needs_peeling = false, seeds_removed = false }
for map_entries as me do
if me.key == "FULLNAME" then
configuration_map["full_name"] = me.value
elseif me.key == "FAVOURITEFRUIT" then
configuration_map["favourite_fruit"] = me.value
elseif me.key == "NEEDSPEELING" then
configuration_map["needs_peeling"] = true
elseif me.key == "OTHERFAMILY" then
configuration_map["other_family"] = me.value:split(" , "):map(|s| -> s:strip())
elseif me.key == "SEEDSREMOVED" then
configuration_map["seeds_removed"] = true
else
print($"Encountered unexpected key {me.key} = {me.value}")
end
end
print(new configuration(configuration_map))