Fork me on GitHub
#clojure
<
2016-11-16
>
etherfuse00:11:00

Hi, I’m trying to generate a java class that would be used as a Builder in the builder pattern. I can get it to work but it’s kind of a strange compile process. Is there a standard way to return self in :gen-class ?

hugesandwich00:11:12

@brycecovert nice, I had thought of doing exactly this which is why I mentioned FFI. There's plenty of things in other languages doing FFI to curses, so lots of examples to reference and improve upon. Just depends on the use-case. Was thinking of releasing some lighter-weight escape sequence parsing/ansi colors related stuff with a friend, eventually for those that don't need a full virtual term.

etherfuse00:11:31

If I remove the return statement, compile…add it…then compile it works properly

notid01:11:23

@hugesandwich very cool. best of luck to you! I’ve also seen some clojure folks use Lanterna, but, of course, it’s not a real terminal either.

echristopherson01:11:22

I want an ADM-3A

etherfuse04:11:59

I have kind of solved my own problem but it feels very hacky. I’m still curious if there is a more standard approach. https://stackoverflow.com/questions/40622226/have-clojure-generate-a-java-class-which-is-used-like-a-builder-pattern

etherfuse04:11:27

Basically, I created a precompile folder and have a file that generates and empty builder.class, the class is overwritten when the general compilation happens and the return values reference the previous builder.class which enables it to compile.......heads spinning

hlolli09:11:24

I notice that when generating transit+json response, special characters like german quotation marks and special back-ticks are turned into question marks. Is there a way to force the response to be utf-8 even when the Content-Type header is set to "application/transit+json" ?

thheller09:11:10

@hlolli everything is usually utf-8 by default

thheller09:11:37

so usually the problem occurs when reading the source data or the emitted data as something else

thheller09:11:14

ie. say you read a csv file which then transmitted via transit

thheller09:11:32

if the csv is ISO-8859-1 or so but read as utf-8 things break here

hlolli09:11:43

ok, Im sending an html snippet, which has these characters within its contents. I see from the front-end log in om that the question mark is there already on arrival. But not in the slurp from the backend.

thheller09:11:35

do you manually set an encoding for the HTML file that evals the JS?

hlolli09:11:22

no at no point I manually set the encoding. I slurp the snippet, store it in atom, and send it via

(defn generate-transit-response [data & r]
  {:status  200
   :headers {"Content-Type" "application/transit+json"}
   :body    data})

thheller09:11:29

no not the transit

thheller09:11:41

the HTML file that loads the javascript which then loads the transit?

thheller09:11:04

your frontend

hlolli09:11:27

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">

thheller09:11:47

and you did verify that the slurp data is actually utf-8?

hlolli09:11:46

I see in my repl when I dereference the atom this: “live code” in om I have ?live code? (hard to see but the these are german quotation marks).

thheller09:11:08

hmm maybe the REPL is smarter than the browser

hlolli09:11:37

yes plausable, is there a setting to slurp that I could try?

thheller09:11:46

try (->> (slurp "snippet.html") (spit "test.html"))

thheller09:11:57

see if test.html is broken

thheller09:11:22

(slurp "snippet.html" :encoding "ISO-8859-1") should work

hlolli09:11:01

no, when opening test.html with gedit all the characters are the same. “live code”

thheller09:11:33

:headers {"Content-Type" "application/transit+json; charset=utf-8"}

thheller09:11:57

try that then, but is defaults to utf-8 so that probably has no effect

thheller09:11:40

charsets are weird

hlolli09:11:23

1 min.. repl restart. Yes I would find dangerouslySetHTML to be suspicious here. But in my app state I also have the question mark, so its seemingly not to blame.

thheller09:11:44

typically if you don't mention charsets anywhere it defaults to utf-8 in java/clojure

thheller09:11:02

so you just have to be careful when reading things from somewhere else

thheller09:11:06

ie. files/network

thheller09:11:20

but your clojure side seems happy

hlolli09:11:26

damn, still question marks.

thheller09:11:16

try (slurp "snippet.html" :encoding "ISO-8859-1") maybe it helps 😛

hlolli09:11:29

ok, I try anything hehe, 1 min..

thheller09:11:36

I don't trust the quotation marks

thheller09:11:50

those usually mean the file was edited with Word or so 😛

hlolli09:11:37

yes, that could be the case actually, Im just given data, "man on the floor" 🙂

thheller09:11:13

no sane editor I know actually uses those

hlolli09:11:02

ok, @thheller I should buy everyone on this channel a beer, for noticing that Im actually not using this transit response function, but I noticed looking deeper in the code that Im doing at other place "Content-Type" "text/html" so I was asking for a help for a wrong problem.

hlolli09:11:43

yes doh homer style. Now solved aw_yeah

roelofw10:11:09

Im have done the atom and ref chapters of the clojure koans. For real applications can I better use atoms or refs or is this my own preference ?

val_waeselynck10:11:32

@roelofw the choice depends on the use case. Atoms are for uncoordinated updates, and refs are for coordinated updates. I've never had a use case for refs personally, I suspect atoms are used much more often.

val_waeselynck10:11:16

@roelofw note that the applications I've been developing on Clojure JVM (web apps backends mostly) usually require very little state management (which is mostly done at the level of the database).

roelofw10:11:48

oke, so if for example I want to update two financial accounts , I can better use refs

val_waeselynck10:11:27

@roelofw or you can hold the state for boths accounts in an atom

val_waeselynck10:11:48

what in your applications requires this state to be kept in memory though ?

roelofw10:11:16

so one atom can have the state of multiple accounts

roelofw10:11:35

I think the state schould be kept in memory

val_waeselynck10:11:41

sure, it's only a matter of data format

roelofw10:11:53

only the transactions schould be stored in a database

val_waeselynck11:11:57

so, is it for caching purposes? (I'm insisting on this, having grown suspicious of most use cases for stateful references on the server side )

roelofw11:11:28

nope, for displaying and validating purposes.

val_waeselynck11:11:10

ah, so it's a GUI application ?

roelofw11:11:26

yep, propaly a web application

val_waeselynck11:11:33

@synzvato have you tried interleave ?

val_waeselynck11:11:01

(not interposeb ut interleave, corrected)

val_waeselynck11:11:51

something along the lines of (into [(first coll-1)] (interleave coll-2 (rest coll-1)))

synzvato11:11:27

I just tried it and it seems to work fine, many thanks for the clean solution! 👍

val_waeselynck11:11:30

@roelofw alright, well in any case be careful with stateful references, for most web application use cases they have no role to play in your server-side application code 🙂

roelofw11:11:17

what do you mean. I do not understand it complete

val_waeselynck12:11:14

@roelofw what I mean is that you usually want your server-side program to be stateless, and if there is state, it should be scoped globally (typically shared between instances of your server via a database). The use case for process-scoped (which is what atoms and refs give you) application state is rare

gfredericks12:11:25

if I have a loop that can easily be rewritten as a reduce, is it weird to do so if the loop has side effects as well as producing a result?

borkdude12:11:25

Is it possible to have a type hint on an atom or volatile, so when you do (.foo @my-volatile), it knows the class?

gfredericks13:11:21

I very doubt it

val_waeselynck13:11:29

@borkdude does this not work ? (.foo ^MyClass @my-volatile)

borkdude13:11:13

@val_waeselynck of course, but then I would have to repeat it everywhere I deference the my-volatile. Not a huge problem, but just want to know if there is support for that

val_waeselynck13:11:52

ha, yeah I doubt it indeed

borkdude13:11:25

I could of course write (fn get-val ^MyClass [] @my-volatile)

borkdude13:11:43

and then (.foo (get-val)), or something

val_waeselynck13:11:07

yeah, or make sure that all the code that consumes the value downstream is type-hinted

val_waeselynck13:11:35

e.g in function parameters

etherfuse17:11:45

Yeah. That's cleaner...and it works

dpsutton17:11:23

anyone have experience with a webapp running in tomcat? I'm creating a zipfile for download and running into issues with permissions in creating the file

dpsutton17:11:38

and I'd like to move to just having the file in memory of course, but this is what i have at the moment

dpsutton17:11:57

i'm a .NET developer so all of the different streams are very strange to me at first glance

dpsutton17:11:05

input, output streams, filter streams, etc

dpsutton17:11:23

but i'm creating a zip file, everything works locally, just lack permissions to create the file on the server it seems

dpsutton17:11:34

i have full control of it but just not really sure where to start

Prakash17:11:24

if u are running it on some linux box, u will need write permissions on the directory in which you are creating the zip file

dpsutton17:11:00

for which user?

dpsutton17:11:44

tomcat7 owns the directory that the war files are expanded to

dpsutton17:11:51

is this not enough?

gfredericks17:11:05

you could start by chmod 777 __ing it to make sure that works

gfredericks17:11:15

otherwise you have weirder problems

hiredman18:11:04

don't do that

Prakash18:11:26

also I don’t think creating archives in the same directory is a good idea, that directory is expected to be cleared on a new war deployment

hiredman18:11:44

you should have an explicit directory someplace where you do the file stuff, it shouldn't be in the same directory where tomcat is putting war files

hiredman18:11:03

e.g. create your zip file in /tmp

Prakash18:11:43

I think java file API has an explicit method for creating temp files, if u need a temporary file, that is the right way to do it

cddr19:11:58

Kind of a java/framework interop question but we have a lib (lets call it "clj-foo") that pulls in a big java library, and then clj-foo is used by a bunch of apps. One problem I have is that the upstream library depends on logging and json libraries which clash with those used by other dependencies. Looks like I could exclude the problematic dependencies in clj-foo's project.clj with the understanding that the app would bring in it's own versions of these things. Is this a good idea? Is there any downsides I'm missing?

bfabry19:11:40

@cddr yes sounds like you want a combination of :excludes in the dependencies, and :provided

cddr19:11:46

Cool. Thanks

ag22:11:22

can someone help me with a macro? so, I need inside a macro, inside unquote splicing block ~@ to call another function, this works fine if I call function directly, but if the function I want to call is referred through another function that doesn't seem to work. It throws null pointer exception.

ag22:11:22

something like this for example:

(defn get-fn [b]
  clojure.core/str)

(defmacro foo
  [bindings]
  `(do
     (defanothremacro bindings
       ~@(map (fn [x]
                   (let [bar-fn (get-fn x)]
                     (bar-fn " - " (first x))))
              bindings))))

ag22:11:10

I guess I’m missing some “magical" clojure symbols

moxaj22:11:23

@ag what would you like to achieve? what is defanothermacro? can you show the call which throws npe?

ag22:11:07

so for example if instead of (bar-fn “ - “ (first x)) I do: (str “ - “ (first x)) - it works, right? But when I reference to str through another function - it doesn't

ag22:11:48

there’s some quoting/unquoting needed, defanothermacro doesn’t matter - it’s just to demonstrate why the first quote there (before do)

moxaj22:11:18

@ag you should probably unqoute that bindings after defanothermacro - but other than that, I see nothing

ag22:11:54

yeah I missed that it should be (defanothermacro ~bindings but that’s not it

ag22:11:59

it seems I have to quote somehow in get-fn and unqote it in the foo… I dunno

moxaj22:11:05

@ag post a gist with all the relevant code if you can

ag22:11:39

@moxaj oh, it’s still quite messy.

moxaj22:11:05

@ag when/where do you get the npe? during macroexpansion? at runtime? do you have a stacktrace?

ag22:11:23

during macroexpansion

moxaj22:11:47

@ag maybe (get-fn x) returns nil

ag22:11:22

no, no.. it does get the right function, it just doesn’t like me calling it ;(

ag22:11:40

it refers to it like this: #function[clojure.core/str]

gfredericks22:11:52

I think there are too many missing details to debug this

gfredericks22:11:19

the mess that prevents you from posting your code probably contains the problem :)

ag22:11:05

ok, I still can’t figure it out: so there’s body-fn how do I properly call it? https://gist.github.com/agzam/49df3515235a080628f86d7893507d0c so the last thing would macroexpand properly?

gfredericks23:11:06

@ag if the call to body-fn is throwing a NPE, I think the first guess is that your case on line 2 is not dropping to the default clause

gfredericks23:11:16

if you comment lines 3->9 do you get that same NPE?

ag23:11:10

oh… shit, right.... I forgot that I have body-fn only for the default case… goddammit

moxaj23:11:19

@ag body-fn was nil for me, hence the npe

ag23:11:34

yeah that was it, thanks guys!

seancorfield23:11:59

Please remember that this is a professional space and use language appropriate to such (with my Clojurians admin hat on).

seancorfield23:11:54

Also, as a general reminder, the Code of Conduct for Clojurians is here https://goo.gl/VsJ8q8

hwk23:11:19

https://www.youtube.com/watch?v=824yVKUPFjU <-- TeX in Clojure <-- is this open source anywhere?