Fork me on GitHub
#beginners
<
2017-06-13
>
eslachance00:06:16

I mean. I don't want to use a high-level framework like luminus, I really just need straight-up 1 post and 1 get - there should be a way to do that without going into 50 different files, no?

noisesmith00:06:17

you could use raw lein new, but then you would need to edit project.clj to add all the right deps for ring

noisesmith00:06:30

@eslachance then don't use luminus, us compojure

noisesmith00:06:50

lein just gives you clojure by default, ring doesn't come with clojure, no web server comes with clojure

eslachance00:06:13

yeah but I could have just added the dependencies to project.clj, which I did of course

noisesmith00:06:22

well you did it wrong

eslachance00:06:51

Probably. Let me try this again, in case I really was that dumb, there.

eslachance00:06:48

facepalm apparently, I was, both times. Welp.

eslachance00:06:05

I swear I'm not an idiot, I just play one on TV

noisesmith00:06:24

the proliferation of nested dependencies is an issue with clojure (and with the java ecosystem in general), but our dep management is actually pretty good once you get to know it

eslachance00:06:27

I actually have worked with clojure enough that this was just a derpy moment. TBH I'm just slowly getting back into it after 9 months of not touching it. https://github.com/eslachance/dithcord is proof that I'm usually not that empty-headed.

victora01:06:05

Hello guys! What is the easiest way to build a RESTful application with clojure? Or best API

seancorfield02:06:07

Check out compojure-api as a starting point @victora

seancorfield02:06:47

If you want something simpler, with less magic, and you don’t mind rolling it yourself, just basic Compojure could get you going (and you’ll need to understand Ring and Compojure somewhat to use compojure-api anyway).

victora02:06:24

I started a lein project with a compojure template and it was just what I needed. Simple also!

victora02:06:07

I have another task, though. I need to build a simple database myself. (Not use any API). To be fair, it's not even a database, but a map from int's to lists. Very simple. How can I accomplish state in clojure, though?

noisesmith02:06:29

an in-memory store? I'd use an atom

victora02:06:54

What's an atom?

noisesmith02:06:33

a thread safe mutable data storage for immutable data

noisesmith02:06:54

you can update it via swap!

victora02:06:18

So basically I'll have an atom with an empty map, and whenever a request occurs, I'll do something with it and swap

victora02:06:30

it starts with one*

victora02:06:33

is what I meant

noisesmith02:06:41

pretty much, yeah, but the ! is part of the name, swap!

victora02:06:07

Thank you! That worked

victora02:06:10

Should be enough

eslachance05:06:33

Need a tiny bit of help, probably just a very quick spoonfeding... I just need to open an H2 database inside my file, and there isn't a flurry of examples online. Luminous uses conman, and that's the closest I've been to actually finding code that uses h2 in clojure. That is unless anyone else has an idea of just... a very simple persistent string-based key-value system that doesn't require any setup, that'd be super awesome. (I used LevelUp/LevelDown with node if that's any indication)

eslachance05:06:05

as low-level as possible, no server, no auth, no queries. Just set/get, is all I need.

curlyfry06:06:05

@eslachance Try connecting with https://github.com/clojure/java.jdbc and don't forget to add the H2 driver to your project.clj

eslachance06:06:12

Actually, that looks like a good resource! Thank you 😄

seancorfield06:06:18

@eslachance Happy to answer any of your Qs around java.jdbc as I maintain it. It's fully tested against H2.

eslachance06:06:48

I'll see if I can figure it out first, this is a seemingly very easy project made difficult by my necessity to learn new things 😉

eslachance06:06:53

But I'll keep that in mind 😄

joshkh14:06:19

i've defined a lein alias to seed a database, however when i call it via lein run seed it instead just runs (-main) , and the function i'm trying to call doesn't run at all. any ideas?

aliases {"seed" ["run" "-m" "my.seed/setup"]}

4n5u16:06:15

I'm trying to create a list of specific Java objects

(map #(.setTag (new Tag) %) tags)

4n5u16:06:59

but for some reasons I get exceptions like No matching ctor found for class com.amazonaws.services.s3.model.PartETag

4n5u16:06:18

or No matching field found: setTag for class java.lang.String

4n5u16:06:21

any tips?

timgilbert17:06:49

@joshkh: I think the way aliases work is that you define the namespace they start with, but they will always call a function (-main) inside that namespace

timgilbert17:06:31

So you'd want something more like aliases {"seed" ["run" "-m" "my.seed"]} and then rename your setup function to -main

timgilbert17:06:48

...just keep it distinct from the namespace you specify in the :main key of your project.clj, which has the version of (-main) that gets run if you just do lein run by itself

joshkh17:06:16

hmm okay i'll give it a shot. any idea why it's running the myapp.core/main regardless?

timgilbert17:06:42

Not really sure, I'd expect an error

timgilbert17:06:55

@4n5u: your syntax for the constructor there looks incorrect to me, instead of (new Tag) you'd want (Tag.), so more like (map #(.setTag (Tag.) %) tags)

noisesmith17:06:34

new is valid, but not recommended - (Tag.) actually expands to (new Tag)

joshkh17:06:09

@timgilbert thanks for the advice but no luck. renamed setup to -main, changed the alias, but lein run seed still just runs core/main and the seed main is never called

joshkh17:06:17

am i running aliases the right way?

timgilbert17:06:43

Oh! You want just lein seed, not lein run seed

joshkh17:06:44

lein run banana seems to do the same thing

joshkh17:06:23

lein seed
'seed' is not a task. See 'lein help'.

Did you mean this?
         new
         repl
         deps
         less
         help
         test

joshkh17:06:32

i can't win!

joshkh17:06:52

oh wait there we go

joshkh17:06:10

that did the trick. thanks a lot, @timgilbert !

timgilbert17:06:21

Sweet, glad to hear it

timgilbert17:06:15

@4n5u - based on your error and @noisesmith's correction, I think the problem is that you are trying to call a no-arg constructor that doesn't exist for your class

timgilbert17:06:01

You'd need something like (map #(Tag. partNumber %) tags)

timgilbert17:06:13

Not sure where you get partNumber though

noisesmith17:06:36

use map-indexed, they should be in order

victora17:06:06

Is there an implementation of a red black tree in clojure?

donaldball18:06:52

A sorted-set isn’t strictly a red-black tree, but it should have similar performance characteristics if that’s what you’re after.

victora18:06:51

Thank you, I will try that!

victora18:06:23

How can I define the comparator for a sorted set?

donaldball18:06:31

Eh, not 100% sure but I think it uses the Comparable interface

val_waeselynck18:06:09

@victora any arity-2 function is a comparator (clojure.core/compare being the default one). Use clojure.core/sorted-set-by to make a set with a custom comparator

victora20:06:52

Can someone recommend me a library that transforms a sorted-set to json?

donaldball20:06:11

Most clojure json libraries will convert an arbitrary seq into a json vector

donaldball20:06:44

There is no json type which unambiguously preserves the clojure set type, to say nothing of a sorted set. Converting from a sorted-set is free, but you’ll have to explicitly convert back to a sorted-set if you need to consume this json from clojure.

victora20:06:29

So i'd have to make my sorted-set into a seq, and only then transform it?

val_waeselynck20:06:28

@victora it's already a seq 🙂

donaldball20:06:12

In your repl, you can do e.g. (cheshire.core/generate-string (sorted-set [3 2 1])) and see

victora22:06:09

Is there such a thing as a background job in clojure? With ring

noisesmith22:06:13

there’s a lot of options - we have threads and schedulers and such, quasar makes sense if you need to track scheduled things across multiple hosts and / or restarts

noisesmith22:06:42

ring itself doesn’t have anything for scheduling, but iirc pedestal does