Fork me on GitHub
#clojure
<
2015-12-15
>
escherize00:12:29

woah I just found a reason one might prefer to use %1 instead of %

a.espolov00:12:39

Guys how to sort-by for nested maps?

a.espolov00:12:04

example structure [{:a {:b {:c 1}} {:a {:b {:c 0}}]

escherize00:12:46

.. or so I thought

escherize00:12:40

;; in cljs
(#(+ 10 %1) 1 2 3) 
;;=> 11
yippie, however...
(#(+ 10 %) 1 2 3) 
;;=> 11

escherize00:12:03

@a.espolov: that structure you posted wouldn't compile.

escherize00:12:33

Also, you can sort it like this; (sort-by (comp :c :b :a) [{:a {:b {:c 1}}} {:a {:b {:c 0}}}])

a.espolov01:12:48

(->> inspection-by (when (not (nil? (:sorting params))) (sort-by (comp :promo/name :inspection/promo)) (when (:value (:status (:filters params))) (filter #(= :inspection.status/unverified (:inspection/status %)))))) this way it’s ok?

a.espolov01:12:11

throw exception Wrong number of args (1) passed to: core/sort-by

a.espolov01:12:56

I understand that the macro - >> will not work with sort-by?

dmitrig0101:12:00

@a.espolov: it's trying to substitute it into the first argument of the when

dmitrig0101:12:45

additionally, even if this were to work, if (:sorting params) is nil, then the when will return nil and pass a nil down to the next function which is probably not what you want to happen

a.espolov01:12:14

position irrelevant

dmitrig0101:12:57

sorry - trying to put it in the last argument of when. try running a macroexpand on it

dmitrig0101:12:33

(macroexpand-1 '(->> inspection-by
                                   (when (not (nil? (:sorting params)))
                                     (sort-by (comp :promo/name :inspection/promo))
                                   (when (:value (:status (:filters params)))
                                     (filter #(= :inspection.status/unverified
                                                 (:inspection/status %)))))))

a.espolov01:12:04

so everything should be ok

a.espolov01:12:18

inspection-by is not empty collection

dmitrig0101:12:42

did you run that?

a.espolov01:12:17

@dmitrig01: if usage “when” for run sort-by or not run then throw exception

a.espolov01:12:03

@dmitrig01: whether you can use a macro - >> and functions at a branch of a chain of function calls?

Josh Horwitz01:12:28

Hi everyone, new to Clojure and hoping to make a website for a friends nonprofit using it

Josh Horwitz01:12:52

I'm working my way through the clojure for the brave and true and as a Java developer it's just very hard to grasp the recursion and functional programming, very frustrating!

ghadi03:12:38

joshua.d.horwitz: keep at it brother. your brain will be taffy for a little while, but it will click.

polymeris03:12:23

In the same boat myself, @joshua.d.horwitz It gets easier fast! Not that I understand any of the "advanced" topics like macros... but after a month I can get some stuff done, at least.

Josh Horwitz03:12:42

@polymeris: What are you using to learn?

polymeris03:12:19

I am reading The Joy of Clojure, but mostly I am just experimenting

polymeris03:12:37

Using the luminus stack as a base

polymeris03:12:28

Tool-wise, I am just using IDEA with the cursive plugin. Learing emacs alongside clojure seemed like too much

polymeris03:12:04

There is a #C053AK3F9 channel on this slack, btw

anisoptera04:12:18

once it finally settles into place youll want to write everything in clojure

roelof06:12:54

Is this one : http://rundis.github.io/blog/2015/buddy_auth_part2.html a good example how to make autherisation work ?

rantingbob06:12:16

@polymeris: I might get the Joy of Clojure....think it adds anything that you don't get from Brave and True?

cjmurphy07:12:00

Yes it adds quite a lot. It is a more difficult read as well.

cjmurphy07:12:32

Shows you how to intermingle code/data early on, close over to create 'functional objects' (my expression). It is the first time I really saw what persistent data structures are.

jmglov08:12:21

Hopefully someone from the Clojure core team hangs out here, because I suspect they'd be best-placed to answer: I'd like to port clojure.tools.cli to Pixie (https://github.com/pixie-lang/pixie), and I want to make sure this is OK before pushing to GitHub.

jmglov08:12:45

If my reading of the Eclipse Public Licence is accurate, it seems that as long as I EPL derived code and preserve the contributors notice from the original, it should be legal to do.

jmglov08:12:12

However, I want to also make sure that it is in the spirit of the Clojure community before proceeding.

jmglov08:12:09

My feeling is that it benefits Clojurists to provide familiar libraries in Pixie, so when Pixie is a better fit than Clojure (shell scripts and other places where JVM startup time or memory overhead all but rules out Clojure), there is very minimal friction to being productive.

mpenet08:12:22

it's epl 1.0, so you can just do it without asking as long as you respect the license

mpenet08:12:56

I think somebody started a cli lib for pixie recently tho, you might want to check it first

mpenet08:12:51

-> https://github.com/thomasmulvaney/invoke, but it's in a very early stage actually

jaen08:12:32

Also, some people don't like the EPL since it forbids them to release code as GPL, so there's that to keep in mind.

jaen08:12:33

Pixie is LGPL so I see no problem with the languge itself, but it's still might be a valid point nonetheless.

jmglov08:12:42

@mpenet: I've been talking to Thomas. simple_smile

jmglov08:12:55

He's happy with having tools.cli in Pixie.

jmglov08:12:32

I think he might add his Sparkle stuff from invoke to my tools.cli port.

jmglov08:12:03

@jaen: I'm keeping tools.cli out of the core Pixie language for that reason.

jmglov08:12:25

As an external library, EPL is perfectly fine to call from LGPL code.

mpenet08:12:46

it's better also to avoid bloating the runtime

mpenet08:12:14

the "lightweightness" is one of the good parts of pixie atm

martinklepsch10:12:46

@pesterhazy: did you try using a profiling tool to see what the garbage is? I guess this could lead you to it's source originally, the garbage fountain.

pesterhazy10:12:38

garbage fountain? is that an expression? simple_smile

pesterhazy10:12:01

well, "profiling is hard, let's go shopping"

pesterhazy10:12:27

I upgraded the ec2 instances and tripled the jvm heap, that seems to have fixed the issues

martinklepsch10:12:01

> I upgraded the ec2 instances and tripled the jvm heap, that seems to have fixed the issues lol 😄 garbage fountain was made up. 😄

martinklepsch10:12:25

@pesterhazy: it's not that hard really simple_smile

pesterhazy10:12:39

It's decided then. From now on, the cause of GC issues shall be called the "garbage fountain".

joelkuiper12:12:10

naive question, with Hiccup markup would it be possible to generate HTML like “foo <span>bar</span> foo” or are you always locked to the “<span>foo</span><span>bar</span></span>foo</span>” structure?

joelkuiper12:12:27

I’m trying to get sort of XML flavored annotations of text

joelkuiper12:12:11

ah I guess it’s possible, it takes varargs

Chris O’Donnell12:12:54

@joelkuiper: that's right. If all your spans are sitting in a div, you could get your original html with [:div "foo" [:span "bar"] "foo"].

Petrus Theron14:12:35

Question for the database guys: What tools are Clojurians using to manage the shape and versioning of a large central Postgres database, e.g. for retail operations? My new junior hire is a Python guy and we have to make some tech decisions. Naturally, he would like to use SQLAlchemy because that's what he knows, but my experience with ORMs has been mixed. Rightfully he wants to reach for something familiar, but I would rather stick to Clojure. HoneySQL seems like the most sane query library, but it doesn't seem to be focused on migrations and a code first approach to maintaining the DB. Any advice? Appreciate it.

jaen14:12:42

For migrations you have a few choices - ragtime (migrations with plain sql), joplin (based on ragtime but extended to non-sql databases and gives you ability to write code-based migration which can be useful), lobos (which is probably the most similar to Rails migrations), migratus (which looks similar to ragtime) or you can use Java solutions - liquibase or flyway (there should be some wrappers for those around).

jaen14:12:53

I like joplin the most, I think.

roelof14:12:09

maybe a off-topic question : I wil use yesql for my project but now I have to learn sql. Can someone recommend a free online course to learn it good and fast ?

roelof14:12:43

jaen : thanks

borkdude15:12:09

I really like the feedback kibit is giving me, just caught an (= .. ..) instead of (is (= .. ..)) in my tests

borkdude15:12:39

inline in emacs, using squiggly-clojure

jaen15:12:43

Hm, curious if IntelliJ/Cursive can be extended with custom lints like that.

bostonaholic15:12:18

@borkdude: I didn't realize that kibit could catch something like that

borkdude15:12:48

@bostonaholic: apparently it has improved over the years

joelkuiper15:12:08

here’s an interesting interview type question 😛 given a sequence of pairs (e.g. [[1 2][4 10][5 10]] remove the overlapping pairs, (e.g. 4 10 in this case)

joelkuiper15:12:33

currently I’m sorting on the first element and then filtering statefully, but maybe there’s a more elegant solution

jaen15:12:19

ie. there is a list of pairs, possibly unordered and "overlapping" means here that union of ranges produced from those values is non-empty? Are the ranges open or closed? (I'd guess closed, so that [5 7] [7 8] would overlap, but unsure).

cddr15:12:48

Is there anything to gain from defining a protocol with the same methods as an existing java interface?

joelkuiper15:12:25

they’re closed indeed, the concrete use case are annotations of text, where we want to highlight the longest match only

Alex Miller (Clojure team)15:12:33

@cddr it can be extended to other types that don't implement the interface

cddr15:12:41

Oh that might be useful. Thanks

andy.fingerhut15:12:14

@borkdude: I'm not sure, but that may be the Eastwood linter giving you that warning, not kibit

andy.fingerhut15:12:36

Eastwood can definitely do that, and squiggly-clojure uses Eastwood by default, I believe.

borkdude15:12:19

you're right. I have both turned on

nha16:12:32

@joelkuiper: I actually had that use case with dates (ie. ranges of dates, want to know the full "occupied" slots list)

joelkuiper16:12:50

right that’s very similar!

borkdude16:12:32

@joelkuiper: overlapping on the first or the last?

wotbrew16:12:17

@joelkuiper: Not really practical to implement for an interview but an approach for large amounts of pairs would be a tree like an https://en.wikipedia.org/wiki/Interval_tree

borkdude16:12:21

(map first (vals (group-by last [[1 2][4 10][5 10]])))

joelkuiper16:12:48

both or either, [[5 10][4 9]] should return only [[5 10]], [[5 10][4 10]] should return [[4 10]], etc

joelkuiper16:12:06

yep interval tree is the “proper” solution I guess, but it’s as you say some work

joelkuiper16:12:15

and the library implementations are a bit lacking 😛

borkdude16:12:51

it would be a nice challenge for advent of code simple_smile

borkdude16:12:55

of 4clojure

joelkuiper16:12:13

it’s a fairly common problem with dates and text spans too

jaen16:12:37

I'd probably sort by range start, then do a sliding window with partition and fold over that either adding a new value if ranges are disjunct, or substituting the last one if they are not.

jaen16:12:45

But that's my first thought, not sure if that makes sense.

joelkuiper16:12:08

that might make sense simple_smile

joelkuiper16:12:10

I think 😛

borkdude16:12:30

(let [filter-dups (fn [f coll] (map first (vals (group-by f coll))))] (->> [[1 2][4 10][5 10][1 4]] (filter-dups first) (filter-dups second)))

borkdude16:12:41

but probably not so performant with very big datasets

nha16:12:30

one of my very first pieces of Clojure : https://gist.github.com/nha/f1474c905b38a7bf5466

roelof16:12:03

Anyone a example of the code how to add a user to the database with luminus. I have read the docs about it on the luminus page but it's still not clear to me

jaen16:12:41

Ugh, NPEs on comparisions with nils ; /

jaen16:12:45

(let [values [[1 2][4 10][5 10][1 4]]]
  (as-> values $
    (sort-by first $)
    (reduce (fn [[[last-start last-end] & rest :as acc]
                 [new-start new-end :as range]]
              (if (<= new-start last-end)
                (cons [last-start new-end] rest)
                (cons [new-start new-end] acc)))
            [(first $)] $)
    (reverse $)))

jaen16:12:03

If only I could avoid those $es.

nha16:12:51

Does something exists in Clojure to parse strings ? Something like : (parse "the app is live" "the app is %status") ;;=> {:status "live}

jr16:12:20

clojure.core/format

nha17:12:38

Ah sorry wasn't clear. I am looking for the exact opposite of that.

dm317:12:37

you're looking for something very strange. Doubt it exists in the form you want.

rantingbob17:12:34

Though you could probably write it yourself with a regexp

wamaral17:12:34

regex + core.logic maybe?

wamaral17:12:20

or core.unify even

roelof17:12:31

No one who has a example of code for adding a user with buddy. I try to make my first web site and this way its very frustating

Alex Miller (Clojure team)17:12:04

@nha you can do that with the text formatter/parsers in the jdk

nha17:12:29

@alexmiller: you mean like jsapar ?

Alex Miller (Clojure team)17:12:14

I'm sure there are other libs. I don't know anything about JsaPar

Alex Miller (Clojure team)17:12:43

there is also regex support which is fairly well integrated in Clojure

Alex Miller (Clojure team)17:12:52

and then of course various Clojure parsers too

nha17:12:35

I will dig a bit in this direction then, thanks simple_smile

danmidwood17:12:43

@joelkuiper: If the list is ordered by the first number in each pair then you can use reduce. If it isn't, then you can order it and use reduce simple_smile

michaeldrogalis17:12:42

Is it possible to use test.chuck's checking macro to refer to other generators? The goal is to use test.check's bind generator: https://github.com/gfredericks/test.chuck/blob/master/src/com/gfredericks/test/chuck/clojure_test.cljc#L97

roelof18:12:39

No buddy users here ?

roberto18:12:27

I don’t know. I use friend.

roelof18:12:46

oke, im looking if friend has better examples then buddy

roelof18:12:59

oke, I cannot find out if a new person register in my new app. How I store the data in the database. I can find the data the user has added but how do I take care that the password is not stored in plain text and how do I store in in the h2 database

jaen18:12:35

If you do not want to store a password in plain text - hash it.

jaen18:12:32

Really, buddy has really good documentation. I think friend is cryptic compared to that.

jaen18:12:42

Or I just suck at understading Clojure ; d

roberto18:12:52

I found buddy to be cryptic and friend with the better docs

roberto18:12:00

just a matter of perspectives, I guess

roelof18:12:42

Everything is a matter of perspectives

roelof18:12:27

jaen : oke, and how do I make a store function so yesql stores the data in a database

roelof18:12:46

I find the readme of yesql not very clear about that

roelof18:12:36

oke, I thought I need to write a save function myself

roelof18:12:50

but if I look at this , im wrong

jaen18:12:04

@roberto: I suppose, for me friend was just too hard to understand for some reason.

mbertheau18:12:48

I have a function that takes named arguments like this: (defn f [& {a :a b :b}] [a b]). I have a map {:a 1 :b 2}. How do I call f with the map so that the result is [1 2]?

roberto18:12:17

did you try (f :a 1 :b 2) ?

mbertheau18:12:40

Yeah, I want to use this map that I have, {:a 1 :b 2}.

jaen18:12:42

That's one of the downside of using kwargs; I never use them for that reason.

roberto18:12:59

yeah, I was a fan of those

mbertheau18:12:18

So you just use positional arguments?

roberto18:12:20

i don’t know if he wants to use them as kwargs tho,

roberto18:12:27

how do you want to call them>

jaen18:12:30

Look for apply-kw on the internet, it can do what you want.

mbertheau18:12:21

Hrmm, some batteries missing from clojure, it seems 😕 Thanks for the info!

mbertheau18:12:36

Do you know of alternatives for clojure "kwargs"?

jaen18:12:44

I just use maps.

mbertheau18:12:55

Hmm, I'll try that.

jaen18:12:24

Maybe kwargs looks slightly cleaner - two curly braces less, but handling them seems unwieldy to me.

roberto18:12:32

(defn f [& {:keys [a b]})] [a b]) ?? is that what you wanted? Then you can do (f :a 1 :b 2) ?

roberto18:12:49

I have an extra ) there

jaen18:12:50

I think what he means

jaen18:12:21

Is that he wants to be able to "apply" the map

jaen18:12:29

So he can generate the arguments somewhere

jaen18:12:33

And then call the kwarg function

jaen18:12:39

Hence my suggestion of apply-kw.

jaen18:12:45

But maybe I'm misunderstanding.

roberto18:12:53

oh, then it would be better to not use kwargs

mbertheau18:12:56

You're understanding correctly.

roberto18:12:13

you can do the destructuring inside a let within your function

roelof18:12:58

@jaen is the tutorial I posted a good one for buddy

jaen18:12:15

Not being able to apply kwarg functions easily is basically what turned me away from them.

jaen18:12:42

In Ruby you can do function(*array) or function(**hash)` to apply array and hash respectively.

roberto18:12:47

aphyr tweeted recently about staying away from kwargs also

jaen18:12:49

In Clojure this is just akward.

mbertheau18:12:10

Can I extract through destructuring just one value from the map and still get the rest or all of the original map?

jaen18:12:50

I'm not so sure if that was recently, since I've seen it a while back, but yeah, I've seen the tweet. He's a smart guy with all that Jepsen stuff and I kind of agree.

jaen18:12:08

@mbertheau: yeah, use :as something inside the map pattern

mbertheau18:12:28

Thanks. https://gist.github.com/john2x/e1dca953548bfdfb9844 seems to be a good cheat sheet on destructuring.

jaen18:12:04

@roelof: which one? The one by rundis? It's ok, but it uses JWT so you might be a bit confused by that if you want to do a website.

jaen18:12:30

@mbertheau: that looks handy, I usually google and end up here when I need to check something - http://blog.jayfields.com/2010/07/clojure-destructuring.html

roelof18:12:06

@jaen thanks . The kendru tutorials looks very good. When I do it from part 1 Everything is build up, Thanks

jaen19:12:19

I'd skip the vagrant part though, that's overkill for a learning exercise.

ghadi19:12:06

gfredericks: i didn't know emacs had a slack plugin

gfredericks19:12:57

B) I'm not using it just yet

gfredericks19:12:04

C) But I will definitely switch to that

gfredericks19:12:29

so once I do, you can send coded messages to each other that I can't decipher by using gifs

gfredericks19:12:53

emacs doesn't support slack directly, slack supports irc

michaeldrogalis20:12:21

@gfredericks: Heh, hello. Thanks for the advice simple_smile

cfleming20:12:33

@jaen: @borkdude: I’m planning to port Kibit to Cursive, yes

cfleming20:12:46

With intentions to apply the fixes

roelof20:12:52

When I have this in my code : ` (defqueries "sql/queries.sql" {:connection conn}) `

roelof20:12:40

how do the insert user looks like when I have a file in resources/sql which contains the insert sql command. I work with yesql

edvorg20:12:44

Anybody know what’s wrong with clojars? No responce from site, could not fetch deps also

edvorg20:12:13

Sorry works now

roelof21:12:37

Normally you would do something like this `(defn add-user [] (database (connection) "xxx")

roelof21:12:09

but on my code database is not defined only a [:connectoon conn] part

roelof21:12:17

so why is asked ?

rcanepa21:12:16

@roelof defqueries builds a function for every query it found on the sql/queries.sql file. Also, If you pass the connection parameter, the functions won’t need to be called with a connection argument every time. Every query in the file must have a name, which will be used to call the function that execute that query. If you have a SELECT query named select-item-by-id with one parameter named id, you must call that function like this: (select-item-by-id {:id id})

rcanepa21:12:44

And your SQL should be like this: SELECT * FROM items WHERE id = :id;

rcanepa21:12:26

Remember that you need to name your queries using a comment line that start with the text: — name: name_of_my_function.

roelof21:12:55

@rcanepa oke, so I can do like this ` (defn add-user[user-record] (insert-user(user-record] where users-record is a map with all the data entered in the form

rcanepa21:12:16

insert-user is your SQL query?

rcanepa21:12:34

In that case, yes, you should be able to do that.

rcanepa21:12:55

You have to make sure that your map contains all the parameters you specified in the SQL query.

profil23:12:12

Any ideas on how to reduce (heh) two transducers into one? I currently have one transducers in which the reducing function calls another transducer. It is this way because I have sequences in sequences which in each 'level' needs to be reduced.

profil23:12:03

How can I remove the extra intermediate data structures?

spei23:12:18

@profl: i think you might have to compose your reducing functions inside a single transducer call...? maybe a specific example will help