Fork me on GitHub
#beginners
<
2017-05-17
>
matan00:05:19

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

noisesmith00:05:48

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

matan05:05:57

@noisesmith yep!

stardiviner06:05:27

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

curlyfry07:05:59

@stardiviner What is your expected result after sorting?

stardiviner07:05:45

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

rauh07:05:33

@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"]))

stardiviner07:05:20

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

curlyfry07:05:02

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

curlyfry07:05:00

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

curlyfry07:05:45

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

curlyfry07:05:58

@stardiviner ^

stardiviner07:05:14

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

curlyfry07:05:52

@stardiviner I think the docs + examples do a pretty good job of explaining it: https://clojuredocs.org/clojure.core/sort-by

stardiviner07:05:12

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

curlyfry07:05:06

👍 Let us know if anything is still unclear 🙂

stardiviner07:05:50

(sort-by count
         ["lummox"
          "antidisestablishmentarianism"
          "a"])
;; => ("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?

stardiviner07:05:04

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

curlyfry07:05:19

@stardiviner Yeah, that sound right

stardiviner07:05:17

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?

mamapitufo08:05:10

@stardiviner you probably want first

stardiviner08:05:37

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

mamapitufo08:05:45

ah, sorry... take?

stardiviner08:05:55

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

stardiviner08:05:52

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?

stardiviner08:05:37

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

curlyfry09:05:53

@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

stardiviner10:05:42

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"}

stardiviner10:05:58

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.

stardiviner10:05:50

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

curlyfry10:05:39

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

stardiviner10:05:49

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

curlyfry09:05:04

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

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

goomba19: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 😅

goomba19:05:07

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

noisesmith19:05:18

@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

noisesmith19:05:27

case does no evaluation of args

noisesmith19:05:54

@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

noisesmith19:05:20

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

noisesmith19:05:38

@goomba maybe this is simpler

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

noisesmith19:05:14

or maybe

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

noisesmith19:05:39

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

goomba19:05:12

fascinating, thank you @noisesmith!

goomba19:05:53

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

goomba19:05:08

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

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

goomba20:05:15

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

ghadi20:05:41

https://clojurians.slack.com/archives/C053AK3F9/p1495051196896501 clojure always evaluates symbols except in a macro argument

ghadi20:05:04

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

goomba20:05:44

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

ghadi20:05:44

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

ghadi20:05:04

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

ghadi20:05:21

(I say usually because you can destructure the arguments)

goomba20:05:17

fascinating 🤔

goomba20:05:38

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

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

noisesmith20:05:16

it’s pathological actually

noisesmith20:05:36

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

goomba20:05:40

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

noisesmith20:05:48

well, case is a macro

noisesmith20:05:58

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

goomba20:05:16

macro-ception!!! 😮

noisesmith20:05:35

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

goomba20:05:44

oh yeah def just for learning

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

goomba20:05:09

so need to learn the basics first!

noisesmith20:05:35

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

noisesmith20:05:02

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

goomba20:05:25

interesting 🤔

noisesmith20:05:31

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

goomba20:05:35

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

goomba20:05:47

like, in a vector or hashmap or unevaluated list

noisesmith20:05:56

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

noisesmith20:05:11

easier to skip the whole human readable form step

noisesmith20:05:27

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

goomba20:05:34

man, I love this slack team / community