Fork me on GitHub
#clojure-uk
<
2020-11-13
>
dharrigan06:11:15

Good Morning!

dharrigan08:11:19

One thing that I can't figure out is why does this work (let [{funky-name :old-name} my-map] funky-name). How does the {} know to pull out the key called :old-name and give it a new name called funky-name?

Ben Hammond08:11:27

what is the value of `my-map'?

Ben Hammond08:11:51

looks like an unwholesome kind of destructuring within-a-let-statement kind of thing?

alexlynham08:11:23

not unwholesome, that's just destructuring

Ben Hammond08:11:02

people get tempted into deeply nesting them

Ben Hammond08:11:08

I'm not a big fan

dharrigan09:11:24

my-map is just {:old-name "david"}

alexlynham10:11:00

i prefer this syntax, it's declarative and i can read it at a glance years later

dharrigan09:11:27

I use {:keys [foo bar baz]} my-map a lot in my code

Ben Hammond09:11:43

yeah like that form

dharrigan09:11:45

but there a few odd occasions, for clarity, that when I pull out a key from a map, I want to rename it

Ben Hammond09:11:23

I dislike it because the syntax is subtle

Ben Hammond09:11:03

and I dont want to have to deal with nuance when reading some elses code

mccraigmccraig09:11:01

i'm the other way around - i quite often find myself destructuring two objects of the same type next to each other, and :keys is useless then, and even when it's not absolutely necessary i find a destructure like [{u-name :name u-id :id :as u} user] makes the subsequent code easier to read

mccraigmccraig09:11:50

so i actively avoid :keys

dharrigan09:11:16

🙂 It’s good to know there are a few ways of doing these things. I’ll ask in #beginners

dharrigan09:11:29

about how it works for the renaming

mccraigmccraig09:11:11

doh! dup :man-facepalming:

dharrigan09:11:10

Yes, I’ve read them

dharrigan09:11:16

but I don’t understand the mechanics

dharrigan09:11:27

how does it know to pull out the key from the map

dharrigan09:11:38

it’s abit counterintuiative

dharrigan09:11:44

I would have expected this

dharrigan09:11:57

{:old-name funky-name} so to speak

dharrigan09:11:05

since we do this

dharrigan09:11:09

(:old-name my-map)

dharrigan09:11:10

So this explains what it is doing

dharrigan09:11:12

The destructuring form is now a map rather than a vector, and instead of a symbol on the left side of the let, we have a map. The keys of the map are the symbols we want to bind in the let. The values of the destructuring map are the keys we will look up in the associative value.

dharrigan09:11:24

I just don’t understand the mechanics

rlj09:11:29

I've steered clear of the basic associative destructuring approach in the past of favour of keys because the way that works is more intuitive.

mccraigmccraig09:11:00

destructuring forms are written backwards from map-literal forms - which kind of makes sense @dharrigan - map-literals are putting something in to a map, destructuring is taking it out

mccraigmccraig09:11:27

(let [{foo :foo} {:foo 10}] foo)

dharrigan09:11:36

okay… so why not (my-map :old-name) since that’s taking old-name out of my-map

mccraigmccraig09:11:49

(let*
  [map__211906
   {:foo 10}
   map__211906
   (if (seq? map__211906)
     (clojure.lang.PersistentHashMap/create (seq map__211906))
     map__211906)
   foo
   (get map__211906 :foo)]
  foo)

mccraigmccraig09:11:10

there's an example of what happens (gotten with macroexpand-1)

mccraigmccraig09:11:58

@dharrigan (my-map :old-name) is exactly what happens after macroexpansion

dharrigan09:11:18

right, using the get variant

mccraigmccraig09:11:00

here's a keys example

mccraigmccraig09:11:23

(let [{:keys [foo]} {:foo 10}] foo)

(let*
  [map__211910
   {:foo 10}
   map__211910
   (if (seq? map__211910)
     (clojure.lang.PersistentHashMap/create (seq map__211910))
     map__211910)
   foo
   (get map__211910 :foo)]
  foo)

mccraigmccraig09:11:47

the generated code is identical!

dharrigan09:11:58

ah! right. digesting. starting to make sense

dharrigan09:11:22

what a lively on-topic discussion for a Friday morning! 🙂

dharrigan09:11:25

Thank you everyone 🙂

dharrigan09:11:29

I will go away and study.

dharrigan09:11:14

I don’t really use a mac, but were people here stung badly by the oscp outage yesterday?

dharrigan09:11:21

prevented mac apps from opening

dharrigan09:11:52

(which is diabolical anyway! - apple controls what you can open up on your computer. every bit of software calls home to apple on open)

rlj09:11:05

I can't upgrade my new work laptop to Big Sur yet (although maybe that's a good thing)

mccraigmccraig09:11:57

i never noticed - didn't try and install anything yesterday

rlj09:11:51

Oh, my OS upgrade is downloading now. Exciting!

dharrigan09:11:07

the outage wasn’t about installing anything

dharrigan09:11:43

it was the ocsp server on apple being unavailable. Everytime you open an app on a mac, it calls home

dharrigan09:11:47

every single time

dharrigan09:11:16

to check if it’s allowed to run or not (i.e., if apple kindly bestows its blessing upon you to run the apps that you may have bought)

dharrigan09:11:56

if that is offline, then apps don’t open, as rather than a soft-fail (which is what is recommended), apple did a hard-fail.

alexlynham10:11:12

@dharrigan are you sure about that? surely they must work offline

alexlynham10:11:36

also you could just block the phoning home :woman-shrugging:

alexlynham10:11:14

also final point on the destructuring - the long form with an :as entity-type is imo the most readable

dharrigan10:11:16

Alex, it was confirmed by multiple instances across t’interwebs

dharrigan10:11:33

I do block it

dharrigan10:11:57

I have 0.0.0.0 in my hosts file for my mac that I use now-and-again

pithyless10:11:34

@alex.lynham the problem was instead of hard-failing on the TCP connection (which would be fast), it was timing out after a long time (30s? 60s?); Happened to me as well and rebooting (hence opening lots of apps) exasperated the problem. I ended up booting into safe-mode to modify my /etc/hosts and restore my sanity.

pithyless10:11:31

It's a wonderful example of Leslie Lamport's: "A distributed system is one in which the failure of a computer you didn't even know existed can render your own computer unusable."

alexlynham10:11:57

so it is solvable by modifying your hosts file then? cool cool

alexlynham10:11:20

i used to do a similar thing to stop adobe creative cloud cocking up my work laptop when i switched wifi networks

dharrigan12:11:18

Be careful on upgrading to Big Sur

dharrigan12:11:37

looks like apple can bypass any vpn restrictions (or little snitch restrictions) you have in place

alexlynham12:11:13

but presumably not the hostsfile?

alexlynham12:11:20

i'm still on mojave, no plans to move atm

dominicm12:11:55

I use one of those ethernet power plug things, and I frequently lose internet but stay connected. I don't usually notice for about 30m or so. It's just me, vim and localhost. Bliss.

mccraigmccraig12:11:49

do you have flakey electrics @dominicm? i've got the ethernet power plug things and i almost never lose connection

dominicm12:11:29

@mccraigmccraig the electrics in this house haven't been checked since before I was born.

dominicm12:11:35

So, probably.

mccraigmccraig12:11:05

my old house was much different - definitely flakey electrics and some sockets didn't work very well, others were fine

dominicm12:11:23

I also have really old versions of the ethernet plugs, which don't use ground. The new ones are supposed to be way more reliable.

dharrigan13:11:52

I have a powerline too. Never experienced any droppage that I didn’t initiate myself.

dharrigan13:11:59

They are rock solid

dharrigan13:11:15

a pair of dlinks

alexlynham13:11:06

yeah i found they were solid but the speed was poor, so i switched a several relay wireless routers and the speed is way higher now

👍 3
alexlynham13:11:24

although that powerline unit was bought in errrr 2014? so possibly they're better now

dharrigan13:11:36

I have a pair of Gigabit powerlines

dominicm13:11:22

They were all us based before I believe. So the power line standard was just 2-port. I'm planning to run ethernet throughout the house for cctv and computers pis etc

Ben Hammond15:11:52

I think the powerline bandwidth is highly sensitive to the quality of the wiring in your house

dharrigan15:11:21

When I was living in a shared house, I would just drop cable everywhere

Ben Hammond15:11:43

and other electrical items perturbing the AC wave

dharrigan15:11:17

Oh, I like that word

dharrigan15:11:24

haven't heard that in a while

dharrigan15:11:36

I sense a great perturbance in the force