Fork me on GitHub
#beginners
<
2015-12-04
>
noonian00:12:13

(apply (resolve '+) [32 32])

tmtwd00:12:31

cool thanks

trancehime03:12:37

awesome, that filter worked

tmtwd03:12:17

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] ‘+)

tmtwd03:12:41

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

seancorfield04:12:15

Use macroexpand to see what it produces.

seancorfield04:12:35

You'll see that you have (apply (resolve op) 3 9) and apply expects the last argument to be a sequence

tmtwd04:12:49

thank you simple_smile

trancehime05:12:53

I wonder why it returns nil...

seancorfield05:12:35

@trancehime: are we supposed to guess what is returning nil? simple_smile

trancehime06:12:55

(into [] (map (fn [sched] (get sched "schedule_id")) schedulelist)) where schedulelist is a vector of maps {:schedule_id id}

trancehime06:12:34

It's been stumping me for a bit

roelof06:12:11

I wonder if the get is allright. IM used to write (get amount :amount )

roelof06:12:25

@trancehime: schould it not be ( get schedule_id :sched)

trancehime06:12:01

well my keys are written as "key" and not :key since they are JSON.

roelof06:12:24

oke have you tried then `(get "schedule_id" sched) I think you have to order wrong in your get

trancehime06:12:15

its (get map key) as args

trancehime06:12:21

so I didn't have the wrong order

roelof06:12:58

oke, then I do not have a clue, sorry

trancehime06:12:06

it is alright, thanks anyway

seancorfield06:12:21

That whole thing is producing nil? Seems odd since even if map produces an empty sequence you'd get [] not nil

trancehime06:12:49

well it doesn't seem to be showing anything, I have the whole thing wrapped into (str )

seancorfield06:12:58

And your vector of maps is a vector of {"scheduled_id" id} not keywords, right?

trancehime06:12:11

I just need to verify something first

trancehime06:12:28

@seancorfield: ok, yeah, it's producing []

trancehime06:12:48

and yeah it's a vector of {"schedule_id" id}

seancorfield06:12:56

So schedulelist is empty then?

seancorfield06:12:18

Since map over schedulelist must produce a sequence with the same number of elements.

trancehime06:12:27

no it's not, that's the thing

trancehime06:12:51

My schedulelist looks like this: [{"schedule_id" 52778}]

seancorfield06:12:18

Then map must produce a list of one element of some sort.

trancehime06:12:41

So why is my resulting vector empty, wow much confuse

trancehime06:12:46

I must be doing something wrong

trancehime06:12:05

I just want to get the 52778 into a vector without any of the map structure ;(

roelof06:12:20

if you do use only the get it produces a good output ?

roelof06:12:53

If I have this sort of problems I try to make the code first smaller to see what each part produces

trancehime06:12:24

Uncaught TypeError: Cannot read property 'cljs$core$IFn$_invoke$arity$1' of null well that explains a lot

trancehime06:12:49

oh I forgot #

trancehime06:12:12

ayyy it worked

trancehime06:12:25

thanks @roelof for suggest that, now I have fixed the problem

seancorfield06:12:56

Ah, so what you posted wasn't the actual code? simple_smile

trancehime06:12:45

It was my first attempt at it then I decided to just do (map #(get % "schedule_id") schedulelist) instead

trancehime06:12:50

apparently that worked

roelof06:12:04

happy for you it worked @trancehime

seancorfield06:12:44

Yay! Working code!

seancorfield06:12:02

Yeah, you can get some really bizarre errors if you omit #.

trancehime07:12:34

Working code is good!

roelof07:12:27

all the time working code is great

roelof07:12:40

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

roelof07:12:07

its now 2 things but it can also be three or more later

seancorfield07:12:53

You can't give things arbitrary names like that. What's the problem you're really trying to solve?

roelof07:12:14

I have a app where I can keep track of my wallet

roelof07:12:35

but now I can only withdraw and deposit money

roelof07:12:05

what I try to do is that I can withdraw money from my bank and deposit it into my cash

roelof07:12:26

According to noonian I could be done with refs and dosync

roelof07:12:54

I try to mimick this with accounts do

roelof07:12:13

to bank 100`

roelof07:12:06

@seancorfield: is the picture clear ?

seancorfield07:12:11

Not really. Not sure what restructuring has to do with it. And it's way past bedtime here anyway.

roelof07:12:39

good night

trancehime07:12:15

it might be easier to work with atoms

roelof08:12:49

I can also try this with atoms

trancehime08:12:28

I'm not saying it will be easier, but it could be. I'm not fully familiar with refs though.

roelof08:12:45

not a problem, I have a little project that I use to play with some things

roelof08:12:08

In the future this project would be a real project

sveri09:12:44

I am doing the 4 clojure stuff. shouldn't this be true? (= '(1 4 7 10 13) (take 5 (iterate #(+ 3 %) 1)))

sveri09:12:59

my repl says its true...maybe an older version on 4clojure?

sveri09:12:43

Totally my bad...I pasted the whole expression

roelof11:12:03

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} ]

roelof11:12:15

but I see this error in repl : Map literal must contain an even number of forms

roelof11:12:52

oke found it

roelof11:12:04

I make a mistake on calling the function

roelof11:12:12

but I see another problem :

roelof11:12:01

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}'

roelof11:12:25

How can I make it work so I can see 200 which is the total of the amounts

ordnungswidrig11:12:21

roelof: is transactions an atom?

roelof11:12:23

no, its a vector

roelof11:12:37

see the process-user-action call

ordnungswidrig11:12:00

oh, the value for :account as an atom, right?

roelof11:12:56

yes, account is a atom : (def bank (atom {:amount 0}))

ordnungswidrig11:12:49

(reduce + (map :withdraw [ { :account :cash :withdraw 100} { :account :cash :withdraw 200} ]))

ordnungswidrig12:12:00

gives 300 for me

roelof12:12:20

wierd, this code : https://www.refheap.com/112333 does not provide the wrong function

roelof12:12:27

I think the problem is somewhere in the get part

roelof12:12:22

when I do only (get transactions :withdraw) I see this output : nil

roelof12:12:19

and accounts looks like this : [{:account #object[clojure.lang.Atom 0x7225704a {:status :ready, :val {:amount 0}}], :withdraw 200}]

ordnungswidrig12:12:51

what is transcations, the vector?

roelof12:12:43

that is the transactions that need to be handled

roelof12:12:39

oke, now with this code : (map (fn[transactions] (get transactions :withdraw)) transactions ) ) I see this answer (200) so almost good

roelof12:12:05

clojure is driving me grazy now

roelof12:12:53

giving up for today

roelof12:12:25

this has already costs me the whole morning and a big piece of my afternoon

trancehime12:12:59

yeah i think it is becaus

trancehime12:12:12

you are passing a vector of transactions into your process-user-action

trancehime12:12:23

it just so happened that your vector only had 1 action

trancehime12:12:43

that why you had to use map

trancehime12:12:29

It thus returned (200) which lazyseq containing only 200

grounded_sage12:12:45

Aside from Luminus is there any other considerable effort in creating a standard way of doing web development?

roelof12:12:13

Never know that I would be so difficult to get the total amount of for example all the items which has :withdraw in it

roelof12:12:34

@grounded_sage: as far as I know not

trancehime12:12:36

@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)

trancehime12:12:34

@roelof: yeah, I had similar stuff to figure out when doing my app

roelof12:12:13

@trancehime: any tip then how I can do it a better way

trancehime12:12:13

Hmm, do what a better way? The whole thing, or a specific part of your task?

roelof12:12:16

The part if I have this '[ {:account cash :withdraw 200} {:account bank :withdraw 100} I see 300

roelof12:12:09

or a better way to use for transactions parameter

trancehime12:12:56

OK, so basically

grounded_sage12:12:58

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.

trancehime12:12:26

Your entire transaction looks like this: [ {:account cash :withdraw 200} {:account bank :withdraw 100} ] right?

trancehime12:12:59

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

roelof12:12:06

that can be a example

roelof12:12:35

yes, so I can check if the transaction is valid I need the total of withdraw

roelof12:12:55

and later on the same for deposit

roelof12:12:11

these totals needs to be the same

trancehime12:12:25

Do you want one function to accept either :withdraw or :deposit?

roelof12:12:28

so the above example is not valid

trancehime12:12:48

or would you make separate ones for withdraw and deposite

roelof12:12:03

@trancehime: both are fine

roelof13:12:44

you only have to make one of the two

trancehime13:12:02

For simplicity's sake, let's just do one of them

roelof13:12:09

Im just curious where I did take the wrong way

trancehime13:12:24

First of all, you are on the right track that you should probably pass in maps of the same structure.

trancehime13:12:01

It's implied if you are withdraw with cash and bank account, you are taking from the bank

trancehime13:12:16

Now you wanted to check for value equality, right?

roelof13:12:56

correct , so if someone takes 100 euro from the bank and puts 80 in cash there wll be a error

roelof13:12:28

all the amounts of withdraw must be the same as all the amounts of deposit

trancehime13:12:24

Then can we both assume the vector will only have 2 maps in it.

trancehime13:12:54

[{:account your-account :withdraw value} {:account bank :withdraw value}]

roelof13:12:02

for now yes

roelof13:12:09

later on it could be more

roelof13:12:27

if I want to include things like taxes

trancehime13:12:13

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.

trancehime13:12:30

This is important because it'll affect how we process the transaction parameter

trancehime13:12:48

(For example do we want [[] []])

trancehime13:12:50

(Or do we want a massive [{} {} {} {}])

roelof13:12:42

[ {:account cash :deposit 100} {:account taxes :withdraw 20} {:account sales : withdraw 80} ] as a example

roelof13:12:13

I use now accounts I do not have so just a example of a sales with taxes

trancehime13:12:32

Okay, good, so we clear up it's just a single vector containing all the stuff.

roelof13:12:48

I could change the transaction if there is a better alternative

roelof13:12:12

im just trying some ideas

trancehime13:12:33

Now you mentioned for it to be valid, the :deposit 100 should be equal to the withdraw, right?

roelof13:12:02

yes, so for my sales example 100 = 20 + 80

roelof13:12:09

I made a typo in it , sorry

roelof13:12:14

I will edit it

trancehime13:12:21

we are definitely getting somewhere now

trancehime13:12:48

Here's what I would suggest - your transaction should be modeled like this:

trancehime13:12:32

Assuming your use case is withdrawing some money to put into your :account cash

trancehime13:12:57

You should have {:account cash :deposit value} separate from your vector containing your :withdraw maps

trancehime13:12:31

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)

trancehime13:12:05

If the values are equal, then good, it's valid. Otherwise, do something else to inform the user that it's invalid

roelof13:12:29

oke, so for one transaction make two seperate vectors

roelof13:12:48

a vector [ {:account bank : withdraw 100} ] and a vector [{:account cash :deposit 100}]

roelof13:12:47

Do I understand you right ?

trancehime13:12:55

Yeah, we can roll with that

trancehime13:12:12

there might be a case where you withdraw from just the bank but deposit into 2 different accounts

roelof13:12:25

yep, that can be

trancehime13:12:25

You'd still have to check that the sum of :deposit is equal to :withdraw

trancehime13:12:33

So it works both ways

trancehime13:12:02

you can use reduce to help you sum up the values, I think.

roelof13:12:06

and I can find the total with ' (reduce (fn[acc it] (+ acc it)) withdraw )'

roelof13:12:38

it will have to be (get {:account cash :deposit value} :deposit)

roelof13:12:04

I will try it that way

roelof13:12:11

I hope this one will work

trancehime13:12:37

I'm gonna grab a cookie and come back in case you need someone to ask again

roelof13:12:51

this "simple" things costs me a lot of hours but I still learn a lot

trancehime13:12:02

Oh yeah, no kidding, I'm new to CLJ+CLJS too lol

roelof13:12:26

chips when I try this ` (reduce (fn[acc] (+ acc (get {:account bank :deposit value} :withdraw))) transactions ))'

roelof13:12:34

value is unknown 😞

trancehime13:12:58

your fn should accept 2 arguments

roelof13:12:06

oke, so I have to think what the second argument is

trancehime13:12:39

usually, your function's arguments are the collection you're passing and each item in the collection

trancehime13:12:00

Note: (reduce + [1 2 3]) should return 6

roelof13:12:39

I know but each item is known if we have used the get function

roelof13:12:02

that is what bugging me the whole day 😞

roelof13:12:14

maybe use a let here so value is (get {:account cash :deposit value} :deposit)`

trancehime13:12:39

what about this: (reduce + (into [] (map #(get % :deposit) transactions)))

roelof13:12:33

that looks to work

trancehime13:12:02

It's not very elegant but it's quarter to 10 PM right now so haha

roelof13:12:28

and it works even with 2 maps

roelof13:12:45

oke, it's here 14:45 in the afternoon

roelof13:12:55

here = the Netherlands

trancehime13:12:59

just change :deposit to :withdraw if you have to

roelof13:12:28

now time to find out how this works instead of my old code

trancehime13:12:46

How this works? 😮

roelof13:12:28

yes, why the code with into works and the rest is not working

roelof13:12:51

Nice part I think because IM also learning

trancehime13:12:33

(map #(get % :deposit) transactions) is the same as (map (fn [transaction] (get transaction :deposit)) transactions)

roelof13:12:33

I think I have a idea , The into takes care that all the amounts are in a map so reduce could handle it

trancehime13:12:49

this gives you a sequence of all :deposit values

trancehime13:12:07

wrapping (into [] ...) simply just puts the values of the sequence into a vector

trancehime13:12:34

the vector of all the values is then run into a simple (reduce + ...) which should give you the sum of all :deposit values

roelof13:12:48

yep, I figuyred it out by watching and looking in my head what really happens

trancehime13:12:17

into is a very handy function if you want to change what type of collection something is

roelof13:12:39

thanks for the help and thinking with me

roelof13:12:33

I still have a lot to learn

roelof13:12:38

especially thinking in smaller parts , I still want to do too much in one time

trancehime13:12:06

I initially started developing everything in Clojure before moving to CLJ+CLJS/Reagent

trancehime13:12:20

It helped me a lot to pick up the basics and refreshing myself on FP

roelof13:12:12

I still do everything in clojure . CLJ/CLJS/Reagent is a step to far right no

roelof13:12:22

First learn to master clojure

trancehime13:12:42

Time's not really on my side, since it's part of a work assignment I received since November

roelof14:12:07

For me it is . Programming is a hobby of mine

roelof14:12:20

so no dead-lines except I make one

roelof14:12:14

again thanks. IM leaving for a moment. Have to do some shopping. Busy weekend ahead

trancehime14:12:28

Right, take care

roelof14:12:06

and I hope your work assignment will bring your further on your profesion

trancehime14:12:21

Hehe, thanks I'm sure it will, it's a big thing for me to shoulder on my own

roelof14:12:20

take care and do not forget to relax often

roelof15:12:28

how to make this function smaller `(if (not(zero? ( - (reduce + (into [] (map #(get % :withdraw) withdraw-tranactioons))) (reduce + (into [] (map #(get % :deposit) deposit-tranactions))))))'

paszek15:12:08

if (not(zero? ( - (reduce #(+ (:withdraw %2) %1) 0 w) (reduce #(+ (:deposit %2) %1) 0 d))))

roelof15:12:22

@paszek: thanks

rcanepa20:12:51

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.

alexmiller20:12:39

it is hard to avoid learning at least something about the core Java API libraries (I/O, collections, strings, dates)

alexmiller20:12:11

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

alexmiller20:12:29

similarly, there are many useful Java libraries and it is often easiest to interact with them directly

alexmiller20:12:35

although this depends a lot on the domain

rcanepa20:12:18

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?

sveri20:12:21

@rcanepa: From my point of view basic web development is fully clojure / clojurescript stuff.

rcanepa20:12:00

@sveri: Good to know!