Fork me on GitHub
#beginners
<
2016-02-04
>
amrit07:02:12

any one got ideas for building a clojure app for a newlearner coming from Ruby? [1:04] maybe suggest porting some existing Ruby gem?

samlinncon08:02:47

amrit what kind of project?

amrit08:02:50

@samlinncon: anything interesting and easy enough to start off with and iterate

samlinncon08:02:28

you are familiar with the syntax first?

samlinncon08:02:44

okay,you can check out luminusweb which is a framework for building web apps in clojure

amrit08:02:29

would prefer building some library

martinklepsch08:02:06

@amrit: Database clients, markdown parser, a custom datatype, ... :)

samlinncon08:02:15

that am not yt that good

martinklepsch08:02:46

@amrit: what kind of libraries have you built before?

samlinncon08:02:04

@martinklepsch: have you used luminusweb before?

samlinncon08:02:10

worked with sessions in the framework?i could use some help

martinklepsch09:02:18

@samlinncon: don't remember anything about that. I'd recommend to just ask, some people in here might know and be happy to help :)

udit10:02:32

@martinklepsch @samlinncon Would you guys have experience in session management using ring middleware?

martinklepsch13:02:34

@udit: as said earlier: you're most likely to get help if you state your problem and ideally provide some code to give context simple_smile

chadhs16:02:47

i’m looking for some ideas to solving a small problem: given a number of length 1 to 8 digits, return an 8 digit number filling in with leading zeros, then split it into two 4 digit pieces

chadhs16:02:43

(function 25467) => 0002 5467

chadhs16:02:43

making some progress here:

((fn prod-dir [prod-id]
   (loop [p (str prod-id)]
     (if (= 8 (count p))
       p
       (recur (cons 0 p)))))
 22747)

akiva16:02:44

(defn fx
  [n]
  (let [n' (str n)
        c (- 8 (count n'))
        cl (partition 4 (str (clojure.string/join (repeat c 0)) n'))]
    (str (-> cl first clojure.string/join) " " (-> cl second clojure.string/join))))

akiva17:02:10

Just a quick first pass.

jeff.engebretsen17:02:06

(partition 4 (format "%08d" 25467)) ((\0 \0 \0 \2) (\5 \4 \6 \7))

akiva17:02:28

I always forget about format.

jeff.engebretsen17:02:48

Then you have to do the string/join on each of those

akiva17:02:17

Yep. That gets rid of the need for (str n) and the call to count.

chadhs17:02:27

woohoo i did it but i should look at what you guy just posted too

chadhs17:02:31

(defn prod-dir [prod-id]
  (loop [p (str prod-id)]
    (if (= 8 (count p))
      (str (apply str (take 4 p)) "/" (apply str (take-last 4 p)))
      (recur (cons 0 p)))))

chadhs17:02:34

wow this is a lot shorter… (partition 4 (format "%08d" 25467))`

chadhs17:02:09

sadly i started with partition but failed to discover format

akiva17:02:22

(defn fx
  [n]
  (let [cl (partition 4 (format "%08d" n))]
    (str (-> cl first clojure.string/join) " " (-> cl second clojure.string/join))))

akiva17:02:26

Teamwork!

akiva17:02:03

(fx 25467) => “0002 5467”

chadhs17:02:07

my solution probably stinks, but it still felt good to glue things together and make it work.

akiva17:02:57

The more you use Clojure, the more you’ll find yourself composing functions rather than writing an iterative approach.

chadhs17:02:44

that’s why im practicing

chadhs17:02:58

trying to learn to think in that way

akiva17:02:36

Right on. Well, never hesitate to ask questions.

chadhs17:02:47

do you use the thread first macro a lot akiva?

akiva17:02:42

When it lends itself to clarity, yeah, so if I’m chaining two or more functions, I usually use it.

akiva17:02:19

And I use it a lot in the REPL because it’s easy to just CTRL-up and add another function to see.

chadhs17:02:08

i’ll have to experiment with that. i do find things reasonably readable without it, but my second pass at learning to code i was talked into common lisp (a gentle guide to symbolic computation) — perhaps i’m used to reading inside out

chadhs17:02:23

oooh good use case there in repl

timaeus17:02:15

hi all simple_smile (sorry for bashing into the convo @akiva & @chadhs).. i’m hitting a Unable to resolve var: routes issue while following this manual https://devcenter.heroku.com/articles/clojure-web-application.. Does this point to a particular beginners mistake I’m unaware of?

timaeus17:02:59

just after ‘web bindings with Compojure'

akiva17:02:24

You need to make sure (defroutes routes …) is set.

akiva17:02:38

Want to paste your code for that section?

jeff.engebretsen17:02:21

Clojure doest't hoist so it'll need to come first in the file

jeff.engebretsen17:02:25

Before it's used.

chadhs17:02:33

a bit of a tangent but… @timaeus: if you want to know how to push that to a stand alone server and not just heroku i did a quick write up for digitalocean: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-clojure-web-application-on-ubuntu-14-04

chadhs17:02:39

there’s a freebsd version too

akiva17:02:49

There is a declare command that allows you to refer to a var that hasn’t yet been defined.

akiva17:02:19

So if you really want routes at the bottom, you can (declare routes) above -main.

akiva17:02:02

It’s not widely used, though. I like having functions in alphabetical order so I use it personally.

timaeus17:02:25

@jeff.engebretsen: @akiva thanks, I’m not really set on putting the routes at the bottom but it seems that wherever I put the defroutes i can’t start the server

jeff.engebretsen17:02:31

What's the idiomatic thing to do? I'm coming from Java and I really like having the method definition appear after first use.

jeff.engebretsen17:02:11

It didn't work with defroutes before -main?

timaeus17:02:45

doesn’t seem so

akiva17:02:46

Jeff, I don’t see declare used a lot in the wild. Seems most people put the actual definition right before its first call from another function.

timaeus17:02:56

Unable to resolve symbol: index

jeff.engebretsen17:02:37

Same issue. You need index then defroues then -main

jeff.engebretsen17:02:49

Since routes references index

timaeus17:02:02

okey yes it works

timaeus17:02:28

I wasn’t aware of the strict ordering

jeff.engebretsen17:02:58

I guess I'll just get used to the Clojure way.

chadhs18:02:25

does order you define functions in in a file matter?

chadhs18:02:34

like if fn2 uses fn1

chadhs18:02:40

fn1 should be def’d first

chadhs18:02:43

is that correct?

akiva18:02:47

Unless you use declare then you can do

(declare f1)
(defn f2
[]
(f1 …))

(defn f1
[]
…)

Kamuela18:02:42

How do datomic and Clojure tend to work together if you're not ready to make your front end stack Om or Om Next?

akiva19:02:54

I’d be surprised if Datomic cares about anything beyond receiving and sending data.

akiva19:02:08

Whether it’s through a back-end API or something like Om.

akiva19:02:37

(I’ve not used it, mind you; I just doubt the idea of it being tightly coupled to a particular front-end library.)

jmayaalv19:02:23

i’m trying to add a result processor for yesql (and luminus) like this:

(db/get-tenant {:tenantid tenantid} {:result-set-fn first})
I’m getting the following error when i execute the query: IllegalArgumentException db-spec {:result-set-fn #object[clojure.core$first__4339 0x7eee991b "clojure.core$first__4339@7eee991b"]} is missing a required parameter clojure.java.jdbc/get-connection (jdbc.clj:292) The query works just fine without the processor. Anybody can point my on the right direction?

Kamuela22:02:01

@akiva: I agree I'm just not sure about interfacing with it I guess is my major question

jeff.engebretsen22:02:58

@kamuela Datomic will take/give plain old Clojure data structures.

jmayaalv22:02:51

Found my problem. In case somebody finds it useful it turns out that conman (used by luminus) adds a wrapper to yesql. https://github.com/luminus-framework/conman/commit/73098c925b61971b41df0ebe41f7fc612acea4eb this is how i am using

(db/get-tenant {:tenantid tenantid} @db/*db* {:result-set-fn first})