Rewrite main.scm so it does not read the whole wordlist at once
This commit is contained in:
parent
23d993657c
commit
39cbc34e18
3 changed files with 46 additions and 33 deletions
3
.envrc
3
.envrc
|
|
@ -1 +1,4 @@
|
||||||
use flake
|
use flake
|
||||||
|
|
||||||
|
export CHICKEN_REPOSITORY_PATH="$(pwd)/.eggs"
|
||||||
|
export CHICKEN_INSTALL_REPOSITORY="$(pwd)/.eggs"
|
||||||
|
|
|
||||||
30
flake.nix
30
flake.nix
|
|
@ -30,28 +30,32 @@
|
||||||
};
|
};
|
||||||
in {
|
in {
|
||||||
devShells.default = pkgs.mkShell rec {
|
devShells.default = pkgs.mkShell rec {
|
||||||
buildInputs = [
|
buildInputs = with pkgs; [
|
||||||
lua
|
chicken
|
||||||
# fennel
|
openssl
|
||||||
pkgs.chicken
|
gcc
|
||||||
# pkgs.guile_3_0
|
|
||||||
# pkgs.gcc
|
|
||||||
# pkgs.gambit
|
|
||||||
];
|
];
|
||||||
LD_LIBRARY_PATH = "${lib.makeLibraryPath buildInputs}";
|
LD_LIBRARY_PATH = "${lib.makeLibraryPath buildInputs}";
|
||||||
};
|
};
|
||||||
packages.default = pkgs.mkDerivation rec {
|
packages.default = pkgs.stdenv.mkDerivation rec {
|
||||||
buildInputs = [
|
name = "seizethemeans";
|
||||||
pkgs.chicken
|
src = ./.;
|
||||||
# pkgs.guile_3_0
|
|
||||||
pkgs.gcc
|
buildInputs = with pkgs; [
|
||||||
pkgs.gambit
|
chicken
|
||||||
|
openssl
|
||||||
|
gcc
|
||||||
];
|
];
|
||||||
|
|
||||||
buildPhase = ''
|
buildPhase = ''
|
||||||
chicken ./main.scm -explicit-use
|
chicken ./main.scm -explicit-use
|
||||||
gcc
|
gcc
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
installPhase = ''
|
||||||
|
mkdir -p $out/bin
|
||||||
|
cp main $out/bin/seize-the-means
|
||||||
|
'';
|
||||||
|
|
||||||
LD_LIBRARY_PATH = "${lib.makeLibraryPath buildInputs}";
|
LD_LIBRARY_PATH = "${lib.makeLibraryPath buildInputs}";
|
||||||
};
|
};
|
||||||
|
|
|
||||||
46
main.scm
46
main.scm
|
|
@ -1,34 +1,40 @@
|
||||||
(import (chicken io)
|
(import (chicken condition)
|
||||||
|
(chicken io)
|
||||||
(chicken process-context)
|
(chicken process-context)
|
||||||
(chicken random)
|
(chicken random)
|
||||||
(chicken format))
|
(chicken format))
|
||||||
|
|
||||||
(define *api-url* (get-environment-variable "MASTODON_API_URL"))
|
(define *api-url* (get-environment-variable "MASTODON_API_URL"))
|
||||||
(define *access-token* (get-environment-variable "MASTODON_ACCESS_TOKEN"))
|
(define *access-token* (get-environment-variable "MASTODON_ACCESS_TOKEN"))
|
||||||
|
(define file "./words.txt")
|
||||||
|
|
||||||
(define (file-lines file-path)
|
(define (file-num-lines file-path)
|
||||||
(let ((file (open-input-file file-path)))
|
(let ((file (open-input-file file-path)))
|
||||||
(let loop ((lines '())
|
(let loop ((num-lines 0))
|
||||||
(cur (read-line file)))
|
(if (eof-object? (read-line file))
|
||||||
(if (eof-object? cur)
|
|
||||||
(begin (close-input-port file)
|
(begin (close-input-port file)
|
||||||
(reverse lines))
|
num-lines)
|
||||||
(loop (cons cur lines)
|
(loop (+ 1 num-lines))))))
|
||||||
(read-line file))))))
|
|
||||||
|
|
||||||
(define lines (file-lines "./words.txt"))
|
(define (file-line-at file-path n)
|
||||||
|
(let ((file (open-input-file file-path)))
|
||||||
|
(let loop ((m 0)
|
||||||
|
(cur (read-line file)))
|
||||||
|
(cond
|
||||||
|
((= m n)
|
||||||
|
(begin
|
||||||
|
(close-input-port file)
|
||||||
|
cur))
|
||||||
|
|
||||||
(define (list-nth ls n)
|
((eof-object? cur)
|
||||||
(if (zero? n)
|
(begin
|
||||||
(car ls)
|
(close-input-port file)
|
||||||
(list-nth (cdr ls) (- n 1))))
|
(signal (format "~s is greater than the number of lines in ~s" n file-path))))
|
||||||
|
|
||||||
|
(else (loop (+ 1 m) (read-line file)))))))
|
||||||
|
|
||||||
(define (list-remove-nth ls n)
|
(let* ((n (pseudo-random-integer (file-num-lines file)))
|
||||||
(if (zero? n)
|
(line (file-line-at file n)))
|
||||||
(cdr ls)
|
|
||||||
(cons (car ls) (list-remove-nth (cdr ls) (- n 1)))))
|
|
||||||
|
|
||||||
(let ((nth (pseudo-random-integer (length lines))))
|
|
||||||
(display "Seize the means of ")
|
(display "Seize the means of ")
|
||||||
(display (list-nth lines nth))
|
(display line)
|
||||||
(display "\n"))
|
(display "\n"))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue