workspace
stringclasses
4 values
channel
stringclasses
4 values
text
stringlengths
1
3.93k
ts
stringlengths
26
26
user
stringlengths
2
11
clojurians
clojure
Hi, If I want to find the index of the biggest element that is <= 5 , and the index of the biggest element that is <=8, in a sorted array. Can I get the two indices in one search in Clojure?
2017-11-25T10:11:39.000094
Tari
clojurians
clojure
``` (defn tmp-search [] (let [v [1 2 3 4 5 6 7 8 9 10]] (loop [index1 9 index2 9] (let [condition1 (<= (get v index1) 5) condition2 (<= (get v index1) 8)] (if (and condition1 condition2) [index1 index2] (recur (if condition1 index1 (dec index1)) (if condition2 index2 (dec index2)))))))) ```
2017-11-25T10:19:27.000033
Tari
clojurians
clojure
<@Tari> You could consider a one pass using `reduce-kv` ``` (reduce-kv (fn [[ndx1 ndx2] k v] (cond-&gt; [ndx1 ndx2] (&lt;= v 5) (assoc 0 k) (&lt;= v 8) (assoc 1 k))) [nil nil] [1 2 3 4 5 6 7 8 9 10]) ```
2017-11-25T11:03:48.000030
Dirk
clojurians
clojure
I suppose your approach is more efficient <@Tari> in that it traverses a part of the sequence from the end. You could force that into a single-pass `reduce` by using `reduced` to stop things. Thinking something like: ``` (take 2 (reduce (fn [[ndx1 ndx2 ndx :as acc] v] (if (and ndx1 ndx2) (reduced acc) (cond-&gt; [ndx1 ndx2 (dec ndx)] (and (nil? ndx1) (&lt;= v 5)) (assoc 0 ndx) (and (nil? ndx2) (&lt;= v 8)) (assoc 1 ndx)))) [nil nil 9] (rseq [1 2 3 4 5 6 7 8 9 10]))) ```
2017-11-25T12:15:29.000011
Dirk
clojurians
clojure
Would you recommend using <https://github.com/funcool/cats> on a project, or are there better FP librairies out there? (for simple functor/monad stuff)
2017-11-25T13:09:05.000003
Lucio
clojurians
clojure
Actually, my only real use-case right now is using Either to model failure instead of throwing exceptions
2017-11-25T13:10:04.000123
Lucio
clojurians
clojure
Have you also seen <https://clojure.github.io/algo.monads/> <@Lucio>?
2017-11-25T13:33:09.000080
Cherelle
clojurians
clojure
I've only heard good things about it. I know that <@Shameka> is a fan
2017-11-25T13:45:28.000123
Evan
clojurians
clojure
cats is ok <@Lucio> - it was the best of the clojure fp libs i tried. the context stuff is a bit awkward, and i end up writing sugar macros for `(with context ... (mlet [...`
2017-11-25T14:38:38.000104
Shameka
clojurians
clojure
they took out the monad transformer stuff - i had to resurrect it for one of my libs (uses a promise-state transformer)
2017-11-25T14:40:00.000106
Shameka
clojurians
clojure
and there is no reader or continuation or comonad stuff
2017-11-25T14:42:57.000028
Shameka
clojurians
clojure
but what is there is solid and straightforward - i’ve come across only a couple of bugs in 2 years of heavy use in both clj and cljs
2017-11-25T14:44:49.000019
Shameka
clojurians
clojure
and if you use `either` to model failure you might also want to take a look at <https://github.com/zalando-stups/cats.match>
2017-11-25T14:51:58.000074
Shameka
clojurians
clojure
Since these sorts of solutions slow the code down, I’ve never used the either in production. But what’s the driver to avoid the exceptions?
2017-11-25T14:52:33.000045
Sandy
clojurians
clojure
<@Sandy> I just thought it would be a neater way to differentiate between "expected" failure-like return values and actual exceptions
2017-11-25T15:42:14.000123
Lucio
clojurians
clojure
for example, I am implementing a unification algorithm. When unification fails it's not really an error, just a failure scenario that the caller will handle
2017-11-25T15:42:45.000065
Lucio
clojurians
clojure
a try/catch seemed a bit odd in that case
2017-11-25T15:42:52.000014
Lucio
clojurians
clojure
I could return a custom map with a success/failure tag, but in that case why not use functors and get chaining etc for free...
2017-11-25T15:43:12.000066
Lucio
clojurians
clojure
Well one reason is that they are opaque. That’s my biggest criticism against using functions instead of data, there are few ways to introspect them.
2017-11-25T15:48:01.000029
Sandy
clojurians
clojure
But either way the chaining isn’t free, since either method requires using non standard Clojure forms instead of let, do, etc
2017-11-25T15:48:47.000027
Sandy
clojurians
clojure
But I agree exceptions are not the answer for this.
2017-11-25T15:49:07.000004
Sandy
clojurians
clojure
<@Sandy> ah yes you are right, I was using the term "free" a bit too freely. I am open to better approaches!
2017-11-25T16:00:18.000089
Lucio
clojurians
clojure
is there a way to tell `(for [[k v] someMap] ...)` to process the elements in order sorted by 'k' ?
2017-11-25T16:37:24.000057
Berry
clojurians
clojure
<@Berry> `(for [[k v] (sort-by key someMap)] ...)`
2017-11-25T16:38:15.000054
Margaret
clojurians
clojure
and, incidentally `(sort someMap)` will return key/value pairs in the same order as `(sort-by key someMap)` - when sorting collections it sorts by the size, then first item, then second item if first items sort equal
2017-11-25T16:40:29.000105
Margaret
clojurians
clojure
I guess technically sort-by key might be different if sort thought two of your keys were equal even though hash does not
2017-11-25T16:41:13.000048
Margaret
clojurians
clojure
fun ```+user=&gt; (assoc {} [:a] 0 '(:a) 1) {[:a] 1} ```
2017-11-25T16:42:00.000068
Margaret
clojurians
clojure
Is there a way to filter a collection with various rules? Fox example: ```(def jobs [{:id "f26e890b-df8e-422e-a39c-7762aa0bac36", :type "type1", :urgent false} {:id "ed0e23ef-6c2b-430c-9b90-cd4f1ff74c88", :type "type2", :urgent true} {:id "690de6bc-163c-4345-bf6f-25dd0c58e864", :type "type3", :urgent false} {:id "c0033410-981c-428a-954a-35dec05ef1d2", :type "type2", :urgent false}])``` I want to get the jobs with type 2, but considering this: return only urgent jobs if they exists. If not, return jobs with the same type that are not urgent. Just to avoid using lots of ifs… is there a way to do this with `filter`?
2017-11-25T16:45:40.000030
Audie
clojurians
clojure
<@Audie> `(or (seq (filter :urgent jobs)) ...)`
2017-11-25T16:47:37.000100
Margaret
clojurians
clojure
oh, the type 2 part
2017-11-25T16:47:58.000018
Margaret
clojurians
clojure
`(let [type2 (filter (comp #{"type2"} :type) jobs)] (or (seq (filter :urgent type2)) type2))`
2017-11-25T16:49:21.000093
Margaret
clojurians
clojure
<@Margaret> in this case `type2` will have all the jobs right? Do I have to put the `urgent` part inside the `let` somehow?
2017-11-25T17:01:19.000054
Audie
clojurians
clojure
type2 will have all the type2 jobs, `(or (seq (filter ....)) ...)` ensures that you look at the regular jobs if none of them are urgent
2017-11-25T17:02:10.000114
Margaret
clojurians
clojure
but otherwise you get the urgent ones only
2017-11-25T17:02:18.000068
Margaret
clojurians
clojure
I tried in my application and it works perfectly! thanks
2017-11-25T17:09:31.000032
Audie
clojurians
clojure
What is wrong with this macro? ``` ;; (comment [1;31mjava.lang.ClassCastException[m: [3mclojure.lang.Symbol cannot be cast to clojure.lang.Keyword[m) ``` so the two 'small' examples work fine, but when I merge the maps, I get an error
2017-11-25T17:19:51.000004
Berry
clojurians
clojure
``` (defmacro def-fsi [args] `(do ~@(for [[v ks] (sort-by key args) k ks] `(clojure.spec.alpha/def ~k ~v)))) (macroexpand-1 '(def-fsi {number? [::a]})) #_ (comment (do (clojure.spec.alpha/def :a.aa-util/a number?))) (macroexpand-1 `(def-fsi {::foo [::cat]})) #_ (comment (do (clojure.spec.alpha/def :a.aa-util/cat :a.aa-util/foo))) (macroexpand-1 '(def-fsi {number [::a] ::foo [::cat]})) Unandled java.lang.ClassCastException clojure.lang.Symbol cannot be cast to clojure.lang.Keyword ```
2017-11-25T17:20:08.000064
Berry
clojurians
clojure
<@Berry> is it `number` that it doesn't like?
2017-11-25T17:21:28.000063
Margaret
clojurians
clojure
good cll; unfortunately ``` (macroexpand-1 '(def-fsi {number? [::a] ::foo [::cat]})) ``` also give same error
2017-11-25T17:22:04.000004
Berry
clojurians
clojure
number? is also a symbol rather than a keyword
2017-11-25T17:22:26.000056
Margaret
clojurians
clojure
what I don't get is why does ``` ;; works (macroexpand-1 '(def-fsi {number? [::a]})) ```
2017-11-25T17:22:31.000023
Berry
clojurians
clojure
oh
2017-11-25T17:22:37.000060
Margaret
clojurians
clojure
`{number? [::a]}` and `{::foo [::cat]}` works, but when I merge the two maps -- BAM, error -- can't even macro expand
2017-11-25T17:23:16.000012
Berry
clojurians
clojure
`(sort-by key {'a 20 :fo 30})` -- totally your fault for suggesting (sort-by key ...) :slightly_smiling_face:
2017-11-25T17:25:38.000012
Berry
clojurians
clojure
so clojure's sort can't compare keyword vs symbol
2017-11-25T17:25:50.000068
Berry
clojurians
clojure
you could provide a compare that knows how to
2017-11-25T17:26:12.000109
Margaret
clojurians
clojure
I actualy doh't care about the order so long as it's deterministic
2017-11-25T17:26:38.000068
Berry
clojurians
clojure
maybe I'll just cast everything t strings
2017-11-25T17:26:43.000035
Berry
clojurians
clojure
<@Berry> `(fn [coll] (into [(type (first coll))] coll))` - that ensures that every item in the collection has an item of type Class as its first element
2017-11-25T17:27:40.000064
Margaret
clojurians
clojure
and the second elements would only be compared if the types of their first elements were the same, etc.
2017-11-25T17:27:57.000009
Margaret
clojurians
clojure
does that get around this issue here: (sort [[:foo] ["hello"]]) ?
2017-11-25T17:30:27.000005
Berry
clojurians
clojure
hmm... it's not working as I expected...
2017-11-25T17:40:14.000006
Margaret
clojurians
clojure
<@Berry> ```+user=&gt; (defn weird-compare [x y] (if (= (type x) (type y)) (compare x y) (compare (str (class x)) (str (class y))))) #'user/weird-compare +user=&gt; (weird-compare :a :b) -1 +user=&gt; (weird-compare :a "b") -7 +user=&gt; (sort weird-compare [:a "b" 'c 3]) (:a c 3 "b") +user=&gt; (sort weird-compare [:a "b" 'c 3 66 2 50]) (:a c 2 3 50 66 "b") ```
2017-11-25T17:48:55.000023
Margaret
clojurians
clojure
its order is stable, and you get arbitrary order if they are not equal
2017-11-25T17:49:13.000095
Margaret
clojurians
clojure
it's not quite right yet for k/v or collections yet
2017-11-25T17:49:26.000043
Margaret
clojurians
clojure
If you search for 'cc-cmp' on this page, you can find a function I wrote a couple years back that tries to be a 'universal comparator' between objects of different classes, sorting first by class names alphabetically, then by class-specific means between objects of the same class: <https://github.com/jafingerhut/thalia/blob/master/doc/other-topics/comparators.md>
2017-11-25T18:31:28.000060
Micha
clojurians
clojure
It doesn't succeed at being universal, quite, but it goes a lot farther than clojure.core/compare does.
2017-11-25T18:31:50.000009
Micha
clojurians
clojure
Isn’t that just map, or perhaps the result of (map f)?
2017-11-25T19:49:24.000026
Sandy
clojurians
clojure
I'm using timbre for logging and wondering if there's any way how to change the log level of running application from outside. Sometimes, I want to raise the logging level temporarily - mostly for staging/production environment. I was thinking about something like jmx...
2017-11-25T19:52:29.000028
Terra
clojurians
clojure
set-level! just changes a key in an atom, but you could easily hook up an api to alter it. My app starts a localhost only socket server that we sometimes connect to in order to change the log level, or the logged namespaces
2017-11-25T19:54:12.000048
Margaret
clojurians
clojure
I was thinking about socket repl but not sure if that's feasible in terms of security compliance on production :slightly_smiling_face:. Nevertheless, thanks for advice. Maybe I'll try to add simple jmx api
2017-11-25T20:02:59.000023
Terra
clojurians
clojure
it's localhost only by default
2017-11-25T20:03:31.000041
Margaret
clojurians
clojure
if they are on localhost, there's nothing they could do via the socket that they couldn't do easier with a shell
2017-11-25T20:03:49.000007
Margaret
clojurians
clojure
That sounds reasonable. Will speak to ops guy and see if that's feasible. Thanks!
2017-11-25T20:04:27.000034
Terra
clojurians
clojure
@jumar you don’t actually need the full repl - the socket server can be used to trigger the invocation of an arbitrary function with args
2017-11-25T20:47:32.000004
Sonny
clojurians
clojure
<https://clojure.org/reference/repl_and_main>
2017-11-25T20:49:10.000033
Sonny
clojurians
clojure
I keep forgetting it can do that, that's smart
2017-11-25T20:58:22.000002
Margaret
clojurians
clojure
spec is not behaving the way I expec it to is there a way to query the "spec database" to see what is "registered" corresponding to a given keyword/spec ?
2017-11-26T05:11:11.000071
Berry
clojurians
clojure
Yep, there’s a private atom &amp; a helper which derefs and returns the current value.
2017-11-26T05:54:17.000006
Wendi
clojurians
clojure
Can you point me at the documentation of the helper function? don'ts see it in the spec guide.
2017-11-26T06:01:23.000059
Berry
clojurians
clojure
there is (defn get-spec “Returns spec registered for keyword/symbol/var k, or nil.” [k] (get (registry) (if (keyword? k) k (-&gt;sym k)))) from the source
2017-11-26T06:34:08.000003
Daine
clojurians
clojure
What are the benefits of using `keyword` over `string`? In particular, in a map?
2017-11-26T07:31:15.000014
Mina
clojurians
clojure
``` (:foo {:foo 20}) ("hello" {"hello" 20}) ``` kw works, string gives error
2017-11-26T07:55:21.000040
Berry
clojurians
clojure
<@Mina> there's a performance benefit - because of interning, Clojure Keywords are very fast to check for equality (essentially a pointer comparison), making for fast lookups in maps; they also then to consume less memory. There's also a practical benefit that keywords are functions of the maps that (potentially) contain them as keys. Finally, IMO, there's a clarity benefit: when you see a piece of data as a keywork, you know that it's a programmatic name, not content meant to be shown to a user; Strings have that ambiguity.
2017-11-26T07:56:00.000013
Danyel
clojurians
clojure
<@Berry>: keyword implement IFn and act as functions, strings don't. Similarly, you can't use a number as a function like `(1 ["foo" "bar" "baz"])`. However, maps implement IFn, so you can do `({"hello" 20} "hello")`.
2017-11-26T08:50:09.000070
Basil
clojurians
clojure
ok thanks. One more question, do you use keywords scattered in your codebase or abstract them using a `def`? What I mean: ``` (def name :name) (name {:name "itaied"}) ``` Rather than `(:name {:name "itaied"})`
2017-11-26T08:55:50.000047
Mina
clojurians
clojure
so that if the model name changes (lets say that the map is a dynamic variable), you have to change it in only one place
2017-11-26T08:56:48.000058
Mina
clojurians
clojure
like `:name` change to `:fullname`
2017-11-26T08:57:02.000057
Mina
clojurians
clojure
This is something I've also been interested in, and has been one of the things I've felt myself wanting monads for.
2017-11-26T09:06:53.000001
Jodie
clojurians
clojure
<@Jodie> have you ended up using this approach on any project?
2017-11-26T09:46:15.000085
Lucio
clojurians
clojure
<@Mina> I know why you may feel this is a good idea, but from experience, I recommend using keywords in their literal form, not abstracted behind a name.
2017-11-26T09:49:32.000089
Danyel
clojurians
clojure
This level of indirection is unlikely to make your change non-breaking, because the keyword will probably end up being exposed raw somewhere, e.g serialized over the network
2017-11-26T09:51:09.000064
Danyel
clojurians
clojure
but then the change would be in one place, doesn't it?
2017-11-26T10:16:31.000008
Mina
clojurians
clojure
Hey folks. I’m trying to parse a string of the format `aaa bbb "ccc" {:a 1} foo bar...`, where non-bracketed whitespace-separated parts are to be read verbatim, and `{..}`, `#{..}` and `[..]`-parts are to be read as ClojureScript expressions. What’s the easiest way to do the expression read? I had a look at `clojure.edn/read-string`, but it does not return the unread tail, and I wouldn’t want to count brackets manually.
2017-11-26T11:27:31.000066
Maryann
clojurians
clojure
<@Mina> No, it will be everywhere: instead of changing a keyword throughout the code you’ll need to change a variable throughout the code. If you don’t, you’ll end up with moral equivalent of `(def fourteen 17)`.
2017-11-26T11:40:55.000024
Maryann
clojurians
clojure
that reminds me of this lib <https://github.com/gfredericks/seventy-one>
2017-11-26T11:42:22.000059
Margaret
clojurians
clojure
Not yet, no.
2017-11-26T11:57:57.000091
Jodie
clojurians
clojure
<@Mina> it's not considered idiomatic clojure, people simply use keywords and repeat them whenever they need to, you should also know that there's another feature regarding Clojure keywords that is useful to provide this isolation you're looking for, it's called namespaced keywords. Check this article for more info on that: <https://kotka.de/blog/2010/05/Did_you_know_III.html>
2017-11-26T12:18:31.000081
Ahmad
clojurians
clojure
also, if you define a name for your keywords you would have to require them in the other namespaces to use them, therefore, keywords and namespaced keywords are easier to use.
2017-11-26T12:21:51.000060
Ahmad
clojurians
clojure
( for extremely large values of fourteen )
2017-11-26T12:29:29.000084
Magda
clojurians
clojure
is there a nice way to interface Clojure with either Rust, C, or C++ ? my ideal setup would be: 1. Clojure has a DSL for Rust/C/C++ 2. I write some high performance code in this DSL 3. at runtime, this DSL -&gt; Rust/C/C++ -&gt; *.so -&gt; dynamic loads as JNI -&gt; I can call it from Clojure The only structured I need to pass back &amp; forth is float-array, int-array, and double-array
2017-11-26T15:41:49.000021
Berry
clojurians
clojure
i’ve used jna for integration with `C`
2017-11-26T15:48:44.000019
Jonas
clojurians
clojure
and it was pretty enjoyable
2017-11-26T15:48:48.000090
Jonas
clojurians
clojure
<https://github.com/Chouser/clojure-jna/>
2017-11-26T15:49:10.000035
Jonas
clojurians
clojure
it seems like rust supports being called like a c function, <https://doc.rust-lang.org/book/first-edition/ffi.html#calling-rust-code-from-c>
2017-11-26T15:56:38.000009
Jonas
clojurians
clojure
so it seems like you could setup rust code to be called from clojure-jna, although it might involve some boilerplate
2017-11-26T15:57:07.000023
Jonas
clojurians
clojure
the same will apply for c++, where you would create c wrapper functions to your c++ code
2017-11-26T15:57:25.000037
Jonas
clojurians
clojure
I've used Clojure with JNI directly, it was straightforward to follow the JNI docs. You have to be a little careful to make sure pointers passed to native code don't get garbage-collected. I think there is some overhead involved in the interaction between native code and JVM, which might matter if there is a lot of calling back-and-forth between the native code and JVM code.
2017-11-26T17:06:24.000102
Edwin
clojurians
clojure
I currently have a boot process that looks like: ``` (deftask learning-dev [] (comp (repl :port 4001) (watch) (fn [next] (fn [fileset] (require 'server.ss-web :reload))))) ``` the goal is to auto reload all *.clj *.cljc files unfortuantely, it's not doing that; how can I fix this?
2017-11-26T17:13:00.000052
Berry
clojurians
clojure
(server.ss-web) is a ns that happens to require all the namespaces I care about
2017-11-26T17:13:12.000036
Berry