This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-08-23
Channels
- # babashka (22)
- # beginners (8)
- # calva (7)
- # clj-kondo (65)
- # cljdoc (9)
- # cljsrn (1)
- # clojure (53)
- # clojure-australia (4)
- # clojure-europe (49)
- # clojure-gamedev (2)
- # clojure-italy (13)
- # clojure-nl (1)
- # clojure-spec (19)
- # clojure-uk (4)
- # clojurescript (48)
- # clojureverse-ops (1)
- # core-async (3)
- # css (2)
- # cursive (15)
- # datomic (6)
- # degree9 (2)
- # depstar (4)
- # emacs (2)
- # find-my-lib (1)
- # fulcro (16)
- # graalvm (11)
- # gratitude (1)
- # honeysql (9)
- # introduce-yourself (2)
- # jobs (1)
- # joker (2)
- # livestream (2)
- # malli (16)
- # nbb (4)
- # news-and-articles (2)
- # off-topic (1)
- # pathom (7)
- # polylith (10)
- # practicalli (1)
- # re-frame (7)
- # reitit (1)
- # releases (3)
- # remote-jobs (1)
- # rewrite-clj (19)
- # shadow-cljs (10)
- # tools-build (1)
- # tools-deps (9)
- # uncomplicate (1)
- # vim (3)
- # xtdb (44)
Hello, Clojure beginner here. I am looking for critiques/improvements on some pretty simple code. I have a vector of maps set up like so:
(def people [
{:name "joe" :phone "+17325550101"}
{:name "jon" :phone "+17325550133"}
{:name "jimbus" :phone "+17325550122"}])
Context: this is a list of friends and phone numbers I've been having a bot send goofball messages to. The bot also has an automated message set up for when people call it. I am using clj-http to make the API requests to Twilio.
Problem: I wanted functionality that extracted phone numbers from "people", texted individuals given a name, and sent a mass message to all the people on the list.
My solution: https://gist.github.com/esciafardini/45a1d5a0d97c43ed85e137f5da39c120
(the name "test" is linked to my personal phone number)
The thing that gives me the creeps most is line 2 in the gist. Any suggestions would be great, thanks for taking the time to read if you got this far :)if you're looking things up by name, you could instead use a data structured that is a map keyed by name, which would simplify your line 2
{"joe" {:phone "...."}
"jon" {:phone "..."} ... }
as a general comment, when getting data out of your data structure seems hard, reconsider your data structure
@alexmiller Thanks. I'll try that out. I was so focused on trying to simplify the calculation, I didn't think to alter the data.
Transforming the data structure into a different shape often leads to simpler functions. There are a great many functions in clojure.core to help transform data for not a different shape
ideally, you would make a list of how you need to use your data first, then determine the data structure to simplify those reqs. in reality, if you are impatient like me, you do it wrong one or more times before you fully understand the problem :)

(defn get-number [name]
(get-in people [name :phone]))
😫👌