This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-01-18
Channels
- # bangalore-clj (1)
- # beginners (60)
- # boot (98)
- # cider (8)
- # clojure (158)
- # clojure-dusseldorf (16)
- # clojure-france (3)
- # clojure-hamburg (2)
- # clojure-mke (2)
- # clojure-russia (11)
- # clojure-serbia (1)
- # clojure-spec (123)
- # clojure-uk (59)
- # clojurescript (44)
- # code-reviews (16)
- # community-development (51)
- # core-async (46)
- # cryogen (1)
- # cursive (9)
- # datascript (5)
- # datomic (36)
- # emacs (3)
- # events (12)
- # hoplon (57)
- # jobs (1)
- # juxt (3)
- # klipse (55)
- # lein-figwheel (3)
- # leiningen (5)
- # luminus (3)
- # off-topic (8)
- # om (75)
- # om-next (9)
- # onyx (17)
- # pedestal (7)
- # portland-or (3)
- # proton (36)
- # protorepl (6)
- # re-frame (3)
- # reagent (33)
- # remote-jobs (1)
- # ring (23)
- # ring-swagger (2)
- # rum (1)
- # specter (1)
- # untangled (36)
- # yada (11)
So i'm trying to work out Djikstra's Algorithm in clojure... How can I "iterate over" a #{:hash-set :of :elements} ? very beginner fp question.
A set can be treated as a sequence so you can map
and reduce
over it.
boot.user=> (map inc #{3 1 2})
(2 4 3)
(there is a #beginners channel in case you feel more comfortable asking those sorts of questions there)
map
and reduce
etc call seq
on their collection argument — you can call seq
directly on a set:
boot.user=> (seq #{3 1 2})
(1 3 2)
(you always get the same order from the same set, but that order is essentially “random” across all sets)
Cool. Thanks @seancorfield ! 😃 map and reduce are powerful indeed.
in spec, how do I efine somethign that matches everything of the form [:foo any?] so I want everhthing that is a vector of length 2, where the first arg is :foo, and the second arg can be anything
@qqq https://clojure.github.io/clojure/branch-master/clojure.spec-api.html#clojure.spec/tuple (s/tuple #{:foo} any?)
I've already used spec/fdef to spec a function. How do I have spec (1) generatea . test case and (2) tell me how it worked?
is there a macro capturing an idiom like this? (let [x '()] (if (test x) x :some-default))
Something along the line of
(defmacro if-test [x testfn else]
(let [x# ~x testfn# ~testfn else# ~else] (if (testfn# x#) x# else#)))
@sveri @tcrawley good morning! So, I've fixed the problem of the main function and the schedule fires ok, but not as I spected.
Fox example, I need the schedule to fire every day at 00:30, but when I start the application, the schedule fires immediately
if I change the interval, to 1 minuto for example, the schedule waits 1 minuto and then fires, not respecting the time I need
schmee yeah I looked at a couple of these macros and some resemble the task more, some less, none perfectly. Since an empty sequence is true
in boolean context one cannot do (or x :default)
in spec, number? allows NAN; is there a somethign similar but does NOT allow NAN? NAN breaks everything as NAN != NAN
there oughta be an anything-but-NaN
too
which I guess is ambiguous though, since you might want to also exclude collections which contain a NaN
in any fashion
NaN-free?
augh it's all terrible
guess I should be greateful it's cosidering all these edge cases I'm not thinking of, lol
it makes you not want to write functions with edge cases :)
@gfredericks : how would you rewrite (fn [a b] (+ a b)) ?
it would depend on the domain; you could respec it to be only bigints, or you could use +'
instead, for example
I think PBT makes you a lot more conscious of edge cases, but clojure.core has a lot of edge cases
spec'ing +
is fine, but if you’re learning to spec functions, maybe start with something a little more specific to your domain.
I use tools like this to avoid those edge cases
@gfredericks : what's PBT ?
property-based testing; test.check etc.
Hi. Is there any way to do something like (let [(make-bindings …) {:foo :value-of-foo}] …)
where make-bindings
generates the bindings at runtime?
only at compile-time via macros
Mhm. I’m trying to make the following two scenarios work, where I generate a destructuring form from a macro input that is either already a “query” or an expression that generates a “query”. The problem is that I can’t really evaluate the second case prior to expanding the inner macro (`with-query-bindings`): https://gist.github.com/Jannis/af5c0dfed612fae19cb7b38705ad561c#file-nested-macros-clj-L19
@jannis (make-query)
is a function call, so that result isn't even available until runtime
Even if make-query
was a macro, it would only be expanded after the surrounding with-query-bindings
macro is expanded, right?
yeah that's also a problem; you might want to back up and describe your higher-level goal
I need to route based on parameter presence (GET "/transactions" {{trxid :trxid} :params}) (GET "/transactions" {{trxdate:trxdate} :params})
@h.elmougy How about you define one route and dispatch on the parameter?
Is there a clever standard function that computes ((juxt (partial filter odd?) (partial remove odd?)) (range 5))
simultaneously? Or, perhaps, a neat way to achieve this with transducers?
@sveri I already done that but i wanned to eliminate the if else with delegating the condition to destructuring the params
@h.elmougy check the coercion operators of compojure https://weavejester.github.io/compojure/compojure.coercions.html
there is int and UUID, but you should be able to write own easily?
@karol.adamiec Nice, I did not know about these 🙂
You could also use if
or when
. If a route returns nil
, it’s treated as not matching.
(GET "/basket/:uuid" [uuid :<< as-uuid ] {:body (get-basket db nil uuid)})
(GET "/basket/:username" [username] {:body (get-basket db username)})
[trxid :<< identity]
would probably work as well.
(GET “/transactions” [trxid :<< identity] …)
@weavejester this seems elegant. thanks
@nooga not that I know of, let me know if you work something out like this because it’s an intriguing concept. just did a reduce
-based solution for a single pass but it was not faster than the filter/remove version you did
is there anywhere I can look to track the Status of Clojure 1.9? or does anyone know approximately when it will be released? I’m trying to do a bit of planning for what we want to do this year on my team and just trying to figure out if/when 1.9 and spec could become a possibility. I can branch with the 1.9 alpha and get things rolling on our various products, but I’d like to limit the time I have to maintain a parallel fork
@joshjones I’ll keep you posted 🙂
@djjolicoeur Rich Hickey et al have generally avoided roadmaps with specific features or dates. "It's done when it's done." That said, the Clojure 1.9 alpha releases are only really "alpha" with respect to new features such as spec. Generally, I would regard it as safe to use Clojure 1.9 alphas, with the understanding that new features, such as spec, may still be subject to breaking API changes. Of course, you should still run tests on your application and watch the release announcements for potential issues.
thanks @stuartsierra !
hello, I am looking at source-fn
source code; I can’t understand how the reader knows the end of the line to read (which is not specify in (meta ‘sym)
(defn source-fn [x]
(when-let [v (resolve x)]
(when-let [filepath (:file (meta v))]
(when-let [strm (.getResourceAsStream (RT/baseLoader) filepath)]
(with-open [rdr (LineNumberReader. (InputStreamReader. strm))]
(dotimes [_ (dec (:line (meta v)))] (.readLine rdr))
(let [text (StringBuilder.)
pbr (proxy [PushbackReader] [rdr]
(read [] (let [i (proxy-super read)]
(.append text (char i))
i)))
read-opts (if (.endsWith ^String filepath "cljc") {:read-cond :allow} {})]
(if (= :unknown *read-eval*)
(throw (IllegalStateException. "Unable to read source while *read-eval* is :unknown."))
(read read-opts (PushbackReader. pbr)))
(str text)))))))
does someone can help me ?
so it looks like if you have two defn
s on a line it may not work correctly with source
ok, I should try then
@dpsutton you are right, if you define 2 defn
on the same line and try to`source` the 2nd one, it wont work
hi guys
first one
(/ 22 7.0) 3.142857142857143
how can i take only two or 3 number after dot ?
3.142
@dpsutton meta
give you the column
information, so it should be possible to make it work but not very usefull. I never saw 2 inline defn
lol, offcourse, I was just playing with source
@abdullahibra this may be too naive but i just did it so it gives you an idea at least. might be rounding issues, just test thoroughly:
(defn precision
[n x]
(let [p (Math/pow 10 x)]
(/ (Math/floor (* p n)) p)))
@djjolicoeur As a testament to Clojure’s stability in alphas, at World Singles, we’ve taken alpha and beta builds to production with every release dating back to 1.3 alpha 7 or 8 (back in 2011). We’re on 1.9 alpha 14 in production right now.
We also always test our code against whatever “release” we plan to use in production and the current master snapshot — so we don’t get surprised by changes coming down the pipe.
thanks @seancorfield, my only concern here is leaning too hard on the alpha spec API, as we are thinking of replacing schema with spec. I suppose if breaking changes are really only scoped to that narrow domain we might be ok.
Like I say, set up your tests to run against the released version and master so you get advanced notice of any breaking changes and you can prepare for them.
We’re using spec fairly heavily already and so far, across 14 alphas, we’ve only had to make a few changes.
excellent, that sounds like a viable option. Thank you, that is very helpful information
@djjolicoeur I don’t have anything official to add at this time re Clojure 1.9/spec. I do hope that we can say more in the near future about plans, which we have been discussing. There is a lot of pending work in the various jira tracking reports and those are the most likely bug/enhancement kinds of things to get in there as that’s what has been worked on. I do not expect there to be major changes in the current API or the spec forms - most changes at this point are likely to be either in implementation or additive.
one area of the api that we are unhappy with is the consistency with which things like gen overrides, stub/spec/replace options, etc are specified across various parts of the api (exercise, instrument, check, etc) and that’s something that may require a bit of tweaking.
thanks @alexmiller, really appreciate the info
the jira reports I’m talking about are these: - screened (waiting for Rich): http://dev.clojure.org/jira/secure/IssueNavigator.jspa?mode=hide&requestId=10383 - screenable (vetted, need screening): http://dev.clojure.org/jira/secure/IssueNavigator.jspa?mode=hide&requestId=10374 - needs patch (vetted, needs patch): http://dev.clojure.org/jira/secure/IssueNavigator.jspa?mode=hide&requestId=10373
(see http://dev.clojure.org/display/community/Contributing for more report links)
@tvalerio: I'm not sure why the schedule-seq
would fire immediately, but for your needs, you don't need it. You can just do (schedule my-job (-> (at "00:30") (every :day)))
Dear #clojure, I love you guys. Awful hacker news comments makes me love you guys more.
all the Swift and Rust programmers come out of the woodwork to complain about Go, but they just don’t understand why Go is the way it is....
I so dearly wanted to like Go. It has some very cool stuff in it. But it has some very weird stuff too (and, last I looked, its ecosystem was still pretty fragile?). I think, deep down, I wanted it to be Rust tho’… and then when Rust came along, I loved it… but found myself in the same love/hate position as I do with Haskell 🙂 So Clojure remains the sweet spot for me.
Clojure just makes me happy.
me as well - but i’ve written a fair amount of very useful software in Go for situtations where clojure just wouldn’t have been appropriate
and the code i wrote was very similar to the code i’d have written in Clojure, although a bit more verbose
type switch and interfaces are quite similar to multi-methods switched on a single keyword or protocols
but structs are implicitly copied in most situtations, so it’s quite easy to write “functional” code in that you never mutate your arguments - although in Go there’s some annoying situations where you need to manually clone maps or arrays/slices
if you have heterogenous maps encoded as structs with “struct embedding”, then argument passing is effectively functional with values
top tip, never depend on the behavior of str on a map if you are evaluating a lazy sequence inside of println which binds print-readable to nil.
the immutable structs sound cool, but I don’t think I can ever go back to loops after doing so much Ruby and Clojure 😛
¯\(ツ)/¯ it’s pretty easy to squint and view for/range + if/continue + append
as a direct analog to map, filter, and mapcat
but in the absence of a compiler that does loop fusion, you’re going to get better perf with the imperative stuff
it’s the same tradeoff as static vs dynamic: you commit to some strategy earlier with loops etc than you do if you use functional operations
I also have a love-hate thing with Haskell, as in I love to hate it
If Go's interfaces were namespaced, it wouldn't be half as bad
@bbloom let's try to keep current US politics out of this, okay?
Has anyone run into core.logic as a performance bottleneck in any of their applications?
I guess the questions are: - can my usage of core.logic be optimized somehow? (cpu snapshot filled with core/logic/unify, clojure.core/count, hash - if not, are there much faster ways to do a similar type of scenario where you load tuples into memory and then query them?
https://github.com/halgari/odin might be of interest