This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-06-22
Channels
- # bangalore-clj (6)
- # beginners (110)
- # boot (49)
- # cider (13)
- # cljs-dev (35)
- # cljsrn (5)
- # clojure (145)
- # clojure-conj (3)
- # clojure-dev (60)
- # clojure-italy (2)
- # clojure-nl (3)
- # clojure-russia (3)
- # clojure-serbia (1)
- # clojure-spec (116)
- # clojure-uk (58)
- # clojurescript (235)
- # cursive (14)
- # datascript (7)
- # datomic (31)
- # dirac (144)
- # emacs (1)
- # events (1)
- # hoplon (12)
- # leiningen (11)
- # luminus (60)
- # lumo (19)
- # off-topic (18)
- # om (74)
- # onyx (5)
- # pedestal (13)
- # precept (3)
- # re-frame (3)
- # reagent (15)
- # remote-jobs (7)
- # ring-swagger (25)
- # rum (1)
- # untangled (53)
- # vim (3)
Also, is there someone that can help me bringing a method from python to clojure? I can't seem to do it
you could make the collection part of the accumulator, but I think it would be easier to use loop instead if you need to access the full collection on each iteration
if you only need a specific part - eg. the very next item, there’s other simpler things
(reduce (fn [acc [x y]] ...) {} (partition 2 1 coll))
- each step sees the next value in line
only problem with that is the sequence is one short
(defn each-pair [coll] (partition 2 1 (concat coll [nil]))) is one approach
in 1.8 forward (prn e) on an exception shows the stack trace
there’s also the .printStackTrace method, that can be called on exceptions, and .getStackTrace which can be called on threads
Ok, first things first: Can I reduce over a map? If so, the element given is the pair (key, value)?
there’s even a dedicated reduce-kv
for this which gives the key and value as separate args
The thing about the code I'm trying to do is, it has many if/elses. With the reduce passing many arguments, the thing is horrible to read
Also (transduce (map :attribute) + attr-map)
Or (reduce + (map :attribute attr-map))
@madstap i believe that it does. underneath it calls seq on it which should obey the sorted map
@madstap vals
returns elements in the same order as seq
on the map so, yes.
Jinx 🙂
How can I make this less ugly?
(defn get-periods-of-debt [account]
(create-entry-if-necessary account)
(json/write-str ((reduce
(fn [{:keys [report old-balance]} k v]
(let [current-balance (apply + (map :amount v))
current-date k]
(if (< old-balance (bigdec 0.0))
(if (>= current-balance (bigdec 0.0))
{:report (assoc report (- (count report) 1) :end current-date)
:old-balance current-date}
{:report (conj (assoc report (- (count report) 1) :end current-date)
{:principal current-balance
:start current-date})
:old-balance current-date})
(if (>= current-balance (bigdec 0.0))
{:report report
:old-balance current-balance}
{:report (conj report
{:principal current-balance
:start current-date})
:old-balance current-date}))))
{:report []
:old-balance (bigdec 0.0)}
(group-by-date (all-operations account))) :report)))
@donyormCould you give me an expected parameter (some dummy value) @victora so I can test the output
As I said, I'm not the best at reducing things to the minimum code, but this about what I would do. And my computer just died. Maybe someone else can take a look too. Great job!
this code probably doesn't do what you intended, there's an extra pair of paren around reduce & the reducing fn signature should look something like this: (fn [{:keys [report old-balance]} [date records]])
group-by-date
is a map where the keys are dates and the values are lists of operations. An operation is a map {:amount, :date, :description, :id}
I changed things a bit and it looks a bit better, but It may still be bugged:
(defn get-periods-of-debt [account]
(create-entry-if-necessary account)
(json/write-str ((reduce-kv
(fn [{:keys [report old-balance]} k v]
(let [current-balance (+ old-balance (apply + (map :amount v)))
current-date k
last-i (- (count report) 1)]
{:old-balance current-balance
:report (if (< old-balance (bigdec 0.0))
(if (>= current-balance (bigdec 0.0))
(assoc report last-i (assoc (report last-i) :end current-date))
(conj (assoc report last-i (assoc (report last-i) :end current-date))
{:principal current-balance
:start current-date}))
(if (>= current-balance (bigdec 0.0))
report
(conj report
{:principal current-balance
:start current-date})))}))
{:report []
:old-balance (bigdec 0.0)}
(group-by-date (all-operations account))) :report)))
Is it possible to dispatch a multimethod on the first argument? Something like:
(defmulti cmd (fn [k & _] k))
(defmethod cmd :a [x y] (+ x y))
(defmethod cmd :b [x y] (* x y))
(cmd :a 1 2)
The problem I'm having, is that dispatch function is complaining about being passed too many args. But it should be able to handle any number of args, and return only the first, no?
((fn [k & _] k) 1 2 3) ;; 1
@victora this is as far as I got, i'm getting off now:
(defn get-periods-of-debt [account]
(create-entry-if-necessary account)
(->> (group-by-date (all-operations account))
(reduce-kv
(fn [acc date operations]
(let [current-balance (apply + (map :amount operations))]
(cond [(< old-balance 0)
(>= current-balance 0)]
[true true] (update acc :reports #(conj ...))
[true false]
[false false]
acc)))
{:reports []
:old-balance 0})
:reports
json/write-str))
Hrm, following this tutorial: https://github.com/Day8/re-frame/blob/master/docs/CodeWalkthrough.md but when I go to http://localhost:3449/example.html the app doesn't load, it just says "Reagent example app – see README.md"
What's the elegant way to code such operation?
user=> (let [s {:a {:b {:c 1}}}]
#_=> (assoc s :a (assoc (s :a) :b (assoc ((s :a) :b) :c 3))))
{:a {:b {:c 3}}}
Thanks!(assoc-in s [:a :b :c] 3)
(let [code (+ (if cond1 1 0) (if cond2 2 0))]
(cond (= code 0) (println "false false")
(= code 1) (println "true false")
(= code 2) (println "false true")
(= code 3) (println "true true")))
was it your question ?What about
(case [cond1 cond2]
[true false] :a
[true true] :b
[false true] :c
[false false] :d)
?that’s pretty good
(if cond1 (if cond2 :a :b) (if cond2 :c :d))
alexmiller: I don't find that quite elegant, since I have to write cond2 twice. But it's what I'm using
Or more verbose
(case (mapv boolean [cond1 cond2])
[true false] :a
[true true] :b
[false true] :c
[false false] :d)
you can if you wrap cond1 and cond2 in boolean
oh, sorry you’ve got that :)
i’m not able to import cljs.spec in a clojurescript file and i can’t figure out why.
clojure.lang.ExceptionInfo: No such namespace: cljs.spec, could not locate cljs/spec.cljs, cljs/spec.cljc, or Closure namespace "cljs.spec" in file src/flierplath/db.cljs
declaration in db.cljs:
(ns flierplath.db
(:require [cljs.spec :as s]))
i have recent versions of clojure/script:
[org.clojure/clojure "1.9.0-alpha10"]
[org.clojure/clojurescript "1.9.562"]
It's moved to cljs.spec.alpha
in 1.9.542
https://github.com/clojure/clojurescript/blob/master/changes.md#19542
Hi guys, any volunteers willing to get online with me for a couple of minutes and explain how to implement core.async on an API call am making so I can use the concurrency and make it faster?
I think the reason you're having trouble mocking this to core.async is that your code is somewhat confusingly structured because its assuming coll in many places
I'd re-write your functions to operate on a single element at a time, only mapping over colls at a higher level, then you'd be able to work them into core.async channels expecting one element put onto the channel at a time
If I could get it working in one of the functions using coll
, I will be just fine. Those functions were written for different purposes earlier and am just trying to see how to make it faster
I get that, i'm just pointing out that functions passing around things at the collection level are going to be harder to use with core.async than ones taking an element at a time, and result in a more confusing flow, imho.
@eriktjacobsen Okay, I could work around that, but the first fn for example does the single API call
gotcha, sec writing up example
is there anything like ->
except the value is threaded in as the first thing in every form?
@swizzard Good point, though here it is been used as the first thing in the forms. I had to remind myself of the flexibility as->
provides
You could check out https://github.com/rplevy/swiss-arrows, there are a bunch more variations although it's all a bit punctuation-heavy for my taste
@victora: I'd check out peridot: https://github.com/xeqi/peridot
Cool. We use it a lot for "user does this, then user does this, then user does this..." type of tests
Not the prettiest API but you can use it to build pretty decent integration tests
("integration" in that they test your app's ring integration, I guess)
I need that java method that goes back in time to a fixed point, for tests. Can anyone give me the clojure alternative/way?
@victora I don’t know what you’re using for writing your tests but that’s something Expectations supports… If I’m understanding your question.
Hmm, bit surprised Midje doesn’t have a freeze time option. It seems to have all sorts of weird and fancy feature… Must admit, I don’t like its non-Clojurey DSL so I’ve never really dug into it.
Not sure which method you mean?
timgilbert: I can't seem to find the name, but it was a "go to following date and execute this function" method
@simon had to step out for lunch. I'm not really a big core.async user, so I'm sure better ways to do this. I think reducers or simply pmap would work well here, since you want to collect all the results before the last step, instead of processing each unit of data by itself.
about the original process-points, it’s never neccessary to use _ to bind items at the end of a let block, you can just put them in the function body
defn has an implicit do block
also '()
is just ()
and as an arg to into it leads to reversing your input
(that might be intentional?)
Thanks guys @eriktjacobsen and @noisesmith. I am trying to decipher and implement it now. Given am in Europe, caffeine is barely keeping my eyes open now but in any case I should have some feedback soonest
Also, the take
is redundant as to-chan
closes the channel after exhausting input coll. I found that out after, but left it in for clarity / example.