Fork me on GitHub

Maybe a slurp spit DSL library that abstract over the use and naming of files, or allows browsing them in the repl, something along those lines


I was thinking about something like that yeah - with a config for the resource path etc.


How to sort this result from code (frequencies ["A" "A" "B" "C" "A" "C" "D"]) ?


@stardiviner What is your expected result after sorting?


@curlyfry {"A" 3, "C" 2, "B" 1, "D" 1}


@stardiviner You can't sort by a map value. But maybe you like this format too:

(sort-by (comp - second)
         (frequencies ["A" "A" "B" "C" "A" "C" "D"]))


I was try to use sort-by, but don't know how to write the compare fn. What does the (comp - second) mean?


I would do (sort-by key (frequencies ["A" "A" "B" "C" "A" "C" "D"]))


Hmm, it's still unclear if you want to sort by key or value from your example! 🙂


If it's by value you can do (sort-by val > (frequencies ["A" "A" "B" "C" "A" "C" "D"]))


@curlyfry Can your explain the sort-by's compare function? why key and val > ?


@stardiviner I think the docs + examples do a pretty good job of explaining it:


I want to sort by the values. The second code is what I want.


:thumbsup: Let us know if anything is still unclear 🙂


(sort-by count
;; => ("a" "lummox" "antidisestablishmentarianism")
Like this code, so the keyfn compare function is like map apply on all elements in collection, then sorted by sort-by. Can I understand it like this?


and > is used to decide sorting in order or in reverse order.


@stardiviner Yeah, that sound right


I found function head is unavailable, where is it? Clojure built-in or other extra libraries? How can I search a function which is in extra third libraries?


@stardiviner you probably want first


@mamapitufo No, first only get the first element, I want to get the first n elements like (head COLLECTION 5).


ah, sorry... take?


@mamapitufo Thanks. It is great. I used to use CIDER's apropos to search, but don't know hot to specify the regexp pattern. Does anybody knows this?


For example I want to apropos search string in function name and function docstring by regexp matching "first n". Then what should I type in CIDER doc apropos prompt?


@curlyfry Can I do juxtaposition sorting like this? (sort-by (juxt val > :id) (frequencies (map :cluster GO-Data))) I want to combine (sort-by val > (frequencies (map :cluster GO-Data))) and (sort-by :id (frequencies (map :cluster GO-Data))) But it does not return the result as I expected. What's wrong there?


@stardiviner Try checking what it is you're sorting by, for example try (map (juxt val > :id) (frequencies (map :cluster GO-Data))) and check if that looks like what you want or not


curlyfry: Here is my data structure:

{:description "antigen receptor-mediated signaling pathway", :bgratio "246/16992", :pvalue "6.29E-07", :cluster "M2", :number "1", :qvalue "4.89E-05", :id "GO:0050851", :count "4", :p.adjust "0.000123837", :geneid "920/952/3119/3118", :generatio "4/6"}


At first (sort-by val > (frequencies (map :cluster GO-Data))) sorted the frequency of :cluster. Then I want to do juxtaposition sorting: if :cluster is equal, then compare :id's value.


I will post the whole code include data etc info to be a better question. sorry for distribute.


I think the best way to solve problems like this is to try to split it up into smaller pieces and make sure that they work as expected. For example, try doing just (map :cluster GO-Data). After this call you don't have access to any of the other keys in the map (such as :id).


destructure this GO-Data hash-map is doing. Any update will post here. Thanks for your reply. Really thanks.


But a hint is that last time you gave sort-by two arguments, and now you're only giving it one

Lone Ranger19:05:30

alright... so experimenting with macros

(defmacro fibm [n]
  `(loop [a# 0 b# 1 i# 0]
     (case i#
       ~n b#
       (recur b# (+' a# b#) (inc i#)))))
(fibm 5)
;; 8
seems to work fine but
(defn fib [n] (fibm n))
seems to go into an infinite loop... feel like I'm missing something obvious

Lone Ranger19:05:37

this came about when I tried to do (mapv fibm (range 10)) and I got back an error CompilerException java.lang.RuntimeException: Can't take value of a macro: which also confuses me 😅

Lone Ranger19:05:07

so I tried to wrap it in a function to make it usable by mapv


@goomba the macro doesn’t see your number, it sees the symbol passed in, and thanks to how case works, case will literally check for n


case does no evaluation of args


@goomba similarly you cannot use a macro first class because a macro takes a form, and returns a new form the compiler (or another macro) to use


the only ways to do first class macros effectively make the language an interpreted one, and we just don’t do that


@goomba maybe this is simpler

=> (let [n 42] (case 42 n :this 42 :that :huh?))


or maybe

=> (let [n 42] (case n n :this 42 :that :huh?))
=> (let [n 42] (case 'n n :this 42 :that :huh?))


see, it’s comparing to the symbol n, not the value of n in that scope

Lone Ranger19:05:12

fascinating, thank you @noisesmith!

Lone Ranger19:05:53

so, is there a solution to make (defn fib [n] (fibm n)) work?

Lone Ranger19:05:08

or do I have to change fibm to make that work?

Lone Ranger19:05:56

since it sounds like you're saying in this case fibm is being passed the raw symbol 'n rather than the value of n like I was hoping

Lone Ranger20:05:15

or are we just saying it's better to go a different route?

ghadi20:05:41 clojure always evaluates symbols except in a macro argument


oh -- you got that, I didn't read all the scrollback.

Lone Ranger20:05:44

it always evaluates except in macro arguments, that's fascinating, I didn't know that.


to be a bit clearer -- when you call a macro, the raw forms are passed in, not what the forms evaluate to. Those raw forms are bound as the arguments to the macro...


... which are (usually) symbols in the macro signature


(I say usually because you can destructure the arguments)

Lone Ranger20:05:17

fascinating :thinking_face:

Lone Ranger20:05:38

this seems to work but I doubt it's considered "idiomatic"

(defn fib [x] (eval `(fibm ~x)))


it’s pathological actually


if you use cond instead of case, cond evaluates its tests

Lone Ranger20:05:40

oh I see so this issue here is more around the case and not the macro?


well, case is a macro


so it’s the same problem, just at another level :)

Lone Ranger20:05:16

macro-ception!!! 😮


and I assume doing fibonacci via macro is an exercise for learning - in real code we only make macros if we need to alter clojure’s evaluation rules

Lone Ranger20:05:44

oh yeah def just for learning

Lone Ranger20:05:31

although I'm really hoping that Clojure is going to turn out to be a great language for AI by using macros to generate code

Lone Ranger20:05:09

so need to learn the basics first!


it turns out it’s much easier to generate algorithms with data, rather than namespaces and vars - manipulating code directly is more useful for humans


macros are for the human layer, you can just manipulate data directly describing computation if computers are your target

Lone Ranger20:05:25

interesting :thinking_face:


because you can avoid a lot of ambiguity and trouble if you just have eg. hash-maps full of functions - humans hate programming that way (for good reason) but a computer can handle it just fine

Lone Ranger20:05:35

so you're saying rather than screw around with macros for code generation just leave the desired "functions" as raw data

Lone Ranger20:05:47

like, in a vector or hashmap or unevaluated list


right, and then throw them at a compiler that can put together calls from that


easier to skip the whole human readable form step


(since the forms it creates won’t make much sense to humans anyway)

Lone Ranger20:05:34

man, I love this slack team / community