Fork me on GitHub
#beginners
<
2021-08-23
>
Edward Ciafardini14:08:51

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 :)

alexmiller14:08:03

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

alexmiller14:08:05

{"joe" {:phone "...."}
 "jon" {:phone "..."} ... }

alexmiller14:08:31

as a general comment, when getting data out of your data structure seems hard, reconsider your data structure

Edward Ciafardini14:08:22

@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.

practicalli-john17:08:09

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

alexmiller14:08:31

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 :)

gotta_go_fast 13
👍 2
😂 6
Edward Ciafardini14:08:00

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