I am once again bemoaning the lack of list comprehensions in javascript. look at the difference between this typescript code:
let hosts = Array.from(networkScanner.getAllHosts(ns))
.filter(([server, _, __]) => canHack(ns, server.hostname))
.map(([server, _, __]) => ({
hostname: server.hostname,
value: getXpRate(ns, server, ns.getPlayer()),
}));
return util.min(hosts, (i) => i.value);
and this Python code:
hosts = [{"hostname": server.hostname,
"value": getXpRate(ns, server, ns.getPlayer()}
for [server, _, _] in networkScanner.getAllHosts()
if canHack(ns, server.hostname)]
return min(hosts, key=lambda i: i['value'])
like idk maybe this is just me and the typescript version is just as readable if you’re experienced in both languages, but it looks so hard-to-follow and cluttered with syntax to me (not to mention how dependent it is on context that changes with each method call) that it makes me not want to write declarative code like this at all and exclusively use loops, and that feels bad
also util.min() is a custom function that I had to write because javascript doesn’t seem to have any equivalent of Python’s min() function
(just for context, this code is for a programming idle game called Bitburner so that’s why it’s about hacking things lol)