Fork me on GitHub
#clojure
<
2015-12-07
>
maxt10:12:43

clojure-csv or clojure.data.csv ?

tragiclifestories11:12:06

nice, reminds me of the Python csv lib, which rather spoiled me for others

hoopes12:12:10

hi all - i was looking for a simple example of a udp server - i found a gist (that appears outdated) using aleph, but not much else - am i missing any obvious libraries? (coming from a python background). Thanks for any notes/help/etc!

hoopes12:12:16

and uh - this is my first time in a slack channel, so if there's an accepted way to ask questions, please feel free to correct my style....

maxt12:12:13

@dm3: thanks, yes semantic-csv looks nice, but it still needs a lib for parsing if I understood correctly

maxt12:12:35

clojure.data.csv seems to be most recent

pupeno12:12:49

For my Prerenderer library I want to have some programs that exercise it, to help test/develop it. These would be almost full Leineingen projects. Is there any conventions where to put them? I thinking /example. And is there a way to have leineingen pick the Prerenderer source code instead of the installed one? the checkout trick won’t work as github can’t keep track of symlinks.

rcanepa14:12:40

Anyone is using duct?. I am trying to figure out how to pass the database record to the handler component. Actually, I am just passing the database URI, but I would like to pass the DB record in order to facilitate unit testing. The instantiation of the database records happens inside the same map (system-map) that I use to instantiate the handler component. Should I move the DB record instantiation outside (maybe inside the let bindings)?

rcanepa14:12:23

I put comments on the lines where I create the DB record and the handler component.

rcanepa14:12:56

I want to do this in order to mock up the database for my unit tests.

jstew14:12:10

rcanepa: If you just want to add a mock database record, can't you create the system, then just assoc the mock db record into it before calling start on the system (when testing)?

jstew14:12:45

That way you don't have to muck around with the system while you're building it just to account for a mock.

rcanepa14:12:55

@jstew: Yeah, that sounds much more easy. Thanks for the input!

maxt15:12:21

A parsing question: I have a sequence 1ab2cde3f4ghij which I would like to turn into a map {1 (ab) 2 (cde) 3 (f) 4 (ghij)}. I written one version with recur, which got ugly, and another version with partition-by which I'm not entierly happy with either

(let [partitioned (partition-by (set "1234") "1ab2cde3f4ghij")
ks (map first (take-nth 2 partitioned))
vs (take-nth 2 (rest partitioned))]
(zipmap ks vs))

Other suggestions?

maxt15:12:26

In words, I would like each letter to be classified by the digit preceding it.

andreaslundahl15:12:52

@maxt Are you only dealing with strings? Maybe (re-seq #"(\d)([a-z]+)" "1ab2cde3f4ghij") would produce more convenient partitions, to create a map from?

maxt15:12:15

@andreaslundahl: Right, intresting. No I want to use it on any sequences. But your idea does suggest that it's a finite state machine im looking for

jstew15:12:17

(group-by #(nth % 1) (re-seq #"(\d)([a-z]+)" "1ab2cde3f4ghij")) got me close-ish. Not really a better solution than what you posted though.

I need to save various media (images, video) to a file system. Should I use Java interop, or is there a library that handles this for me?

roelof16:12:38

Anyone who knows a good tutorial about making a website with login , authorisation and so on with clojure

roelof16:12:02

I like to not using things like luminus

@roelof I don't know any full tutorials, but Friend is a good place to start.

jaen16:12:09

IMO Friend is too complicated

jaen16:12:32

I suggest buddy instead - https://github.com/funcool/buddy/ - it's considerably simpler

jaen16:12:06

Here are some nice blogposts on how to use it - http://rundis.github.io/blog/tags/buddy.html

jaen16:12:49

The poster uses it to secure a REST API, but securing a website should be analogous

sarcilav16:12:50

I also like/recommend buddy, @roelof here are some examples from https://github.com/funcool/buddy-auth/tree/master/examples

roelof16:12:28

oke, and are there any good tutorials about routes, template engines and so on

jaen16:12:57

Oh, that's probably somewhat better for starters, since it has cookie based authentication example.

jaen16:12:30

For routes and templating you don't really need more than the documentation of libraries, that's pretty simple.

jaen16:12:50

If you want to template in Clojure then hiccup could be a nice templating language

roelof16:12:10

oke, so I do not have to buy a book like web Essentials or web development with clojure ?

jaen16:12:46

If you want something more classic then something like mustache - https://github.com/davidsantiago/stencil - or selmer - https://github.com/yogthos/Selmer - could work

jaen16:12:41

@roelof: I didn't need something like this, but then again I've been doing Rails for four years already.

jaen16:12:53

For you it might be helpful if you didn't do webdev.

jaen16:12:15

Also, why not Luminus? To me it seems like a good introduction to Clojure way of building webapps.

jaen16:12:34

I wouldn't make all of the same choices, but it's a good overview of what you need.

roelof16:12:50

oke, so Luminus is not the same as Rails

roelof16:12:10

I did not like that because it dicating me how I must do something

jaen16:12:12

Yeah, it's not

jaen16:12:17

It's more like a micro framework

jaen16:12:35

If you don't like something Luminus set up for you, you can change it

jaen16:12:48

It's not far off to call it an application template.

jaen16:12:02

I just think it's fairly representative of how most simple Clojure webdev projects would look.

roelof16:12:30

@jaen : oke, then I will take a second look at luminus

jaen16:12:31

There's also duct, but it uses components, that might be somewhat confusing for a newcomer.

jaen16:12:22

@roelof: Luminus also has a book, but it's a bit outdated and a new edition is supposed to come soon-ish.

roelof16:12:40

oke, then I will concentrate on 4clojure at this moment and try to do all the "easy" challenges

eraserhd16:12:59

Has anybody used JNA with Clojure (or even Java)? Is it a good thing?

bigkahuna16:12:11

@jaen: I think duct may have a slightly steeper learning curve but it's definitely worth the effort especially when deciding the best place to hold state. I went to Clojure X and saw a great overview/rationale by the author: https://skillsmatter.com/skillscasts/7229-duct-covered and here's another tutorial which covers all the basics very well by the author of Luminus : http://yogthos.net/posts/2015-10-01-Compojure-API.html

jaen16:12:08

@bigkahuna: yeah, hence my caveat "for a newcomer". I'm using components myself (though not duct).

roelof16:12:55

and im a newcomer, I try to learn clojure for some 3 - 4 weeks

bigkahuna16:12:04

@jaen: Yep, I agree might be information overload for a newcomer

bigkahuna16:12:57

@roelof: Working through 4clojure is a very useful exercise as you'll internalize how Clojure works. I've been attended a few hackadays as part of the LJC and found the following pages useful: http://www.practical.li/ The author John Stevenson also has a github page with a project that can be loaded into a repl or Lightable that outlines Clojure's features. You can experiment with them to further your understanding: https://github.com/practicalli/clojure-through-code/tree/master/src/clojure_through_code I've also found Living Clojure by Carin Meier an excellent book for newbies. It guides you through the language at just the right pace: http://shop.oreilly.com/product/0636920034292.do

roelof17:12:39

@bigkahuna: thanks for the tips

roelof17:12:44

anyone who knows the web essiantials book where I can learn luminus

jaen17:12:59

There's also an older one by the author of the framework - https://pragprog.com/book/dswdcloj/web-development-with-clojure

jaen17:12:07

But I didn't read either so not sure how good they are.

jaen17:12:20

Though probably the first one would be better since it's newer

roelof17:12:14

I mean that book

roelof17:12:33

I saw it it newer , It's from begin this year

bigkahuna17:12:54

https://pragprog.com/book/dswdcloj2/web-development-with-clojure-second-edition is out next year. First book is still worth reading, just that the libraries used are a tad out of date.

roelof17:12:23

Can I refractor this code so it uses reduce

roelof17:12:35

(defn my-gcd
[number1 number2]
(loop
[n1 number1
n2 number2]
(if (zero? (rem n1 n2))
n2
(recur n1 (rem n1 n2))))) 

jeff.terrell18:12:56

Probably. I think there's a way to short-circuit reduce so that at some point you can skip processing the rest of the items. But I'd consider that an anti-pattern and a sign that you should be using loop / recur, so I'd say stick with what you have.

jeff.terrell18:12:43

reduce makes the most sense when you want to process every item in a sequence.

roelof18:12:01

oke, I thought as a beginner that I can better use reduce or map then use loop

jeff.terrell18:12:52

Yeah, it's better to use those when they make sense. But to me, this looks like a good use case for loop. Neither map nor reduce are well-shaped tools for this problem.

roelof18:12:46

oke,thanks

roelof18:12:05

chips, code is somewhere wrong (my-gcd gives as output 5 where it suppose to be 1

roelof18:12:30

maybe try to print both numbers to see where it went wrong

roberto18:12:00

roberto18:12:47

you have them in reverse order

roberto18:12:00

if you enter 7 5 you get the answer you are looking for

roberto18:12:20

which would indicate that you are operating on them in reverse order

roelof18:12:00

oke, but on 2 4 , 10 5 and 1023 858 it works well

roelof18:12:28

so it seems that then I do not matter if the first number bigger is then the second

roberto18:12:30

why would you think that is?

roberto18:12:44

roberto18:12:00

in rem it does matter which one is the first and second argument

roberto18:12:34

(rem 2 4) is not equal to (rem 4 2)

roberto18:12:50

similarly (rem 5 7) is not equal to (rem 7 5)

roelof18:12:32

wait, can it be that on this case both numbers are not even

roelof18:12:10

in all cases that work 1 or 2 of the numbers are even. 2 4 in the first, 10 in the second and 858 in the last one

roelof18:12:23

7 and 5 are both not even

roelof18:12:52

I have to think well how I can make that check in the code I have now

roelof18:12:24

@roberto any tips how I can swap the two values if both are odd ?

roelof18:12:13

it is now a "big" problem that variables are immutable

roberto18:12:03

not really

roberto18:12:16

so, look at your recur portion

roberto18:12:38

try switching the order of the arguments, and see what you get

roelof18:12:41

not good. 1023 858 gives now the wrong answer 165 instead of 33

roelof18:12:06

I do now this ' (recur (rem n1 n2) n1 )) 

roberto18:12:18

ah, I see your algo is wrong

roberto18:12:30

so, you don’t want to check that (rem x y ) is zero

roberto18:12:04

if you swap the args, when you do (my-gcd num1 num2)

roberto18:12:14

you want to check that num2 is zero

roelof18:12:03

???? according to the Wikipedia page I have to check if the remainder is zero

roelof18:12:07

maybe I have to use another algoritm

roberto18:12:37

it works for me, make sure what whatever algo you are looking at on wikipedia, is using recursion.

roberto18:12:17

#'user/my-gcd
user=> (my-gcd 5 7)
1
user=> (my-gcd 1023 858)
33
user=>


roberto18:12:38

i can’t show you the definition of my-gcd without giving away the answer

roelof18:12:25

I tried to use the euclids solution on this page : https://en.wikipedia.org/wiki/Greatest_common_divisor

roelof18:12:40

but I think I maybe misunderstood it

sarcilav18:12:40

@roelof your solution works you are just forgetting something that is in the definition

\gcd(a,b) = \gcd(a - b,b)\quad, if a > b
\gcd(a,b) = \gcd(a, b-a)\quad, if b > a

roelof18:12:29

@sarcilav: so I have to make another if then

sarcilav18:12:46

that should work

roelof18:12:17

oke, thanks

roberto18:12:02

you solved it?

roelof18:12:30

yep, your tip was right I had to check if the second number was zero

roberto18:12:51

this is my solution:

roberto18:12:57

(defn my-gcd [n1 n2] (loop [a n1 b n2] (if (zero? b) a (recur b (rem a b)))))


roelof18:12:58

and first a number and then the calculation of the remainder

roelof18:12:32

I have now the same but I use other names

roelof18:12:48

(defn my-gcd
[number1 number2]
(loop
[n1 number1
n2 number2]
(if (zero? n2)
n1
(recur n2 (mod n1 n2) )))) 

roberto18:12:20

okay, I’m heading home. The chair in this coffee shop is too hard.

roelof18:12:49

I can image. Drive safe

ustunozgur19:12:19

Hacker Dict Our ClojureCup hackathon entry is a dictionary for programmers by programmers. <Http://hacker-dict.com> We believe the programming community needs such a tool for discussing and gathering chronological data about programming. Basic functionality is there, we will improve upon it once the judging period is over. We used Clojure, ClojureScript and Datomic for the project Please sign up and share: http://hacker-dict.com

jeff.terrell19:12:11

Nice work. One thing you probably already noticed: the browser back button doesn't work.

ustunozgur19:12:47

@jeff.terrell: thank you :) yes aim was to use a client side library but didn't have time to learn so it just updates the Url manually via push state

ustunozgur19:12:06

We will implement properly once they allow us to commit more

ustunozgur19:12:40

This is interesting for Clojure community because most stuff on the net is mutable

ustunozgur19:12:47

Wikipedia is mutable

ustunozgur19:12:00

ustunozgur19:12:02

We aim to create a medium where people ca. Share facts and opinions about various aspects of programming

jeff.terrell19:12:18

Yeah, that's a pretty interesting concept. What data should be mutable vs. immutable?

ustunozgur19:12:31

And others can learn chronologically

ustunozgur19:12:47

So that one can learn historical data and trends

ustunozgur19:12:54

And what is what and who is who

ustunozgur19:12:18

We also want to be able to share subjective facts not objective facts :)

ustunozgur19:12:18

You can also link with []

ustunozgur19:12:24

If it contains spaces

ustunozgur19:12:36

[rich hickey]

dmitrig0120:12:55

in some sense, a wikipedia article is a mutable pointer to its most recent revision, and the revisions are immutable

jeff.terrell20:12:13

Much like, in git, the objects (e.g. commits) are immutable but the references (e.g. branches) are pointers that point to different immutable objects at different times. Datomic has a similar model. The stored data is immutable, but an entity's attribute can take on different values at different times.

dmitrig0120:12:36

yep. or clojure atoms

lumengxi21:12:19

wonder anyone used https://github.com/Factual/drake in production? is this still under active development?

roelof21:12:46

When I do lein ring server on a blank luminus project then I see this error message : 'ring' is not a task. See 'lein help'.

roelof21:12:08

I see that ring is mentioned on the dependencies line

roelof21:12:19

so anyone a idea what is wrong ?

johnjtran21:12:38

@roelof most likely this newer and you'll need lein run instead.

roelof21:12:23

?? you mean I have to use lein run instead of lein ring server

borkdude21:12:25

what's wrong with this expression: (aset (make-array Integer 5 5) 0 0 1)

roelof21:12:54

I have this on a book written in febr. 2015 and now a error in the book ?

borkdude21:12:43

nm, this works: (aset (make-array Long/TYPE 5 5) 0 0 1)

johnjtran21:12:47

@roelof: yes, try it lein run when you pull stuff with lein new luminus project it's actually not reflective of the book instead it pulls the latest. In all likelihood this stuff is evolving very rapidly so what was good then is probably dated now.

roelof21:12:22

@johnjtran: thanks, it worked. When I open my browser I see the startup page

borkdude21:12:15

@roelof: when in doubt, you can check http://www.luminusweb.net/docs also

roelof21:12:57

@borkdude: thanks

johnjtran22:12:09

I have a love/hate relationship with luminus. By all accounts, it appears to be very good -- but multiple layers and lots of wrappers etc. I don't advocate that you pick one over another, but take a look at pedestal` as an option. Less bells and whistles but appears to be slightly less aggressive with their updates -- thus stable.

bronsa22:12:55

@borkdude: integer literals in clojure default to long

bronsa22:12:05

you're trying to shove a Long inside an Integer[][]

borkdude22:12:15

@bronsa: yeah, that was it

hoopes23:12:08

hi, i’m trying to go from byte array to map with some spec - googling brought me to buffy and octet, but i just found gloss . is gloss my best bet for byte array to map of data?

hoopes23:12:21

(if this isn’t the right question for beginner questions, please let me know - thanks!)

hoopes23:12:28

right channel, i mean

hoopes23:12:09

http://construct.readthedocs.org/en/latest/ is the python version of what i'm trying to find

hoopes23:12:11

that looks really similar to gloss, down to the -le and -be stuff - 6 of one, half dozen the other? is there a preferred lib? it’s a little weird coming from python, where there’s usually one best way to do something

hoopes23:12:33

paralysis of analysis...