Fork me on GitHub
#beginners
<
2017-11-27
>
tdantas02:11:17

still working with clojure for weeks … I feel my code is like imperative/OO but with a brand new syntax can you guys provide some feedback to improve my code ? (usecase: user must activate the account to receive the api-key )

(defn find-activation-code [db user-id activation-code]
    (if-let [user (users/find-activation-code db {:id user-id :activation_code activation-code})]
      user
      :not-found))

(defn mark-user-as-active! [db user]
  (users/update-active-status db (:id user) {:active true}))

(defn activation [{:keys [components params body]}]   <----------------------------- MY WEB HANDLER ENTRYPOINT   (web/POST "activate/:id" request (r/activate request))
  (let  [db (:db components)
         user-id (:id params)
         activation-code (:actication_code body)
         user (find-activation-code db user-id activation-code)]
    (case user
      :not-found (r/not-found)
      (r/ok (:api_key
              (mark-user-as-active! db user))))))

jumar08:11:12

Not a big improvement but I'd say there's no need to use :not-found in this case - nil will do the thing

tdantas23:11:40

:thumbsup: thanks @U06BE1L6T ( sorry for my lack of confidence on clojure code .. all my code I feel like I’m not doing clojure )

tdantas02:11:08

On java probably I would use Optional on find-activation-code return type and on my activation I would map the optional ( chain )

tdantas02:11:54

how to increase my code above .. make it more clojure idiomatic

derpocious04:11:07

Hi all, I am having problems and just asked a question in the "planck" room. Would appreciate help. 🙂 thanks

derpocious12:11:35

Hey everyone, Is anyone about to load files with references to libraries from cljsjs from a REPL? Whenever I try it always says something like, "No such namespace: cljsjs.moment, could not locate cljsjs/moment.cljs, cljsjs/moment.cljc, or JavaScript source providing "cljsjs.moment"

sam19:11:44

Hello everyone. I've been dabbling in clojure for a couple years now, I haven't built anything serious due to lack of time, but I really enjoy the interactive development of a lisp and have been steadily improving. I just started at a company and joined a team that's building a new product for the company. We're getting close to releasing an MVP. It's mostly a Java shop using Spring and swear by DDD. I have noticed that this company, and my team in particular is quite open to new ideas and technologies. So, I casually brought up Clojure today with the Team Lead, and he was very open to it. He said that if I could show where in the system it could really improve something, we could write it up as a micro-service, and see where to go from there. 😲 Ok, so I need to identify where in our system clojure would shine vs current java approach. I know that clojure is very good at dealing with concurrency, but afaik we haven't had to deal with any concurrency, or haven't had any issues with it. My first thought was to find something rather complex in our system, and see if I can replace it with something elegant in clojure. Any thoughts? Any good articles that talk about what problems clojure is particularly good at solving? Also, Domain Driven Design is a fairly new concept to me. Is it something that fits well with clojure? atm I'm assuming that it is independent of the programming language. Thanks in advance!

drewverlee20:11:52

@sammy.castaneda90 For me Clojure’s biggest selling points is about the workflow experience, if you write pure functions and control your state, you end up with a system that is easy to interact with. I would highlight how clojure makes doing the right thing easy, as a bonus, explaining this will hopefully make everyone more aware of some different and helpful software design principles.

sam21:11:54

@drewverlee can you elaborate with what exactly you mean by makes doing the right thing easy?

drewverlee21:11:52

I feel like many good OOP patterns like those in the GOF book are built into Clojure. Also the SOLID principles of OOP are much simpler to achieve in clojure. I wrote about patterns here: https://drewverlee.github.io/tags-output/Design%20Pattern/ Though it was a very rough attempt. I would see Eric Normads take on SOLID in clojure

sam22:11:05

I'll take a look at those. Thanks for your insights Drew!

eggsyntax20:11:00

I agree 100% with @drewverlee’s point, but I suspect that in the case of a microservice, it's a less compelling argument, because it's already small enough that it's less likely to go off the rails. @sammy.castaneda90 I'd argue that the best thing to pick would be something that's most naturally expressed as a data transformation, hopefully a complex enough one that it'd be a PITA to do in Java. That's one of the places where Clojure really shines.

sam22:11:46

Thanks @U077BEWNQ I'll see if I can find something. I think we do some parsing of CSV files somewhere and process the data...

sam22:11:57

@U83JDR147 thanks for this. I really like seeing java implementation next to clojure one. Plus this is easy to read. I think I may pass this around.

drewverlee20:11:04

I’m also willing to bet, though i haven’t yet experienced it, that Clojure projects tend to evolve much easier then java projects, as despite arguments i don’t understand, i feel like Clojure projects are much more uniform then java ones.

seancorfield20:11:24

@sammy.castaneda90 We introduced Clojure at work (back in 2011) for a specific background process that continuously read data from a MySQL database, filtered and transformed it, and published it as XML via http to a search engine we use. That sort of continuous ETL process was a very good fit for Clojure.

sam20:11:51

@U04V70XH6 can you explain why you think Clojure was a good fit for this process?

seancorfield20:11:53

Filtering and transforming long sequences of data is a sweet spot for Clojure since the sequence is a core abstraction and so many core functions are designed to work on sequences.

sam20:11:48

ok, I will definitely keep that in mind. Thanks Sean

seancorfield20:11:24

We expanded from that one process to a code base that is currently around 56,000 lines (production code, and just over 19,000 lines of tests).

manutter5120:11:03

@sammy.castaneda90 I would recommend something smallish and self-contained. At $lastJob I built a simple web app for managing our localization strings. Basically a web front end to a db, as new strings are added to our main product, put them in the db, then once per release export to Excel to send to the translators. It was a good “hey here’s what Clojure can do” because if it failed we could always go back to the Confluence page we were using before, but if successful it really streamlined the round-trip from the code to the translators and back.

sam20:11:51

That's a neat idea. I'm assuming you made this in your "spare" time? I'm thinking that I will have to spend a lot of time WFH to get this done. Do you remember how long it took you to build this and how much code it ended up being?

manutter5121:11:56

It started out being a spare time thing, and then I mentioned it to my manager, and he liked it, and gave me a sprint or two to work on it. I got the bare-bones, minimum-viable-product in about 4 weeks calendar time, and then improved from there on a time-permitting basis. It was a great project for regression-testing sprints when we weren’t allowed to commit to the main product.

sam22:11:55

Thanks @U06CM8C3V. I'll see if I can find something small and self-contained to work on. But I would also like to show how we could use Clojure's ability to interop with the existing Java codebase. Maybe that's the next step :thinking_face:

manutter5113:11:49

For the Excel export/import I used docjure, which is a clojure wrapper around the Apache POI library.

donaldball20:11:07

Something well-defined and straightforward is a good call. You probably won’t wind up with a system architecture with which you’re happy on your first go.

gonewest81820:11:24

^^^ what he said. If my team were proposing this, I would suggest porting an existing microservice, and likely not the “gnarliest” one, and take it all the way to deployment in a dev environment (if not prod). Keep notes on the development workflow, review and critique the resulting code, observe if there were any complications in deployment, etc.

donaldball20:11:32

Even if you’re successful in demonstrating the utility of clojure

sam20:11:02

@donaldball that's definitely something I'm afraid of: Building something that doesn't seem extendable or maintainable, because I'm new to the language.

donaldball21:11:00

I guess I figure that’s not something to be afraid of, it’s just something to expect 🙂 But hopefully if you keep the scope small, you can evolve it into something you do like.

sam21:11:50

Alright, I'll look for something small, but also something that can show the effectiveness of Clojure. Thanks everyone! I'll definitely stop by again if I have any questions 🙂

ghadi22:11:38

I would specifically avoid concurrency on the smallish/self-contained thing that you choose @sammy.castaneda90

noisesmith22:11:09

having done maintenance on a “I hear clojure is good for concurrency, let’s learn clojure and concurrency” codebase, I heartily agree

admay23:11:53

@sammy.castaneda90 if you have an existing graphql service, converting it to Lacinia (or parts of it) could be a good start. Clojure boasts a great reloadable workflow and good scalability in practice. In my experience, any kind of service in an object oriented language gets to be pretty bloated pretty fast. In my experience, if you can make the life of a developer easier (i.e. less time waiting on builds and local deployments every day), then you can win them over eventually.

sam06:11:03

@U405WJUPL we don't have a graphql service, but I am curious about your second sentence. Can you elaborate on what you mean by reloadable workflow?

admay15:11:36

@sammy.castaneda90 I’m a bit busy right now but I don’t want to leave you hanging so I’m going to link some stuff for you until I can give you a real response! haha http://thinkrelevance.com/blog/2013/06/04/clojure-workflow-reloaded https://github.com/stuartsierra/component https://www.gamlor.info/wordpress/2016/08/code-reloading-in-clojure/ (Check out the rest server in this one)