Fork me on GitHub
#beginners
<
2019-09-18
>
sova04:09:06

(defn show-landing-pg [req]
 (let [ {params :params} req
        {username :username} params
        {password :password} params
        {session :session} req]
        (println req)
        (println ";sesh'''; " session)
  {:status  200
   :headers {"Content-Type" "text/html"}
   :session (assoc session :username "zzzzzzzz2222222255555555522222222ZZZzzz")
   :body    (slurp "resources/public/index.html")}))
does not update the session at all

hiredman04:09:28

You need to use some middleware to support sessions

brandon14905:09:24

Is s/fdef supposed to actually run any validation on a function during development time?

seancorfield05:09:14

@brandon149 Only if you use instrument (argument checking) or check (generative testing of the function).

brandon14905:09:00

@seancorfield thanks It seems like it’d be helpful to use instrument on every function running (at least during some dev time work) is that a common thing, or is spec more designed to just run on one particular function at a time?

seancorfield05:09:47

You can (st/instrument) will which instrument all functions for which s/fdef has been evaluated (so those namespaces have to be loaded).

seancorfield05:09:55

But that might slow down your tests so just instrumenting the functions you're testing / developing is probably best.

seancorfield05:09:51

Have a read of this and see if it gives you more insight on using Spec https://corfield.org/blog/2019/09/13/using-spec/

lennart.buit06:09:16

Fair note: the default st/instrument only checks :args specs, no :ret/`:fn`. If you wish to instrument both, you can use orchestra: https://github.com/jeaye/orchestra

mvar40k07:09:19

hello, i'm playing around with gen-class and class inheritance. I have the following: (ns test.core (:gen-class :name test.core :prefix "t-" :extends javax.swing.JFrame :init init :constructors {[String] [String]} ) (:import (javax.swing JFrame))) (defn t-toString [this] (println "JFrame title: " (.getTitle this))) (defn t-init [x] [[x] (ref {})]) i'm slightly confused about the role of init..Is there any way to include some other action in there like for example (.setSize this 200 300) like i would do in java in the class constructor (after i called super()) ? or do i need to define a separate function and call that one once the object has been created, i.e. (def fr (test.core. "Testing frame")) (.setSize fr 200 300)

andreas.scheinert07:09:32

Hello I want to build a clojureScript applicatin, can you recommend a good project template that works with java 12 (windows)?

markmarkmark11:09:50

I believe that it's snake_case and kebab-case

markmarkmark11:09:09

at least, that's how I've always seen it

markmarkmark11:09:45

or maybe it's kabob-case

mvar40k11:09:18

to me camelCase feels more natural especially when playing around with java libraries

mvar40k11:09:33

but it's just matter of taste

markmarkmark11:09:39

of course you have to use camel case when calling into libraries that use it, but I think it's pretty clear that the preferred Clojure style is kebab case. How many camel case names do you see on this page that aren't links to javadoc? https://clojure.org/api/cheatsheet

neo255111:09:21

In ClojureScript, how does Java Nashorn fit into the picture?

andy.fingerhut14:09:54

I don't have great knowledge here, and asking in the #clojurescript channel is likely to find more people knowledgeable of this topic. My understanding is that Nashorn is deprecated, and perhaps might be removed from a future JDK version, although it still exists in the latest OpenJDK versions. I have used it to run JavaScript code that was the output of the ClojureScript compiler, just fine.

andy.fingerhut14:09:26

It is not getting any new active development, as far as I am aware, unlike other JavaScript runtime engines, which are still being actively developed.

klaus.azesberger12:09:35

hi, this is probably a very noob question but i'm not a very experienced java-guy either: is there an easy way to tell slurp not to cache read file contents?

gerred12:09:06

afaik (and just tested in the repl) it's not caching?

klaus.azesberger12:09:05

lol maybe i got some issue with intellij actually saving the file with a different file handle or stuff like this - let me check

gerred12:09:56

i'm doing it in a comment form and re-evaluating the slurp

klaus.azesberger12:09:04

start repl > slurp file > foo=bar > edit file foo=baaz > slurp again from same repl > foo=bar > cat the file foo=baaz > edit file using cat > slurp in same repl > foo=baaz

gerred12:09:55

did that too and it's good for me.

gerred12:09:10

well, just by editing thef ile that is.

gerred12:09:16

didn't do it with cat

klaus.azesberger12:09:24

hm, what's your intellij setting for saves?

gerred12:09:00

ah I'm not using intellij atm, I'm using vscode and atom

klaus.azesberger12:09:18

ha, this did the trick for me

klaus.azesberger12:09:34

safe writes does something weird with the file handles

klaus.azesberger12:09:48

probably moving files instead of actually editing theri contents

klaus.azesberger12:09:28

after deactivating this option it works as expected

sova14:09:25

So my server is run with http-kit at the moment..

(defn -main [ & args]
		(server/run-server all-routes {:port 8117})
		(println "server on @ localhost:8117"))

sova14:09:08

I want to turn it into

(def app
  (wrap-defaults all-routes site-defaults))

sova14:09:56

Soo... actually as straightforward as

(defn -main [ & args]
		(server/run-server (wrap-defaults all-routes site-defaults) {:port 8117})
		(println "server on @ localhost:8117"))
woot

sova14:09:04

😄

sova14:09:17

slash beginner's mind

services15:09:55

Hi everyone, I'm trying to find my way into ClojureScript but have now hit a wall setting up a development workflow: I keep running into a Java ClassNotFoundException concerning javax.xml.bind.DatatypeConverter. I've found a way around this by specifying a particular version of ClojureScript (1.10.520) in my build.boot. I'm still encountering the problem though when trying to launch the ClojureScript REPL. I've put more details into a question on StackOverflow: https://stackoverflow.com/questions/57972342/clojurescript-repl-with-java-11-0-4-classnotfoundexception-javax-xml-bind-d I'd be super grateful if anyone of you could advise me on what I'm doing wrong… I'm really eager to finally get coding! Many thanks in advance and thanks to the devs for an amazing language! Oliver

andy.fingerhut15:09:19

In that question you say you did some Googling, and you found some answers relevant for when going from JDK 8 to JDK 9, but you did not think they were relevant because you are using JDK 11.

andy.fingerhut15:09:43

I think they are relevant, because the things you found relevant for going from JDK 8 to JDK 9 are relevant whenever going from JDK 8 to JDK later than 8

services16:09:46

Hi Andy, many thanks for the quick reply. I totally see your point. Still I'm wondering why I'm running into a problem dating from 2017… I have revisited a couple of said Google results. It seems the problem can be solved from the side of Java or ClojureScript. On the Java side I tried setting my environment variable BOOT_JVM_OPTIONS to "--add-modules java.xml.bind". But that gives me "java.lang.module.FindException: Module java.xml.bind". Maybe I'm doing something wrong since I know very little Java. (see https://github.com/http-kit/http-kit/issues/356) Some say to just wait for the next ClojureScript release (which has been around for quite some time now) … to my surprise I had to specify it manually and could thus make it compile. It's just that this doesn't affect the ClojureScript REPL where I still get the error… Also I think to have all my libs set to the latest versions… or is there still anything deprecated in my build.boot? Thanks again Oliver

services16:09:36

I've also tried setting JAVA_TOOL_OPTIONS: to "-Djdk.launcher.addmods=java.xml.bind", which does get picked up by boot… but changes nothing as to my problem with the REPL…

andy.fingerhut16:09:14

Regarding your comment of why you are hitting a problem from 2017, if you have recently changed a project that was using JDK 8, to using JDK later than 8, then it shouldn't be surprising that you may hit issues that others noticed when JDK 9 came out.

andy.fingerhut16:09:14

As far as more details on the particular issue, I do not have any more knowledge to share there. I mainly wanted to comment that JDK 8 -> 11 has many issues in common with JDK 8 -> 9 transition (plus possibly more).

andy.fingerhut17:09:27

Also, for issues specific to clojurescript, you may find more people aware of the problem on the #clojurescript channel than this one.

matthewlisp17:09:47

Hey everyone! If you're learning Clojure recently and have web experience but want to check out the Clojure-way to build APIs, checkout my starter guide: http://matthewlisp.com/set-up-clojure-api/

services17:09:51

Hi, thanks… could have thought of this myself… I however managed to solve it now … indirectly thanks to your advice to revisit the 2017 stuff… I had to also specify a more recent version of http-kit in my build.boot. So for now things seem to be working… still I'm puzzled why my setup would default to these old versions of libraries… I am indeed following a tutorial written when Java 8 was standard but I have replaced all version strings contained in the tutorial snippets with newer ones…so I still don't understand why I have to add newer versions of libs not even mentioned in the tutorial and why they would default to older ones… but hey, this is sort of academic at this point at least. Many thanks for your input… sometimes one just needs to talk to someone!

andy.fingerhut17:09:33

This may be a long shot, but if the person who wrote the tutorial is willing to update it for more current versions of software, that might be helpful to others besides you. And it sounds like you have something working for them to refer to, if they have such interest.

andy.fingerhut17:09:46

I have not used boot, so do not know if it has a command to do this, but leiningen and clj command line tools have options that direct the tool to show you all of your project's dependencies, and their dependencies, recursively. Using such a command on your project before the fix you found, and after, might help determine why the older version of http-kit was being used.

services17:09:19

thanks… I'll look into it! Still learning the basics and trying to stick to the tutorials, which tend to become outdated pretty quickly…

andy.fingerhut17:09:42

Well, when they involve libraries or aspects of the language runtimes that change across versions, then they tend to keep working for those same versions just fine. It just isn't very easy to find a machine to run JDK 6 on any more, if you find a tutorial that used that. Most stuff is backwards compatible, but those aren't the things that cause the issues you notice.

brian.rogers17:09:04

I'm trying to do something that I think is very simple and write a function that tells me true/false if it contains the number 2

(defn contains-2 [numbers]
    (mapv (fn [num] (= num 2)) numbers))
(contains-2 [1 3 4 5])
(contains-2 [1 2 3 4])
however when I run the two calls, it returns [false false false false] and [false true false false]. How can I make it return only true OR false?

petterik18:09:02

It is also common to use a set (`#{}`) when testing for the existence of some elements. Using some (as suggested earlier) you could write the contains-2 function as:

user=> (boolean (some #{2} [1 2 3]))
true
user=> (boolean (some #{2} [1 1 3]))
false

noisesmith17:09:26

use some instead of mapv

brian.rogers17:09:47

yay thank you @noisesmith!!

noisesmith17:09:39

you can wrap it as (boolean (some ...)) if you strictly need true/false instead of true/nil

brian.rogers17:09:10

even better =]

sova18:09:01

i added session middleware to my application and my app doesn't like my cljs POSTs

sova18:09:52

cljs GETs work but don't let me pass :params...

sova18:09:04

do I need to provide session data in my cljs (ajax) POST to my server?

noisesmith18:09:14

did you add a CSRF prevention middleware?

noisesmith18:09:44

if so, you need to switch to a post method that uses the CSRF token

noisesmith18:09:29

@sova one thing to look for is if when you post the client is trying to make an OPTIONS query

noisesmith18:09:36

(in the network tab)

sova18:09:17

Yes I added CSRF

sova18:09:53

right on. thanks

sova19:09:15

do you know what to call it in the params?

sova19:09:20

just :anti-forgery-token perhaps?

sova19:09:58

oh i see thank you, it's been a few years since i played with CSRF intimately

sova19:09:52

I have a question, can I do subsequent POSTs with one token or will it require a new token for each POST?

sova19:09:15

like the first successful POST should probably return the new token?

sova19:09:22

(ad infinity)

seancorfield19:09:13

@sova In general, CSRF works by using a unique token for every interaction (but I'm not sure how that works with async calls that could be in various different orders).

sova19:09:16

Right?! so yeah. lol. i wonder. can I just disable csrf for the specific urls?

lennart.buit20:09:18

I don’t think you want to disable CSRF protection

ewan.valentine8921:09:25

Bit of a weird one:

user=> (/ 200 (* 1000 60 60))
1/18000
^ Anyone know why that might happen?

noisesmith21:09:57

because it's the correct answer?

ewan.valentine8921:09:01

I was expecting a whole number, I'm trying to convert between hours and ms, and came across this

noisesmith21:09:21

if you want floats, you can provide one floating point arg, or call double on the result

ewan.valentine8921:09:26

Full function:

(defn to-hours [ms]
  (mod (/ ms (* 1000 60 60)) 24))

ewan.valentine8921:09:21

So it's presenting a decimal as a fraction?

noisesmith21:09:39

right, because floats lose accuracy, and the Ratio type used for fractions does not

quest21:09:47

> (type (/ 200 (* 1000 60 60)))
clojure.lang.Ratio

noisesmith21:09:56

by providing a float or double arg, you opt into the loss of accuracy

ewan.valentine8921:09:09

Gotcha, that makes sense

noisesmith21:09:17

there's also a BigDecimal type

mateus.pimentel.w21:09:44

Hey guys, someone knows the most idiomatic way to add an element to an array that is inside a map?

mateus.pimentel.w21:09:10

like someMap.list.push( item )

dpsutton21:09:41

(update m :key conj x)

mateus.pimentel.w21:09:36

ooh, thanks! 😄

andy.fingerhut21:09:25

assuming you mean a Clojure vector, when you said array (which some people use more often to describe a mutable array in Java/JavaScript/etc.)

noisesmith21:09:55

yeah, we have access to actual arrays too, but on the JVM those are not resizable

sova22:09:23

CSRF works fine with ajax POSTs, doesn't need a new token