Fork me on GitHub
#beginners
<
2018-11-27
>
sova00:11:53

What am I missing? I just tried to put my own client.js file into my index.html and although it loads the file, goog is not defined ;_;

alex56108:11:34

could it have something to do with this? https://gist.github.com/swannodette/8775213 - tldr; try setting a different :optimizations level (eg. :whitespace) in your :cljsbuild options in project.clj

vitorstipanich00:11:14

Hey guys, has anyone in here ever published a CLJS app to github pages? If so, how? Im using shadow-cljs btw

sova00:11:56

@vitorstipanich i've never tried that but it seems possible, if your app is just html and js...

vitorstipanich02:11:51

@sova yeah thats what I thought. but how to generate a ‘production’ js? with the dependencies and stuff

vitorstipanich22:11:19

that helped me. and also I compiled the production build with both js and html on the same directory

sova13:11:58

I was finally able to write a working project.clj that has two javascript files as outputs

sova13:11:24

and now, instead of "hello world," my app says, "The world is yours."

quieterkali14:11:23

Hey guys, i am building a restful api with compojure-api and using swagger. i have to return a vector of vector as result a http request but i can figure out achieve that with swagger. Can anyone help me please?

trailcapital14:11:43

can you give more context on what you're trying to do? code snippets help a lot

quieterkali14:11:09

I just sent it @trailcapital ☝️

trailcapital14:11:28

is vector a schema to be applied against the body or is it the clojure.core/vector function?

quieterkali14:11:22

Clojure function:disappointed_relieved:

quieterkali14:11:34

I need a vector of return type

quieterkali14:11:54

Cause the return type will be the grid

trailcapital14:11:01

You can use https://github.com/plumatic/schema to define a schema for the response

quieterkali14:11:27

I am using it already

quieterkali14:11:55

I just don't know how to achieve what I want :(

trailcapital14:11:22

you just want your return schema to check if it's the body is a vector?

trailcapital14:11:01

I think [s/Any] would work

quieterkali14:11:02

If body is vector

trailcapital14:11:19

where s is schema.core

quieterkali14:11:48

But using any doesn't look like not secure?

quieterkali14:11:03

I mean anything will go throught

trailcapital15:11:20

right, it will allow anything to be included inside the vectors. I'm not sure what your response looks like so I can't scope it down more - but yeah, you can't pass a function as the value for :return - it needs to be a schema which validates the response

quieterkali15:11:15

You helped more than you think 🙂

quieterkali15:11:58

and with your last message i undertood why [s/any]

quieterkali15:11:21

thank you very much @trailcapital 🙏

quieterkali15:11:39

@trailcapital please check this code from swagger dataytpes

trailcapital15:11:17

I'm not an expert on Plumatic's schema by any means, but from my understanding, if you have an object that is not a clojure type then you can define you're own spec by making sure the object is of a certain class. i.e.

(s/validate java.lang.String "schema")

trailcapital15:11:04

I would read through the https://github.com/plumatic/schema README. That's where I got the above example from.

trailcapital15:11:30

there is no s/Vector, but it would be easy enough to define one yourself (and an equivalent for other missing types):

(def Vector [s/Any])
I'm not sure how practical that is - there may be a reason that s/Vector doesn't already exist.

quieterkali15:11:32

that is what i am suspecting or like you said it's one of those datatype that user should create

quieterkali15:11:00

thank very much for taking time to help me 🙂

jaihindh.reddy15:11:34

I thought sequences are compared by value. (= [1 2] '(1 2)) evaluates to true but (= [1 2] #{1 2}) evaluates to false. What am I missing here?

trailcapital15:11:09

sets are not considered sequential types in Clojure.

jaihindh.reddy15:11:12

(= [1 2] (seq #{1 2})) is true and so is (= [1 2] (first {1 2})). I'm confused 😕

trailcapital15:11:32

(sequential? #{1 2})
=> false

jaihindh.reddy15:11:29

What's the difference b/w clojure.lang.ISeq and clojure.lang.Sequential?

alexmiller16:11:36

ISeq is for sequences, Sequential is a more generic trait for any sequential thing (lists, vectors, sequences, etc)

dpsutton16:11:03

@jaihindh.reddy (seq #{:a :b :c :d :e}) is stable but that doesn't mean you can predict what it will be. so in your example it happens to be (1 2) but that is not true in general

ikitommi16:11:17

@quieterkali [[s/Int]] would be a "array of integer arrays".

quieterkali16:11:32

@ikitommi thanks for your answer. so if i have a cusotm object, then it will be [[s/customobject]] 🙂

quieterkali16:11:48

i am going to try that

alexmiller16:11:45

Equality of collections uses an equality “partition” of comparable collection types: sequentials, sets, and maps

alexmiller16:11:10

Collections don’t compare equal across the partitions but do within each partition

somedude31416:11:03

Is there a way to express the following in a more succinct manner: (when (and (contains? params :upsert) (= (params :upsert) true)) "... ... ...")

bronsa16:11:55

(when (:upsert params) "..")

somedude31416:11:06

That's more like it 😄

mathpunk16:11:24

I keep using the pattern (first (filter predicate? items)) and then doing something with the result. Is there some some variation that would be better?

danieleneal16:11:01

defining ffilter as (def ffilter (comp first filter)) is quite common I think. Don't think there's a more succinct way built in

tavistock16:11:40

alternative suggestions ive seen are using a different data structure to store your data, eg a hashmap. which may not be helpful all the time but sometimes i refactor (first (filter ...))s to

mathpunk18:11:05

@ @ thanks guys!

jaihindh.reddy16:11:54

^^^ Makes sense. But what are the semantics of ISeq and Sequential. Why doesn't clojure.lang.PersistentHashSet implement Sequential?

alexmiller17:11:23

sets are unordered

alexmiller17:11:31

and thus have no canonical sequential ordering

tvanish18:11:18

Hello, does anyone have a recommendation of a clojure library for mocking http server?

hiredman19:11:42

like, just run an http server

hiredman19:11:02

it is a function call away

manutter5119:11:32

@tvanish do you mean you want to mock out sending requests and receiving responses, as if with an http server?

tvanish19:11:51

Yes, my code should call http server and get some response. I want to mock the behaviour of the server from the test and assured that it did get my request

manutter5119:11:49

You could use http-kit or similar to just build a real http server like hiredman suggested, but it may be sufficient to just test the code to make sure it’s sending the right request maps, and responding correctly to mock response maps.

noisesmith19:11:25

there's clj-http-fake which mocks http calls via clj-http

sova19:11:28

POST works great, puts the params in the message body (???) so :params {} on the server is empty and :body is #object[IncomingMessage [object Object]]

sova19:11:03

how can I parse out the :params {} map on the server?

sova19:11:57

CLJS params empty when using cljs-ajax o.O

lennart.buit19:11:50

well, [object Object] is the default toString of any plain old javascript object

sova19:11:37

Okay, that helps. I guess the info I want must be cryptically tucked into :body

lennart.buit19:11:41

but I know bare to nothing about cljs, so I cant help you any further

sova20:11:05

no problem, thanks for the tip

sova20:11:50

Hmm what to do with #object[IncomingMessage [object Object]]

noisesmith20:11:46

@sova for things like that in cljs my usual technique is to use the console.log method to inspect in the browser, which should expose the full class name and the methods and slots

noisesmith20:11:03

then if you are lucky it's a stock js type so there's an MDN page for it

sova20:11:49

Thank you @noisesmith i rearranged the sequence of some keys in the map I was trying to POST and things began working.

sova20:11:47

namely, I had to move all my params straight into the :body {} and send the :headers with x-csrf-token a-f-t last. probably due to some sort of pre-emptive packet creating

jstaab21:11:22

Can anyone give me a hint as to how to add meta to a js/Error? I called (with-meta (js/Error "message") {:x :y}), but got "No protocol method IWithMeta.-with-meta defined for type object: Error: message". I assume I could just extend IWithMeta, but would I just stick it on the object using a plain old javascript property?

jstaab21:11:20

Think I got it:

(extend-protocol IWithMeta
  js/Error
  (-with-meta [e meta] (aset e "meta" meta) e))

(extend-protocol IMeta
  js/Error
  (-meta [e] (aget e "meta"))

(meta (with-meta (js/Error. "message") {:x :y}))
;; => {:x :y}

dnolen21:11:40

@jstaab you should be careful with that technique

dnolen21:11:48

you are globally modifying the prototype

jstaab21:11:10

By extending the protocol or doing aset?

dnolen21:11:48

extending the protocol

dnolen21:11:59

and aset on anything but arrays is also asking for trouble

dnolen21:11:23

Clojure(Script) has a standard data conveying error type

jstaab21:11:37

Oh hey that's more what I want

dnolen21:11:57

it is not IMeta but it doesn’t need to be because it takes data which can be IMeta if you like

jstaab21:11:34

Perfect, thanks @dnolen

jayzawrotny22:11:05

What’s the fastest way to convert a hash-map to a list of vector pairs? I got (map identity {:a 1 :b 2 :c 3}) is there a better function for that?

andy.fingerhut22:11:41

Tools like criterium are your friend for determining what is fastest, in the absence of other information, but I'd guess seq would be somewhat faster than what you proposed.

jayzawrotny22:11:23

Heh wow seq will definitely work. I’ll check out criterium now too. It will not be a lot of maps or even a very big map.

ben.grabow22:11:38

What's an idiomatic way to run an infinite go-loop until the initiating thread says to stop?

ben.grabow22:11:46

Looks like I can use alts!. I think this will produce to produce-ch until a value is provided on stop-ch. Is this the right track?

dpsutton22:11:05

i think your use of alts is not correct.

dpsutton22:11:13

check the signature again

hiredman22:11:34

the file has both alts! and alt!

noisesmith22:11:49

usually I would write the code to accept stop-ch as an argument - but otherwise you can also skip defining a stop-channel and just exit if someone closes your input or output channel

noisesmith22:11:10

you should be checking your channel reads and writes for nil anyway, and the natural thing to do is to exit if one gets closed

noisesmith22:11:49

this is how much of core.async works - you ask something to exit by closing a channel it relies on

ben.grabow22:11:35

The go-loop is going to be an algorithm that produces its own values, rather than processing something off an input-channel, so I think I need a stop-ch since the loop is otherwise independent of the outside world. Is my thinking correct? I suppose I could close the produce-ch when I don't need its values anymore.

noisesmith22:11:08

putting to a closed channel returns nil

noisesmith22:11:47

right - if you use a channel to read or write, you should probably check for nil and exit, so the stop behavior is already there in the correct error handling behavior

noisesmith22:11:19

(there are more complex cases of course if you don't rely on any one specific input or output chan...)

ben.grabow22:11:47

Makes sense, thanks!

jstaab22:11:42

Anyone have tips on building a map using something like reverse :keys destructuring (ala javascript)? I know I could write a macro, just wondering if something exists. E.g.:

(let [one 1 two 2]
  (succinct-map one two))
;; => {:one 1 :two 2}

sodesu9923:11:19

(defrecord Node [elm left right]) (Node. 1 3 5) => #cljs.user.Node{:elm 1, :left 3, :right 5}

dpsutton22:11:16

it came up here: https://clojureverse.org/t/shortand-clojure-syntax-for-properties-on-hashmaps/1918 TL;DR. many people hate it for some reason. i think it is great. three's a macro in the comments

jstaab22:11:40

Good enough for me, thanks @dpsutton

noisesmith23:11:20

that succinct-map or whatever you want to call it is a great thing for debugging even if you consider it bad style in committed code