use std/list module for map and memberOf?; some minor cleanup

This commit is contained in:
darkf 2014-02-12 02:41:56 -08:00
parent 49cdd857ee
commit 578d43f7c4
1 changed files with 18 additions and 26 deletions

View File

@ -1,3 +1,5 @@
import("std/list").
-- config constants -- config constants
HOST = "127.0.0.1". HOST = "127.0.0.1".
@ -11,14 +13,13 @@ ADMINS = ["darkf"].
CHANS = ["#lobby"]. CHANS = ["#lobby"].
-- the lexical environment that $eval uses -- the lexical environment that $eval uses
EVAL_ENV = [("loop", loop), EVAL_ENV = [("id", id),
("loop", loop),
("repr", repr), ("repr", repr),
("itos", itos)]. ("itos", itos),
("globals", globals),
-- First we'll define some helper functions ("locals", locals),
("list", list)].
map(f, []) -> [].
map(f, x::xs) -> f(x) :: map(f, xs).
fst((x, _)) -> x. fst((x, _)) -> x.
@ -32,12 +33,6 @@ is_nothing(_) -> false.
unwrap_maybe(("just", x)) -> x. unwrap_maybe(("just", x)) -> x.
-- list membership test
is_member([], _) -> false.
is_member(x::xs, member) ->
if x == member then true
else is_member(xs, member).
-- association list -- association list
-- insert a pair into a map -- insert a pair into a map
@ -63,9 +58,7 @@ splitirc'(" "::xs, stracc, acc) -> do
splitirc'(xs, "", acc + [stracc]) splitirc'(xs, "", acc + [stracc])
end. end.
-- prefix message -- prefix message
splitirc'(":"::xs, _, acc) -> do splitirc'(":"::xs, _, acc) -> acc + [xs].
acc + [xs]
end.
splitirc'(x::xs, stracc, acc) -> splitirc'(xs, stracc + x, acc). splitirc'(x::xs, stracc, acc) -> splitirc'(xs, stracc + x, acc).
-- helper function -- helper function
splitirc(str) -> splitirc'(str, "", []). splitirc(str) -> splitirc'(str, "", []).
@ -81,9 +74,7 @@ ircnick'("!"::xs, acc) -> acc.
ircnick'(x::xs, acc) -> ircnick'(xs, acc + x). ircnick'(x::xs, acc) -> ircnick'(xs, acc + x).
ircnick(str) -> ircnick'(str, ""). ircnick(str) -> ircnick'(str, "").
isAdmin(nick) -> do isAdmin(nick) -> list\memberOf?(ADMINS, nick).
is_member(ADMINS, nick)
end.
-- state getters -- state getters
getFactoids(state) -> do getFactoids(state) -> do
@ -100,7 +91,7 @@ end.
-- basic "key value" (space-separated) format -- basic "key value" (space-separated) format
saveFactoids(factoids) -> do saveFactoids(factoids) -> do
file = fopen("factoids.txt", "w"); file = fopen("factoids.txt", "w");
map(\(k,v) -> fputstr(file, k + " " + v + "\n"), factoids); list\map(\(k,v) -> fputstr(file, k + " " + v + "\n"), factoids);
fclose(file) fclose(file)
end. end.
@ -121,7 +112,7 @@ end.
say(chan, msg) -> fputstr(sock, "PRIVMSG " + chan + " :" + msg + "\r\n"). say(chan, msg) -> fputstr(sock, "PRIVMSG " + chan + " :" + msg + "\r\n").
handleMessage(s, nick, chan, "$factoids") -> do handleMessage(s, nick, chan, "$factoids") -> do
factoids = map(fst, getFactoids(s)); factoids = list\map(fst, getFactoids(s));
say(chan, nick + ": " + repr(factoids)); say(chan, nick + ": " + repr(factoids));
s s
end. end.
@ -258,17 +249,18 @@ sock = sockopen(HOST, PORT).
fputstr(sock, "PASS " + NICK + "\r\n"). fputstr(sock, "PASS " + NICK + "\r\n").
fputstr(sock, "NICK " + NICK + "\r\n"). fputstr(sock, "NICK " + NICK + "\r\n").
fputstr(sock, "USER " + NICK + " 0 * :Lamb Da. Bot\r\n"). fputstr(sock, "USER " + NICK + " 0 * :Lamb Da. Bot\r\n").
map(\chan -> fputstr(sock, "JOIN " + chan + "\r\n"), CHANS).
-- note: workaround for issue #19 (passing lambdas to modules in the global scope is incorrect)
joinChans() -> list\map(\chan -> fputstr(sock, "JOIN " + chan + "\r\n"), CHANS).
joinChans().
-- loop receiving lines -- loop receiving lines
mainloop(state) -> mainloop(state) ->
if feof(sock) != true then if feof(sock) != true then do
do
line = fgetline(sock); line = fgetline(sock);
handleLine(state, line) handleLine(state, line)
end end
else else false. -- EOF, stop looping
false.
putstrln("initializing"). putstrln("initializing").
initialState = (loadFactoids(),). initialState = (loadFactoids(),).