Fork me on GitHub
#clojure
<
2015-12-05
>
Alex Miller (Clojure team)02:12:22

The section about priorities was targeted at the language rather than tools

trancehime02:12:42

I just started using/learning Clojure =o Had no idea there was a community survey for that

roelof07:12:11

I have 2 refs which represent a account like cash or bank. Now I want to calculate the new amount of the accounts. A transaction could look like this [ { :account cash :withdraw 100} ] [ :account bank :deposit 100] }

roelof07:12:45

I know I have to use alter but how do I do it when everything step of a transaction is in a map.

roelof07:12:14

and the map could have more steps then 1 as my example have shown

roelof08:12:25

or can I do something like this ( map (alter ....... ) withdraws) and the same for the deposits

wei09:12:55

is there a way to get a descending infinite sequence out of range?

cjmurphy09:12:47

Does reverse realise it?

cjmurphy09:12:32

Hmm - isn't that kind of impossible?

cjmurphy09:12:40

Calling first is supposed to give you infinity?

cjmurphy09:12:14

Anyway I don't think range is used to create an infinite sequence.

trancehime09:12:26

I don't understand that question.

trancehime09:12:36

What is a "descending infinite sequence"?

cjmurphy09:12:06

Whether it is possible or not you would want to start not with range but repeat or iterate - something that returns an infinite sequence.

cjmurphy09:12:27

Theoretically you could create such a thing and get the last value.

wei09:12:17

guess there were multiple ways to interpret that. I was looking for something like -1, -2, -3 ….

wei09:12:22

here’s my non-infinite hack:

(defn neg-range []
  (range -1 (- (java.lang.Long/MAX_VALUE)) -1))

wei09:12:15

in case anyone’s wondering, my use case is generating tempids for datomic

wei09:12:22

@cjmurphy: remembered that I could (iterate dec -1), thanks

trancehime09:12:11

Oh, an infinite sequence of negative values?

trancehime09:12:37

So literally descending from some positive value down to "negative infinity" (emphasis on quotes)

trancehime09:12:51

It seems you got it though while I was in the shower

roelof09:12:18

@cjmurphy: Am I on the right track with my map alter problem I posted earlier ?

cjmurphy09:12:26

Can I meet you in beginners? I'll re-join beginners...

sveri10:12:02

Hi. let's say I have a list of maps. What is the best way to find a map in that list, update the map and replace the old map with the new one? Is there an idiom for this?

sveri10:12:30

Right now I'd do it manually by splitting it into the described three steps, but I wonder if it could be done better?

trancehime10:12:00

Is your list of maps kept in a vector

trancehime10:12:08

Or is it literally just a list of maps

sveri10:12:00

@trancehime: They are kept in a vector

trancehime10:12:26

Well I guess to update a map you could use assoc

sveri10:12:59

Yea, I get that. What I find repetitive is: get map from list, update map, put back into the same spot in the list

trancehime10:12:13

assoc is basically your step 2 and 3 merged into 1 single step.

trancehime10:12:43

Well, possibly run map-indexed on the vector?

wei10:12:06

update-in?

jaen10:12:24

You totally can update / update-in on a vector

trancehime10:12:26

update-in the index of the map you want to update

sveri10:12:40

Ah, ok...so I just need to get the index

sveri10:12:47

That sounds reasonable

jaen10:12:50

(update [{:a 1}{:b 2}] 1 assoc :c 2)

jaen10:12:58

Though I've just learned that's only since 1.7

jaen10:12:13

update-in is since 1.0

sveri10:12:33

Thanks everyone

sveri10:12:51

I wonder if stuff like that is easier done with specter. Anyone tried it to that extent?

trancehime10:12:56

I personally haven't used specter

jaen10:12:53

Didn't have a need to use it yet either, but it's probably more noticeable with more complex data structures/selectors.

jaen10:12:00

This seems like quite a simple case.

sveri10:12:52

Well. the structure has more nesting, 1 lvl more, to be exact

jaen10:12:41

That depends on what the level more means, I suppose - if it means you have to recursively find an index, then yeah, that starts to look annoying; if that just means the map is nested that's probably not much of an issue.

sveri10:12:54

the map looks like this: {...some...stuff... :new-entity {:columns [{:key "val"}{...}...]}} so yea, it's not index based

sveri10:12:03

still it starts looking ugly

jaen10:12:03

I suppose it starts to looks like something lenses or specter could help with, for sure.

trancehime11:12:41

Oh god yeah, you might want to look into that then

sveri11:12:27

Yea, I am about to

wei11:12:28

I’m digging into specter too, just made #C0FVDQLQ5

sveri11:12:56

So just for reference, the problem I had can be solved with specter like this:

sveri11:12:09

(spec/transform [:new-entity :columns spec/ALL #(= (:id %) 1)] #(assoc % :nullable true) tm)

sveri11:12:40

where 1 is the id of the entity and tm is the whole data structure. Looks better than get-in and update-in, at least, to me

roelof12:12:37

Another question : I have a vector of maps like this [ {:arg1 test :arg2 test2}] Now I need to have the contents of arg1 and arg2 for another function. So I could use map to iterate over the vector and get the first element. So far so good. Then I need a anymous function which as far as I know needs 2 arguments. But the arguments are not known till the first element is chosen. I can named it "it" so I can do (get :arg1 it) but I see then that I need to have 2 arguments. How to solve this one ?

ian13:12:54

@roelof: you mean you just need the values of every key in every map in a vector?

roelof14:12:22

no, what I want is this

roelof14:12:57

I have a vector of maps '[ {:arg1 test :arg2 test2}]`

roelof14:12:18

now I need test and test2 in a alter function

roelof14:12:44

so basically im doing this (map (fn [ ??? it ] ) (alter (get :arg it) (get :arg2 it) coll)))

roelof14:12:17

but when I only do it clojure complains that i need two arguments instead of 1

sveri16:12:33

@roelof: Maybe you could provide a real example with input and outcome you want to have via pastebin?

roelof17:12:10

@sveri I can tell it here.

roelof17:12:12

Let's say I have a example input of [ { :account cash :deposit 100} {:account bank :withdraw 100} ]

roelof17:12:30

Then I want the first time as function (alter deposit cash 100) and the second time (alter withdraw bank 100)

roelof17:12:18

So that the refs cash and bank with holds both {:amount xxx } will be updated

bigkahuna17:12:30

#C075TNSSC Talks were recorded and I think majority are now available. https://skillsmatter.com/conferences/6861-clojure-exchange-2015#program Enjoy. Check out Bozhidar on CIDER. Very entertaining.

roelof17:12:32

I hope this is clear enough ?

sveri18:12:04

Hm, using schema I found that (s/one Keyword "name") requires the "name" part. Anyone knows the reasoning behind it?

sveri19:12:45

Basically either I have [:varchar 300] or :text but schema tells me this: No implementation of method: :spec of protocol: #'schema.core/Schema found for class: schema.core.One. It looks like it is not supported by schema, is that right?

jaen19:12:56

Huh, that's a weird schema. What are the example of data you want to type?

sveri19:12:41

@jaen: this one: [:first_name [:varchar 30]]

sveri19:12:52

but it might also be like this: [:first_name :text]

sveri19:12:01

this is where the weird either comes from

sveri20:12:12

Ok, so this is the cleaner schema I came up with:

sveri20:12:17

And I get the same error, if I remove the either, it works

jaen20:12:11

I think either is deprecated, but hmm, I don't think that's an issue here

roelof20:12:28

sveri: Is my explanation clear

sveri20:12:00

@jaen got it -.- the step by step approach blurred my eyes. This is the solution:

sveri20:12:42

I had s/one inside s/->Either, but is has to be the other way around

sveri20:12:05

thanks for looking at it simple_smile

jaen20:12:12

Let me check something then

sveri20:12:29

@roelof: Sorry. Not to me, I don't get what you want

ian20:12:03

@roelof: the way your maps are setup seems confusing - can you change their shape?

roelof20:12:57

@ian : yes, I can

roelof20:12:10

I thought this was the best way

roelof20:12:12

Maybe more this way : `[ {:debet {:account "cash" :amount 100 }} { :credit {:account "bank" :amount 100}} ]'

ian20:12:56

Well it would be easy if the keys were always consistent, in my opinion. [{:account "cash" :type :deposit :amount 100} {:account "bank" :type :withdraw :amount 100}]

roelof20:12:27

can also be working

Chris O’Donnell20:12:53

What are you trying to accomplish with that vector @roelof ?

roelof20:12:25

and how can I convert this so alter could use the type , the account and the amount part

jaen20:12:51

@sveri: ok, this seems all right, it turns out I was kinda confused about something instead. You can totally use (s/either varchar other-type) though, no need to resort to using the record directly.

jaen20:12:55

Though I'm curious now

Chris O’Donnell20:12:03

So you have some account information in a ref, then? What does that ref look like?

roelof20:12:03

It 's a sort of financial transaction. Its now a very simple accounting system

sveri20:12:17

@jaen: the docs say it's deprecated, that's why I chose it

jaen20:12:22

What is the preferred way to specify something like either without it.

jaen20:12:53

@sveri: yeah, but as I understand it Either is an implementation detail of either, so both are deprecated.

jaen20:12:22

But I'm not really sure how to replicate either behaviour with the conditional schema they suggest.

sveri20:12:32

true, should have looked right, they suggest cond-pre

roelof20:12:51

@codonnell: like this (def cash (ref {:amount 0}))

sveri20:12:36

Ok, another schema problem where I dont know how to handle it conceptually. My vecs may also look like this:

sveri20:12:28

I got the first two parts like :email and :time. But how would I specify that if something comes afterwards it has to contain two things. and then they are also dependent. so a :boolean type can only have :default boolean. But a :text type might have a :default string. and :null can only be followed by a boolean

jaen20:12:13

Sounds like a job for the conditional schema

jaen20:12:40

You'll have to switch the schema based on the type

sveri20:12:41

does it support if statements and alike?

Chris O’Donnell20:12:26

something like that should work @roelof

jaen20:12:20

@sveri: it's like cond

Chris O’Donnell20:12:24

Though I think it would be fine to put all of the accounts into one ref.

jaen20:12:28

(s/conditional
  #(= % 12) s/Int
  :else s/Str)

jaen20:12:31

A contrived example

jaen20:12:39

You get the value you match on as the argument

sveri20:12:45

@jaen I see there are ifs and stuff. That might be part of the solution. But how would I type :null must be followed by false and if one is there the other one has to be there too?

jaen20:12:30

I think there's pair as well, just like one

sveri20:12:03

Indeed...ok, thank you. I will give it a try

andrewboltachev20:12:48

Hi. Is there a way to show list of public resources on classpath?

jaen20:12:50

But it means

jaen20:12:56

(s/validate table-column [:test [:varchar 12] [:null false]])

jaen20:12:59

unfortunately

roelof20:12:05

codonnell : I thought I need to use dosync with alter for transactions

Chris O’Donnell20:12:56

You do. In my code snippet, all of the alter calls are made within a call to dosync

roelof20:12:22

i saw , I forget to expand the codee

roelof20:12:53

so two seperate functions for updating the ref and one for calculating the new amount

roelof20:12:36

codonell : thanks I see now how to solve it

roelof20:12:09

you saved my programm which I have in mind

Chris O’Donnell20:12:37

I'm glad. There are a couple problems I already see with my snippet above, but I think you should be able to iron them out. I really think it would be better to put all of your accounts into a single ref.

roelof20:12:22

I think I do, I ask here for a idea how to solve it and you provided it

roelof20:12:27

so again thanks

sveri20:12:33

I think I ask on their mailing list, have not found anything scrolling through their API doc

roelof20:12:50

byee all and have a good weekend

jaredly21:12:52

has anyone ever tried to do a static validator for prismatic/Schema? Certainly not something that would restrict you (it would probably need to be overly optimistic in lots of cases), but something that can give you some type errors w/o running your code?

jeff.terrell21:12:56

That's the idea behind core.typed, but it doesn't use the same type language as Schema.

jeff.terrell21:12:15

I mean, you have to specify the types differently.

jaredly21:12:30

but core.typed is more restrictive

jaredly21:12:03

I get the feeling people like prismatic schema b/c core.typed isn't expressive enough to capture common clojure idioms (http://blog.getprismatic.com/schema-for-clojurescript-data-shape-declaration-and-validation/)

jaredly21:12:24

And I thought it would be cool to have a tool that gives "best effort" static type errors for Schema

jeff.terrell21:12:07

Right, yeah. As far as I know that's about the only option for validations like that at compile time, though.

roelof21:12:29

@codonnell: one question. Does this work also when I do something like this : [{:account "sales " :type :deposit :amount 100} {:account "taxes" :type :withdraw :amount 20} {:account: "payable" :type: withdraw :amount 80 ] so when there are more then 1 deposit or withdraw.

Chris O’Donnell21:12:50

@roelof: Indeed, the (doseq [t transactions] bit means the following code will execute on each element of the transactions collection, binding t to the transaction value.

andrewboltachev22:12:02

Hi. In Clojure, what's a best way to locate my project's destination in the filesystem? I want to have "file upload" folder's location to be automatically defined

andrewboltachev22:12:29

though I could create "upload" folder somewhere in ~/.myprogram directory of the server, I don't know the way of serving particular folder in Compojure, other than resources/public

roelof22:12:55

@codonnell: thanks, learned another thing. I Always thought that I can only use map or reduce.

roelof22:12:26

I will read the pages I can find about doseq

roelof22:12:37

Thanks a lot for the lessons