From 77812ffdd5b70a332a9947b3c39fe764a8795624 Mon Sep 17 00:00:00 2001 From: Pedro Correa Date: Thu, 22 May 2025 11:34:10 -0300 Subject: [PATCH] :sparkles: (Janet) chapter 6 and 7 --- Janet/chapter6/hosts.janet | 20 ++++++++++++++++++ Janet/chapter6/loop.janet | 40 ++++++++++++++++++++++++++++++++++++ Janet/chapter7/helpers.janet | 5 +++++ Janet/chapter7/main.janet | 20 ++++++++++++++++++ Janet/chapter7/private.janet | 6 ++++++ Janet/chapter7/project.janet | 9 ++++++++ 6 files changed, 100 insertions(+) create mode 100644 Janet/chapter6/hosts.janet create mode 100644 Janet/chapter6/loop.janet create mode 100644 Janet/chapter7/helpers.janet create mode 100644 Janet/chapter7/main.janet create mode 100644 Janet/chapter7/private.janet create mode 100644 Janet/chapter7/project.janet diff --git a/Janet/chapter6/hosts.janet b/Janet/chapter6/hosts.janet new file mode 100644 index 0000000..1b9586c --- /dev/null +++ b/Janet/chapter6/hosts.janet @@ -0,0 +1,20 @@ +(def hosts [ + {:name "claudius" + :ip "45.63.9.183" + :online true + :services + [{:name "janet.guide"} + {:name "bauble.studio"} + {:name "ianthehenry.com"}]} + {:name "caligula" + :ip "45.63.9.184" + :online false + :services [{:name "basilica.horse"}]}]) + +(def services + (seq [host :in hosts + :when (host :online) + service :pairs (host :services)] + service)) + +(pp services) diff --git a/Janet/chapter6/loop.janet b/Janet/chapter6/loop.janet new file mode 100644 index 0000000..149819e --- /dev/null +++ b/Janet/chapter6/loop.janet @@ -0,0 +1,40 @@ +(def hosts [ + {:name "claudius" + :ip "45.63.9.183" + :online true + :services + [{:name "janet.guide"} + {:name "bauble.studio"} + {:name "ianthehenry.com"}]} + {:name "caligula" + :ip "45.63.9.184" + :online false + :services [{:name "basilica.horse"}]}]) + +(each host hosts + (if (host :online) + (each service (host :services) + (print (service :name))))) + +(loop [host :in hosts + :when (host :online) + service :in (host :services)] + (print (service :name))) + +(print) + +(each host hosts + (if (host :online) + (let [ip (host :ip)] + (eachp [service-name available] (host :services) + (if available + (for instance 0 3 + (pp [ip service-name instance]))))))) + +(loop [host :in hosts + :when (host :online) + :let [ip (host :ip)] + [service-name available] :pairs (host :services) + :when available + instance :range [0 3]] + (pp [ip service-name instance])) diff --git a/Janet/chapter7/helpers.janet b/Janet/chapter7/helpers.janet new file mode 100644 index 0000000..239754d --- /dev/null +++ b/Janet/chapter7/helpers.janet @@ -0,0 +1,5 @@ +(def- upcase string/ascii-upper) + +(defn shout [x] + (printf "%s!" + (upcase x))) diff --git a/Janet/chapter7/main.janet b/Janet/chapter7/main.janet new file mode 100644 index 0000000..bd6025d --- /dev/null +++ b/Janet/chapter7/main.janet @@ -0,0 +1,20 @@ +(defn choose [rng selections] + (def index (math/rng-int rng (length selections))) + (in selections index)) + +(defn verbosify [rng word] + (choose rng + (case word + "quick" ["alacritous" "expeditious"] + "lazy" ["indolent" "lackadaisical" "languorous"] + "jumps" ["gambols"] + [word]))) + +(defn main [&] + (def rng (math/rng (os/time))) + (as-> stdin $ + (file/read $ :all) + (string/split " " $) + (map (partial verbosify rng) $) + (string/join $ " ") + (prin $))) diff --git a/Janet/chapter7/private.janet b/Janet/chapter7/private.janet new file mode 100644 index 0000000..286aedd --- /dev/null +++ b/Janet/chapter7/private.janet @@ -0,0 +1,6 @@ +(print "this environment:") +(pp (require "./helpers")) +(print) +(print "creates these bindings:") +(use ./helpers) +(pp (curenv)) diff --git a/Janet/chapter7/project.janet b/Janet/chapter7/project.janet new file mode 100644 index 0000000..a57cf92 --- /dev/null +++ b/Janet/chapter7/project.janet @@ -0,0 +1,9 @@ +(declare-project + :name "cat-v" + :description "cat --verbose" + :dependencies []) + +(declare-executable + :name "cat-v" + :entry "main.janet" + :no-compile true) -- 2.51.2