Fork me on GitHub
#beginners
<
2017-12-28
>
jgh02:12:11

is there a way of reducing an array of strings to basically just parameters? E.g. raynes/conch is for executing shell commands and it takes the command line arguments as a variadic list of parameters…sigh….

jgh02:12:01

apply is similar to reduce though isnt it, in that it runs the function on each parameter?

noisesmith02:12:28

many functions implement varargs by using reduce, that's where the similarity ends

noisesmith02:12:03

+user=> (apply str "a" "b" ["c" "d" "e"])
"abcde"
+user=> (apply get [{:a 0} :a])
0

noisesmith02:12:52

also ymmv but I found ProcessBuilder and Process to be more flexible and easier to use for advanced purposes compared to conch

noisesmith02:12:08

that is, the built in java classes that conch is built on

noisesmith02:12:08

for example I never figured out how to get real-time interactive IO with conch (send string to a process, read result, send another string in reply, etc.), it is straightforward with ProcessBuilder and Process classes

jgh02:12:26

Ok, I’ll look into that if necessary. I’m mostly just messing around right now. Ok looks like apply works for the simple case, let’s see if I can get it working with programmatically adding params.

jgh02:12:54

ah great looks like that works

noisesmith02:12:56

also you don't need to put args into a collection artificially - you can give your initial args to apply normally and put the rest in a collection

noisesmith02:12:51

eg. instead of (apply str ["a" "b" "c" "d" "e"]) above I was able to use (apply str "a" "b" ["c" "d" "e"]) (let's pretend c d e came from some place interesting)

jgh02:12:14

can you do (apply str [“a” “b” “c”] “d” “e”)?

noisesmith02:12:32

no, the collection has to come last - you'd end up with [ ] in your string

noisesmith02:12:57

(it is "smart" enough to coerce "e" into the collection [\e] though... so that case kind of works, accidentally)

noisesmith02:12:12

as opposed to eg. (apply + [1] 2) which just blows up

noisesmith02:12:12

though (apply + [1 2]), (apply + 1 [2]) are both fine

noisesmith02:12:33

+user=> (apply + 6 (range 9))
42
- more typical usage of apply

jgh02:12:08

cool, that’s helpful. Thanks.

amit588108:12:49

i have (("9" "2" "5") ("1" "2" "3")) and i want [{:first "9" :second "2" :third "5"} {:first "1" :second "2" :third "3"}]

curlyfry09:12:40

What have you tried so far?

rcustodio19:12:00

Hi… about (put! chan .... and (go-loop [message (<! chan)] …. is this gonna process 1 message at time? isnt async lib threads?

rcustodio19:12:30

I’ve started that channel with (chan 100)

noisesmith19:12:06

all operations in go loops are done in a thread pool, but there’s no automatic parallelism - if you want two workers, start two go loops

noisesmith19:12:51

or, in your case, maybe you want to start 100 go loops

noisesmith19:12:10

also, don’t do blocking operations inside go blocks - especially with that kind of count you can easily starve and lock up the whole thing

rcustodio19:12:09

Oh… so if I want a Thread pool for example, its better using java thread?

rcustodio19:12:57

this Executors/newCachedThreadPool?

rcustodio19:12:12

If I want parallel

noisesmith20:12:16

core.async provides a thread macro that returns a channel

noisesmith20:12:29

depends if you need async coordination, or just parallelism

noisesmith20:12:52

for parallelism thread pools suffice - you can check out a nice lib called claypoole if you want a more clojure-like API

clojuregeek21:12:52

Hi, I'm trying to connect to a pg database and insert a row

user=> (require '[clojure.java.jdbc :as sql])
nil
user=> dbspec
{:dbtype "postgresql", :dbname "recipes", :host "127.0.0.1", :user "api", :password "api"}
user=> (sql/insert! dbspec :recipes {:name "oatmeal" :url ""}) (sql/insert! "" :recipes {:name "oatmeal" :url ""})

PSQLException This ResultSet is closed.  org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkClosed (AbstractJdbc2ResultSet.java:2852)

jgh21:12:56

You’re using the wrong postgres dependency

clojuregeek21:12:02

from what i read, it has something to do with the connection being close, i was following along with https://devcenter.heroku.com/articles/clojure-web-application ... last updated 2017

jgh21:12:09

(i assume you’re using the one off clojars)

jgh21:12:38

it is very out of date and doesn’t work for inserts

jgh21:12:46

(i ran into that one last week)

clojuregeek21:12:01

doh... i'm using [postgresql "9.3-1102.jdbc41"]

jgh21:12:05

use the one off maven central

jgh21:12:31

[org.postgresql/postgresql "42.1.4"]

jgh21:12:46

that will solve your issue (or at least get you a more useful error) 🙂

clojuregeek21:12:49

yep that works

clojuregeek21:12:59

thanks i thought i was going nuts 🙂

jgh21:12:18

ha no worries, this has been a bit of a discussion in #leiningen

clojuregeek21:12:26

@jgh .. I see the tutorial i was looking at used the right library, i just always go to clojars to grab latest 😊

jgh21:12:10

ah yeah looking at it it just uses an old version.

seancorfield21:12:30

Where is the tutorial that uses the wrong PostgreSQL artifact?

seancorfield21:12:05

I looked at that Heroku one and it has [org.postgresql/postgresql "9.4-1201-jdbc41"] -- which is the correct group ID (not the one on Clojars!).

seancorfield21:12:59

Since this has bitten two people recently, it seems like it's worth trying to get the tutorials updated (that are misleading folks).

jgh21:12:12

well the heroku tutorial uses the wrong version, i see an old stackoverflow question that references it (and that I have clicked on, so perhaps that’s where I got it from) .. I’ve submitted an edit to fix it. There’s also a tutorial from 2016: http://enyert.github.io/rest-api-clojure-tutorial/

jgh21:12:40

but i suspect it’s more like read tutorial -> want updated deps -> go to clojars instead of maven central

seancorfield21:12:42

The Heroku tutorial uses an older driver, true, but it is at least the official one.

jgh21:12:54

and then searching for postgresql gets you an official-looking postgres repo