This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-05-17
Channels
- # aws (16)
- # beginners (82)
- # boot (29)
- # cider (43)
- # cljs-dev (90)
- # cljsrn (14)
- # clojure (79)
- # clojure-dev (12)
- # clojure-greece (4)
- # clojure-italy (12)
- # clojure-russia (81)
- # clojure-shanghai (1)
- # clojure-spec (39)
- # clojure-uk (28)
- # clojurescript (159)
- # consulting (1)
- # cursive (16)
- # data-science (6)
- # datomic (18)
- # devops (3)
- # emacs (22)
- # figwheel (1)
- # graphql (15)
- # hoplon (3)
- # jobs (1)
- # jobs-discuss (8)
- # leiningen (1)
- # luminus (6)
- # lumo (1)
- # off-topic (18)
- # om (6)
- # onyx (38)
- # pedestal (30)
- # perun (3)
- # re-frame (38)
- # reagent (8)
- # ring-swagger (2)
- # rum (2)
- # sql (2)
- # unrepl (14)
- # untangled (1)
- # vim (8)
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?
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: https://clojuredocs.org/clojure.core/sort-by
I want to sort by the values. The second code is what I want.
(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?and >
is used to decide sorting in order or in reverse order.
@stardiviner Yeah, that sound right
Thanks.
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
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 obviousthis 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 😅
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?))
:that
or maybe
=> (let [n 42] (case n n :this 42 :that :huh?))
:that
=> (let [n 42] (case 'n n :this 42 :that :huh?))
:this
see, it’s comparing to the symbol n, not the value of n in that scope
ohhhhh
fascinating, thank you @noisesmith!
so, is there a solution to make (defn fib [n] (fibm n))
work?
or do I have to change fibm
to make that work?
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
or are we just saying it's better to go a different route?
https://clojurians.slack.com/archives/C053AK3F9/p1495051196896501 clojure always evaluates symbols except in a macro argument
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...
fascinating :thinking_face:
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
or condp
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 :)
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
oh yeah def just for learning
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
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
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
so you're saying rather than screw around with macros for code generation just leave the desired "functions" as raw data
like, in a vector or hashmap or unevaluated list
right, and then throw them at a compiler that can put together calls from that
smart
easier to skip the whole human readable form step
(since the forms it creates won’t make much sense to humans anyway)
man, I love this slack team / community