Fork me on GitHub
#clojure
<
2023-12-12
>
diego.videco04:12:10

Is this a bug, or am I doing something wrong:

(def beatings-data
  (let [root 200]
    (->> (combo/combinations
          [67/64
           279/256
           9/8
           75/64
           39/32
           5/4]
          2)
         (mapcat (fn [[a b]]
                   (let [harmonics (range 1 4)]
                     (for [[h1 h1*] (map (fn [h] [h (* a h)]) harmonics)
                           [h2 h2*] (map (fn [h] [h (* b h)]) harmonics)]
                       (let [diff (* root (abs (- h1* h2*)))]
                         {:pair [a b]
                          :harmonics [h1 h2]
                          :diff [diff (float diff)]})))))
         (filter #(> 20 (first (:diff %))))
         (group-by :diff))))

(keys beatings-data) ;; [25/2 12.5] is clearly a key

(get beatings-data [25/2 12.5]) ;; => nil
Works fine when passing the a smaller vector to combo/combinations, like:
[39/32 5/4]

hiredman04:12:54

Using floating point numbers is map keys is going to be a bad time

hiredman04:12:36

In this case the issue is clojure floating point literals are doubles and you are coercing to floats

diego.videco04:12:53

But why does it work with a small map?

hiredman04:12:07

It isn't, the key you claim to be working with the small map is a pair of ratios, which could not be produced by the code you shared

hiredman04:12:25

The code you shared always produces keys that are a pair of a ratio and a float

hiredman04:12:19

Oh, you mean that pair as input not as a key, then I dunno

diego.videco04:12:12

yeah, I meant the pair as input

danielneal11:12:30

When using a library, there’s often this image link to clojars. I’ve seen them in a lot of libs readme’s. What is it that generates it? And if it’s generated automatically, would it be a bad idea to have it create something with copyable text? Ideally with the tools.deps and lein style links?

5
💯 2
p-himik11:12:47

Yeah, I find it frustrating as well. Being able to copy the text without having to go to Clojars would be nice. The image is generated with this Markdown in the README: [![Clojars Project]().

p-himik11:12:50

In the end, it's a trade-off between the convenience of end users and keeping the info in sync + the convenience of the devs. AFAIK, embedding images is the only that you can add some always up-to-date information to your README on GitHub without having to do any automation.

danielneal11:12:22

Ah yeah, you’re right, Github only allows embedding images. Just a small little frustration but sounds like it’s here to stay

danielneal11:12:03

oh it’s already an svg derp

danielneal12:12:02

Looks like a security feature on github’s side, to stop xss attacks from svg

tcrawley12:12:25

Yeah, that was the idea; to have selectable text in an SVG. But GitHub does something to prevent it from being selectable. If you load the image (via their proxy) directly, you can select the text: https://camo.githubusercontent.com/615c0a94877758b800a2a85b020a4d538edb49ca99e193c86f0ce94322c35861/687474703a2f2f636c6f6a6172732e6f72672f6d65746f73696e2f6d616c6c692f6c61746573742d76657273696f6e2e737667

tcrawley12:12:26

Not that that helps at all.

p-himik12:12:14

You gave me an idea, hang on.

cjohansen12:12:04

I honestly don't understand why people use this image. It's terrible for every user, and it saves you "the work" of replacing three characters when you make a release. How one can consider that a good trade-off is beyond me.

tcrawley12:12:39

I believe it is used because we used to advertise it on Clojars as copyable (and, when rendered there, it was selectable). We no longer advertise that version of the badge, and just have http://shields.io badges, which aren't selectable:

p-himik12:12:25

Just wrote this userscript, it might ask you to access a couple of Clojars URLs when triggered for the first time.

// ==UserScript==
// @name         Selectable Clojars coords
// @namespace    
// @version      2023-12-12
// @description  Make Clojars dep coordinates on GitHub selectable
// @author       p-himik
// @match        
// @icon         
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';
    for (let img of document.querySelectorAll('a[href*="://clojars.org/"] > img[data-canonical-src$=".svg"]')) {
        const svgPath = img.dataset.canonicalSrc;
        // Capturing `img`, needed due to lexical scoping of closures.
        const onload = ((img) => (response) => {
            const txt = response.responseText;
            if (txt) {
                if (txt.toLowerCase().startsWith('<svg')) {
                    img.parentNode.outerHTML = txt;
                } else if (txt.startsWith('<?xml')) {
                    const parser = new DOMParser();
                    const doc = parser.parseFromString(txt, 'application/xml');
                    const svg = doc.querySelector('svg');
                    if (svg) {
                        img.parentNode.outerHTML = svg.outerHTML;
                    }
                }
            }
        })(img);
        GM.xmlHttpRequest({method: 'GET',
                           url: svgPath,
                           onload: onload});
    }
})();

p-himik12:12:49

Then you'll be able to triple-click any Clojars banner and select all the text in it since it'll be a regular SVG and not an IMG.

oyakushev13:12:21

Oh my, I was the original author of this badge, you can direct your hate towards me 😅.

😅 1
oyakushev13:12:31

@U9MKYDN4Q I mean, it is a one extra click for the user vs additional action for the maintainer? And also the one that is easy to overlook? Seems like an acceptable trade-off to me.

cjohansen13:12:18

Depending on how many installs you have it's thousands of extra clicks to save the maintainer three seconds when making a release. I never use it on any of my projects.

oyakushev13:12:18

Power to you!

danielneal13:12:18

Oh cool, this user script could be a useful idea @U2FRKM4TW

danielneal13:12:30

@U06PNK4HG no hate from my end, was just curious about whether it’s possible to have something selectable, and wondering what the constraints were. Seems to be a github security issue, and I think @U2FRKM4TW’s solution is something that could be helpful,

danielneal13:12:02

I use tools.deps more these days anyway, so might need to modify it a bit, but I think the principle could still work - add some extension to the browser that changes these images

oyakushev13:12:46

No, it's fine, I also hate that it can't be selected. I spent some time back in the day to figure out if it can be made selectable, since this is indeed just a Github restriction, it works for plain SVG

oyakushev13:12:25

Sometimes, I put this badge alongside the selectable plain text like [ "x.y.z"], so that the user can copy the artifact ID but then fill the version manually taken from the badge. But I'm not sure if that's helping much.

oyakushev13:12:34

And since now there are two syntaxis for a dependency, I treat this badge just as an ad for Clojars that the user can click and copy what they actually need.

p-himik14:12:31

Yeah, I'll definitely improve upon that userscript to let a user copy the coords with the desired syntax without having to go to a separate page. :D

❤️ 1
roboli23:12:40

Hi all. Would you say there is any inconvenience naming a function starting with "def" as in "deflate" or "define"? My editor highlights it as a special form but other than that, it works fine.

seancorfield00:12:25

Sounds like a bug in your editor -- what are you using?

dpsutton00:12:43

i think emacs tooling is a bit loose/lax on things that start with def. But nothing the actual language cares about

👀 1
seancorfield00:12:57

We have lots of functions in our codebase at work that start with def. VS Code/Calva/LSP doesn't flag them. I'd have to ask my colleague whether his Emacs setup flags them... @U0NCTKEV8?

👍 1
roboli00:12:52

Also, github markdown highlights it as a special form 😕

hiredman00:12:58

my emacs setup does not

roboli00:12:54

I'm using clojure-mode

hiredman00:12:03

what do you mean by highlights as a special form?

hiredman00:12:37

you are likely not just using clojure-mode, but clojure-mode with cider and maybe some language server mode, both of which effect highlighting (I am not using cider, but I am using eglot with clojure-lsp)

roboli00:12:52

If you put the following in your md file in github:

clojure (deflate nested-hash-map :option-1-key option-1-val :option-2-key option-2-val ...)
"deflate" is highlighted with a different color than normal functions

roboli00:12:09

Ah, I'm using cider

hiredman00:12:53

you'll have to complain to github about github coloring

roboli00:12:09

Yeah, I guess so

roboli00:12:31

Well, if no issues arises when running code then I'll stick with it

roboli00:12:34

Thanks all 👍

hiredman00:12:18

(deflate) and (not-deflate) hilight the same for me

1