This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-12-04
Channels
- # admin-announcements (6)
- # alda (1)
- # announcements (3)
- # aws (4)
- # beginners (233)
- # boot (82)
- # cider (11)
- # cljsjs (21)
- # cljsrn (7)
- # clojure (262)
- # clojure-japan (1)
- # clojure-russia (129)
- # clojure-sg (2)
- # clojure-taiwan (2)
- # clojurecup (23)
- # clojurescript (162)
- # clojurex (3)
- # core-async (18)
- # cursive (81)
- # datavis (183)
- # datomic (23)
- # emacs (2)
- # funcool (25)
- # ldnclj (82)
- # lein-figwheel (3)
- # om (196)
- # onyx (74)
- # parinfer (7)
- # portland-or (12)
- # re-frame (60)
- # reagent (48)
- # slack-help (1)
- # yada (9)
awesome, that filter
worked
how come this macro doesn’t work?
(defmacro rev-ord [& args]
`(let [op# ~(last args)]
(apply (resolve op#) ~(quote 3) ~(quote 9))))
(rev-ord [12 21] ‘+)
I get
java.lang.IllegalArgumentException: Don't know how to create ISeq from: java.lang.Long
RT.java:528 clojure.lang.RT.seqFrom
RT.java:509 clojure.lang.RT.seq
RT.java:648 clojure.lang.RT.cons
core.clj:29
Use macroexpand
to see what it produces.
You'll see that you have (apply (resolve op) 3 9)
and apply
expects the last argument to be a sequence
I wonder why it returns nil
...
@trancehime: are we supposed to guess what is returning nil?
(into [] (map (fn [sched] (get sched "schedule_id")) schedulelist))
where schedulelist
is a vector of maps {:schedule_id id}
It's been stumping me for a bit
@trancehime: schould it not be ( get schedule_id :sched)
well my keys are written as "key"
and not :key
since they are JSON.
oke have you tried then `(get "schedule_id" sched) I think you have to order wrong in your get
its (get map key)
as args
so I didn't have the wrong order
it is alright, thanks anyway
That whole thing is producing nil
? Seems odd since even if map
produces an empty sequence you'd get []
not nil
well it doesn't seem to be showing anything, I have the whole thing wrapped into (str )
And your vector of maps is a vector of {"scheduled_id" id}
not keywords, right?
I just need to verify something first
@seancorfield: ok, yeah, it's producing []
and yeah it's a vector of {"schedule_id" id}
So schedulelist
is empty then?
Since map
over schedulelist
must produce a sequence with the same number of elements.
no it's not, that's the thing
My schedulelist
looks like this: [{"schedule_id" 52778}]
Then map
must produce a list of one element of some sort.
So why is my resulting vector empty, wow much confuse
I must be doing something wrong
I just want to get the 52778
into a vector without any of the map structure ;(
If I have this sort of problems I try to make the code first smaller to see what each part produces
good idea
Uncaught TypeError: Cannot read property 'cljs$core$IFn$_invoke$arity$1' of null
well that explains a lot
oh I forgot #
ayyy it worked
thanks @roelof for suggest that, now I have fixed the problem
Ah, so what you posted wasn't the actual code?
It was my first attempt at it then I decided to just do (map #(get % "schedule_id") schedulelist)
instead
apparently that worked
happy for you it worked @trancehime
Yay! Working code!
Yeah, you can get some really bizarre errors if you omit #
.
Working code is good!
How canI desctruct this the best `[ { :account bank :withdraw 200} {:account cash :deposit 200} ]' so account1 has the value bank. amount the withdraw amount , account2 cash and amount2 the deposit value
You can't give things arbitrary names like that. What's the problem you're really trying to solve?
what I try to do is that I can withdraw money from my bank and deposit it into my cash
@seancorfield: is the picture clear ?
Not really. Not sure what restructuring has to do with it. And it's way past bedtime here anyway.
it might be easier to work with atoms
I'm not saying it will be easier, but it could be. I'm not fully familiar with refs though.
I am doing the 4 clojure stuff. shouldn't this be true? (= '(1 4 7 10 13) (take 5 (iterate #(+ 3 %) 1)))
what is wrong here (reduce (fn[acc it] (+ acc (get transactions :withdraw) )) (filter #(contains? % :withdraw) transactions))
What I try to do is to sum all the values of :withdraw in for example this vector [ { :account cash :withdraw 100} { :account cash :withdraw 200} ]
I have now this : (reduce (fn[acc it] (+ acc (get transactions :withdraw) )) (filter #(contains? % :withdraw) transactions)))
when I call it with (process-user-action [ {:account bank :withdraw 200} ] )
I was expecting to see 200 but I see this as output : `=> {:account #object[clojure.lang.Atom 0x629fee00 {:status :ready, :val {:amount 0}}], :withdraw 200}'
roelof: is transactions
an atom?
oh, the value for :account
as an atom, right?
(reduce + (map :withdraw [ { :account :cash :withdraw 100} { :account :cash :withdraw 200} ]))
gives 300 for me
wierd, this code : https://www.refheap.com/112333 does not provide the wrong function
and accounts looks like this : [{:account #object[clojure.lang.Atom 0x7225704a {:status :ready, :val {:amount 0}}], :withdraw 200}]
what is transcations
, the vector?
oke, now with this code : (map (fn[transactions] (get transactions :withdraw)) transactions ) )
I see this answer (200) so almost good
yeah i think it is becaus
you are passing a vector of transactions into your process-user-action
it just so happened that your vector only had 1 action
that why you had to use map
It thus returned (200)
which lazyseq
containing only 200
Aside from Luminus is there any other considerable effort in creating a standard way of doing web development?
Never know that I would be so difficult to get the total amount of for example all the items which has :withdraw in it
@grounded_sage: as far as I know not
@grounded_sage: I haven't seen any that's currently maintained aside from Luminus. I'd guess most people who don't use Luminus work things out on their own (I'm doing my web apps from scratch)
@roelof: yeah, I had similar stuff to figure out when doing my app
@trancehime: any tip then how I can do it a better way
Hmm, do what a better way? The whole thing, or a specific part of your task?
The part if I have this '[ {:account cash :withdraw 200} {:account bank :withdraw 100} I see 300
OK, so basically
It's a shame. I get that composing libraries together yourself is a lot more powerful. However for people that are looking to get up and running and building stuff for people to make money it's quite daunting having to figure it all out yourself. I'm currently weighing up learning Elixir and the Phoenix Framework vs using Clojure. I much prefer the Clojure language but there is a huge benefit with people all rallying behind a standard way of doing things.
Your entire transaction looks like this: [ {:account cash :withdraw 200} {:account bank :withdraw 100} ]
right?
You want to return 300
which is the sum of all values in the :withdraw
key as you are withdrawing
from two accounts, cash
and bank
Do you want one function to accept either :withdraw
or :deposit
?
or would you make separate ones for withdraw
and deposite
@trancehime: both are fine
For simplicity's sake, let's just do one of them
First of all, you are on the right track that you should probably pass in maps of the same structure.
It's implied if you are withdraw
with cash and bank account, you are taking from the bank
Now you wanted to check for value equality, right?
correct , so if someone takes 100 euro from the bank and puts 80 in cash there wll be a error
Right
Then can we both assume the vector will only have 2 maps in it.
[{:account your-account :withdraw value} {:account bank :withdraw value}]
Hmm, let's think about that for a moment. In a use case where you have to include taxes or interest, how would you model that in the transaction.
This is important because it'll affect how we process the transaction parameter
(For example do we want [[] []]
)
(Or do we want a massive [{} {} {} {}]
)
[ {:account cash :deposit 100} {:account taxes :withdraw 20} {:account sales : withdraw 80} ] as a example
Okay, good, so we clear up it's just a single vector containing all the stuff.
Now you mentioned for it to be valid
, the :deposit 100
should be equal to the withdraw
, right?
we are definitely getting somewhere now
Here's what I would suggest - your transaction should be modeled like this:
Assuming your use case is withdrawing some money to put into your :account cash
You should have {:account cash :deposit value}
separate from your vector containing your :withdraw
maps
That way, you can run map
on your vector to sum up your :withdraw
values and compare that directly to the value of (get {:account cash :deposit value} :deposit)
If the values are equal, then good, it's valid. Otherwise, do something else to inform the user that it's invalid
a vector [ {:account bank : withdraw 100} ] and a vector [{:account cash :deposit 100}]
Yeah, we can roll with that
there might be a case where you withdraw from just the bank but deposit into 2 different accounts
You'd still have to check that the sum of :deposit
is equal to :withdraw
So it works both ways
you can use reduce
to help you sum up the values, I think.
I'm gonna grab a cookie and come back in case you need someone to ask again
Oh yeah, no kidding, I'm new to CLJ+CLJS too lol
chips when I try this ` (reduce (fn[acc] (+ acc (get {:account bank :deposit value} :withdraw))) transactions ))'
your fn
should accept 2 arguments
usually, your function's arguments are the collection you're passing and each item in the collection
Note: (reduce + [1 2 3])
should return 6
what about this: (reduce + (into [] (map #(get % :deposit) transactions)))
It's not very elegant but it's quarter to 10 PM right now so haha
just change :deposit
to :withdraw
if you have to
How this works? 😮
(map #(get % :deposit) transactions)
is the same as (map (fn [transaction] (get transaction :deposit)) transactions)
I think I have a idea , The into takes care that all the amounts are in a map so reduce could handle it
this gives you a sequence of all :deposit
values
wrapping (into [] ...)
simply just puts the values of the sequence into a vector
the vector of all the values is then run into a simple (reduce + ...)
which should give you the sum of all :deposit
values
into
is a very handy function if you want to change what type of collection something is
No problem
I initially started developing everything in Clojure before moving to CLJ+CLJS/Reagent
It helped me a lot to pick up the basics and refreshing myself on FP
Time's not really on my side, since it's part of a work assignment I received since November
Right, take care
Hehe, thanks I'm sure it will, it's a big thing for me to shoulder on my own
how to make this function smaller `(if (not(zero? ( - (reduce + (into [] (map #(get % :withdraw) withdraw-tranactioons))) (reduce + (into [] (map #(get % :deposit) deposit-tranactions))))))'
if (not(zero? ( - (reduce #(+ (:withdraw %2) %1) 0 w) (reduce #(+ (:deposit %2) %1) 0 d))))
Hi everyone!… how important is to know the Java library ecosystem in order to create real applications in Clojure? I come from a C++ background so I am very unfamiliar with the Java libraries.
it is hard to avoid learning at least something about the core Java API libraries (I/O, collections, strings, dates)
Clojure generally takes the approach that it will embrace the host (JVM) library when it has nothing to add - so that stuff is often invoked directly via Java interop
similarly, there are many useful Java libraries and it is often easiest to interact with them directly
although this depends a lot on the domain
Ok, that sounds reasonable. I guess I should focus on learning Clojure and “jump” to Java land as needed. What do you mean with collections?, Stacks? Queues?