This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-12-06
Channels
- # adventofcode (112)
- # announcements (6)
- # beginners (197)
- # boot (3)
- # calva (52)
- # cider (25)
- # clara (14)
- # cljdoc (6)
- # clojure (147)
- # clojure-austin (6)
- # clojure-berlin (7)
- # clojure-brasil (2)
- # clojure-europe (3)
- # clojure-india (4)
- # clojure-italy (8)
- # clojure-new-zealand (2)
- # clojure-nl (7)
- # clojure-russia (7)
- # clojure-spec (29)
- # clojure-uk (63)
- # clojurescript (103)
- # core-async (5)
- # cursive (11)
- # datomic (16)
- # devcards (1)
- # emacs (28)
- # figwheel-main (3)
- # fulcro (97)
- # graphql (4)
- # hyperfiddle (1)
- # jobs (1)
- # kaocha (3)
- # lumo (9)
- # nrepl (4)
- # off-topic (29)
- # onyx (1)
- # pathom (4)
- # pedestal (8)
- # re-frame (24)
- # reagent (1)
- # reitit (13)
- # ring-swagger (7)
- # rum (11)
- # shadow-cljs (79)
- # sql (46)
- # tools-deps (67)
- # yada (8)
hey guys, can some one tell me to which function the coll
is being passed and how, please?
you are mapping the function #(%1 %2)
over two sequences, one produced by cycle and one produce by calling seq on the vector [1 2 ...]
so in the function #(%1 %2)
%1 is bound to an item from the first seq (created by cycle) and %2 is bound to an item from the second seq (created from the vector)
you might use an argument coll
to indicate a collection is passed in, but if you don't have a binding for the name coll
it doesn't mean anything
but how does it know that it should go over the next 2 element of the vector
and not the same first two element at each call?
it is written to take the first item from each input and apply the function to them, and then do the same thing again with the rest of each input
so the function is the first argument, the seq created by cycle is the second, the vector is the third, and so on as many arguments as you want (as long as the function can take that many arguments)
ah, I was going to say the ability to take any number of arguments can be kind of confusing if you are used to other functional languages where features like type systems and auto currying might make that not possible
I guess haskell has some way to do it https://wiki.haskell.org/Varargs
How can I sort desc this list ([\d 9] [\e 17])
by the integer? I get an error that the key must be an integer when I do (sort (fn [[_ int]] int) ([\d 9] [\e 17]))
(sort-by second ...)
sort
accepts a comparator, which is expected to take two arguments and return -1, 0, 1
@nikola.kasev ^ does that help?
For descending, either reverse
the sorted list or use a key-fn like (comp - second)
user=> (sort-by (comp - second) '([\d 9] [\e 17]))
([\e 17] [\d 9])
user=>
I'm wondering why transducers are implemented in core namespace. (not like the reducers case.) Could it be complecting things into a single function? It doesn't feel like an usual arity overloading. What's the idea behind this design decision?
How can I call java.nio.files.Paths.get(String ...) from clojure? https://docs.oracle.com/javase/8/docs/api/java/nio/file/Paths.html#get-java.lang.String-java.lang.String...- If I try (Paths/get "file") or (Paths/get (into-array ["file"])), I get a class-cast exception ("cannot be cast to java.net.URI"), which is a different overload of that function.
@peterhull90 can you try (Paths/get (into-array String ["file"]))
? I think it needs a hint
I still get "[Ljava.lang.String; cannot be cast to java.net.URI"
So it is the right type ([Ljava.lang.String = array of string) but selecting the wrong overload.
@peterhull90 you need (Paths/get "file" (into-array String []))
the signature is get(String first, String... more)
so the first arg needs to be a string, not a string array
@schmee Works! Thanks. I now have
(defn make-path [part & parts]
(Paths/get part (into-array String parts)))
who/what is cljsjs-robot
on clojars, and how can I encourage them to publish a later npm package?
Raise an issue here: https://github.com/cljsjs/packages
Guys I want to use a lib to hash some passwords with bcrypt, there's a recommendation?
https://github.com/weavejester/crypto-password This one is good?
@d.ian.b I know luminus uses https://github.com/funcool/buddy
hmm. is there a way to extract source position information within a macro to report error messages?
I have a DSL here that definitely could use more than 'something screwed up, here is the offending s-expr'
so I have a list of filenames and a list of data. i want to filter out the unique filenames, and at the same time filter out the corresponding data in that list. is there a nice way of doing this?
imagine something like (def filenames ["apples" "oranges" "apples" "oranges"])
with (def data [[1 2 3] [3 2 1] [1 2 3] [3 2 1]])
.
i can get the unique filenames, f.ex. (set filenames)
which gives #{"apples" "oranges"}
, but that doesn't necessarily help me filter out the duplicates from data
If you create a sequence of maps from filenames and data that looks like this: [{:filename "apples" :data [1 2 3]} {:filename "oranges" :data [3 2 1]} ... ]}
then one way to get one step closer would be (group-by :filename filenames-and-data)
, then take the first element of each value (or whichever one you prefer).
e.g. step 1: (def filenames-and-data (map (fn [fname d] {:filename fname :data d}) filenames data))
step 2: (def g (group-by :filename filenames-and-data))
step 3: (def uniqed (map (fn [[k v]] (first v)) g))
I am sure, given my semi-low-sleep state, that there are even nicer-looking or more concise versions of some of those things.
@restenb is data unique per instance of filenames, or will any repeated filename have the exact same data?
i also can't trust the order of the filenames, but i just want to keep those that are unique with their associated data
so the above solution by @andy.fingerhut seems robust
(def filenames ["apples" "oranges" "apples" "oranges"])
(def data [[1 2 3] [3 2 1] [1 2 3] [4 6 7]])
;; if each filename has a unique data set
(reduce-kv assoc
{}
(zipmap filenames data))
(def filenames ["apples" "oranges" "apples" "oranges"])
(def data [[1 2 3] [3 2 1] [1 2 3] [4 6 7]])
;; if each filename can have multiple different data sets
(reduce-kv (fn [m k v]
(update m
k
(fnil conj #{})
v))
{}
(zipmap filenames data))
maybe something like that?can just get the lists of filenames & data by map first
and map second
after, no need to involve extra keywords
or with keys
and vals
!
Just keep in mind that associating stuff in a map like this doesn't preserve order
just threw this into our production app and tested it (with much larger dataset), works like a charm! thanks
Is there a "shallow" way to turn a clojurescript seq/coll into a javascript array? I'm trying into-array
but the performance seems super bad for large collections/seqs of javascript objects...
perhaps it's not as bad as I'm thinking.. does prn
take longer to print #js...
stuff?
I'm wondering why something like this blocks when I try to add two values to init-chan
but not when I only add one. I would have thought each value would comes through the pipeline when added.
(defn make-go-loop
[in-chan]
(let [out-chan (chan 1)]
(a/go-loop []
(a/>! out-chan (a/<! in-chan)))
out-chan))
(let [init-chan (chan 1)]
(-> make-go-loop ;; a go-loop pipeline. Simply starts a go-loop which reads
make-go-loop ;; values from channels then sends them to the next one.
make-go-loop
make-go-loop
make-go-loop
make-go-loop
make-go-loop
make-go-loop
make-go-loop
make-go-loop
make-go-loop
make-go-loop
make-go-loop
make-go-loop)
(>!! init-chan true)
(>!! init-chan true))
right, I just noticed that - not looping and I need to pass init-chan
as the first argument to the threading - but it still blocks
right, but considering I have a channel per go-loop, then I would have thought I fill all of those channels before I'd hit a blocking put
I suspect (because the example you pasted is so full of issues) that you are attempting to share a simplified example of some more complicated code
(defn make-go-loop
[in-chan]
(let [out-chan (chan 1)]
(a/go-loop []
(a/>! out-chan (a/<! in-chan))
(recur))
out-chan))
;; this blocks
(let [init-chan (chan 1)
pipeline-chan (-> init-chan
make-go-loop)]
(a/>!! init-chan :value)
(prn (a/<!! pipeline-chan)))
;; this doesn't block
(let [init-chan (chan 1)
pipeline-chan (-> init-chan
#_make-go-loop)]
(a/>!! init-chan :value)
(prn (a/<!! pipeline-chan)))
so I think the issue is with the go-loop, but I'm not entirely sure why it would be blocking, since it's literally just taking from a channel, then placing on another channel.
have you actually run the example? (I ask because in some places names from core.async are prefixed with a/ and in other places they are not)
yeah, in my namespace I did a :refer :all
then :as a
, which is why they are a little all over the place.
my guess is you launched some go blocks at some point that blocked, and ended up launching enough of them that it completely stalled the executor that runs go blocks, so any go code stopped running
Advent to code in clojure by @potetm here: https://www.twitch.tv/timpote
(clojure.core/reduce + (clojure.core/map int (clojure.string/split-lines (slurp "../../resources/adv1.txt"))))
I get ClassCastException: java.lang.String cannot be cast to java.lang.Character
instead
I don’t think mapping int is what you want
that’s trying to coerce a string to an int, which is not a thing
you could simply count the output of split-lines
oh, you’re trying to parse there
use #(Integer/parseInt %)
instead of int
or #(Integer. %)
or #(Long/parseLong %)
don’t use the Integer constructor
Why not?
unnecessary boxing?
many static analyzers will warn on this
no, Integer (and Long I think) have caches around 0
and latest JVMs actually special case all this stuff for performance
you should always use the static factory methods rather than the constructors
they actually deprecated the constructors in Java 11 - https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Integer.html#%3Cinit%3E(java.lang.String)
I know it’s super low priority, but would you consider adding something parse-int etc? Would make cross platform code a little bit easier.
Would consider
It’s a nice wtf moment for a newcomer, and probably their first (forced) Java Interop. It was for me :)
^ This one got me a couple times 🙂
so we can't use functions directly from Java jdk even if they match required signature?
to be pedantically specific, methods are not functions, a function is an object you can call (it will have an invoke method), a method isn't a "thing" on it's own (though there are various mechanisms for representing one, like methodhandles etc.)
(on the JVM that is)
Hello everyone, I have a really silly question: how do I put newline characters into a string? If I write (str "string1" "\n" "string2")
, for example, it will return "string1\nstring2", which is not what I want.
I think that is what you want - it’s a string with newline characters in it
Question about order of recursive execution in Clojure. The order that the recursion happens is not clear to me. Say I have this:
(def nested '(0 1 (11 12 (121 122) 13) 2 3))
(defn trv [v] (map #(if (list? %) (trv %) (println %)) v))
(trv nested)
I get the following as output:
0
1
2
11
12
13
121
122
3
But instead of this I want it to descend into the nested elements when they are encountered vs. after doing (mostly) everything at the same level. So I want to get this output:
0
1
11
12
121
122
13
2
3
I believe that every recursive call to trv returns a lazy sequence, which isn't actually evaluated until it needs to be. Putting side effects in lazy sequences can be misleading
@joseph.hance you can try
(defn trv [v] (doseq [e v] (if (list? e) (trv e) (println e))))
if you wrap (map ...)
in (doall (map ...))
, then I'd expect it should work as you expect
just fyi in case you’re not aware of it, you’re kind of reinventing tree-seq
@alexmiller I'm not aware of it. As you know the Clojure vocabulary is fairly extensive. I'll look that up, thanks!
Hey all, so I'm trying something a little weird, and it's not working as expected. In clojure, as far as I know, functions are just values, which means you can e.g. use them as keys to maps, (defn x [] 1) ({x 1} x) ;; => 1
. However, I can't use them as case
matches, e.g. (case x x 1)
, which ought to be tautological, throws an error. Is this a bug? http://cljs.github.io/api/cljs.core/case doesn't say anything about not matching functions, but it does say it requires "constant expressions". Since functions are just bound to vars in my case, they would seem to me to qualify.
For what it's worth, this is consistent between clojure and clojurescript.
> The test-constants are not evaluated. They must be compile-time literals, and need not be quoted.
Yeah, I saw that. Maybe I'm confusing symbols with literals. So they have to be primitives, rather than just share a name?
if you provide a symbol in a case clause on the left side, it matches that symbol
not the thing the symbol resolves too
(case 'inc inc 1)
isn't matching the function inc
but just the literal symbol inc
(just to make the point clear). it is not pairing the function
this is also a rare exception to the normal rules about what parens do
user=> (case 1 (inc 1) :a :no-match)
:a
Yeah, just tried that in context and it still blew up because I had re-bound the function. So I guess in this case matching the symbol isn't helpful, because it's always tautological
yeah, I wouldn't expect case-matching a function to ever be useful, but cond can do it, it's just a little noisier code wise and slightly less fast dispatch