Fork me on GitHub
#beginners
<
2019-05-30
>
jcb11:05:39

I'm muddling through some js to cljs translation for integrating npm libs.

jcb11:05:25

in a component like this - <Rect filters={[Konva.Filters.Noise]} noise={1} x={10} y={10} width={50} height={50} fill={this.state.color} shadowBlur={10} ref={node => { this.rect = node; }} onClick={this.handleClick} />

jcb11:05:47

how would you write filters={[Konva.Filters.Noise]} in clojurescript?

Alex12:05:33

@jcb You should be able to do -> Konva .-Filters .-Noise or (.-Noise (.-Filters Konva)). The .-allows you to "get" a property off a JS object

danielneal12:05:37

also the single element in curly braces { thing } syntax in js ES6 expands to {thing: thing}

danielneal12:05:37

oh wiat, that's not relevant here

danielneal12:05:59

the {} here are representing code within JSX I think

lilactown13:05:09

yes, it would be:

{:filters #js [(.. Konva -filters -noise)]
 :noise 1
 :x 10
 :y 10
 ...
}

Alex23:05:56

Oo that's a nice tip re: nested property access, thank you

lilactown13:05:28

.. is shorthand syntax for nested property access

jcb13:05:00

thanks for the replies, I don't think I gave enough context. It seems to be a namespace? I've not seen this syntax before. whole example can be found here - https://konvajs.org/docs/react/Filters.html

danielneal13:05:03

does the code @lilactown gave not work? I'd have thought it would

danielneal13:05:48

afaik js doesn't really have namespaces, just objects, so you'd access the members just like accessing an object property

jcb13:05:49

no dice, unfortunately. filters is undefined

lilactown14:05:27

@jcb you’re probably importing Konva like this right:

lilactown14:05:42

(:require [“konva” :as konva])
?

danielneal14:05:07

@jcb have you got a repl available?

lilactown14:05:10

FYI it’s really helpful to see code, if you’re saying something isn’t working

lilactown14:05:43

if the above is correct, you’d access it like so: (.-noise konva/filters)

lilactown14:05:53

but it’s really hard to say without you showing us what you’ve done so far

lilactown14:05:57

and how your project is setup

danielneal14:05:25

if you've got a repl available you can kind of figure out where things are going wrong - so is Konva defined? If so, log it to the repl (js/console.dir Konva) then see if you can access the properties etc

jcb14:05:20

thanks for the tip @danieleneal

jcb14:05:54

sorry for the lack of info - I'm dealing with a few things I don't totally get.

jcb14:05:13

it's a shadowcljs setup

jcb14:05:18

and react-konva

jcb14:05:49

So in node modules filters are available in konva/lib/filters in the same way shapes are in konva/lib/shapes. so [react-konva :refer [Rect]] makes rect useable as [:> Rect {etc}] however examining (js/console.dir react-konva) only lists shapes and setup (eg stage etc) functions not the filters. I've had unresolvable problems like this in the past when dealing with esModule exported libraries

jcb14:05:26

apologies for the long-winded explanation, I thought I was asking a syntax question

lilactown15:05:58

@jcb how are you importing react-konva?

jcb15:05:55

(:require [react-konva :refer [Stage Layer Rect]] in the initial ns require along with reagent etc

jcb15:05:52

as in the example I linked above - the filter functions aren't mentioned until the component

lilactown15:05:25

right, notice in their docs they’re also importing Konva

lilactown15:05:33

import React, { Component } from 'react';
import Konva from 'konva';
import { render } from 'react-dom';
import { Stage, Layer, Rect } from 'react-konva';

jcb15:05:45

sorry I am

lilactown15:05:03

so assuming you have something like:

(ns my-app.core
  (:require ["react-konva" :refer [Stage Layer Rect]]
           ["konva" :default konva]))
you should be able to log Konva.Filters.Noise: (js/console.log (.-Noise konva/Filters))

lilactown15:05:39

you might have to play with the import of "konva" - I’m not sure if it should be :default or :as

lilactown15:05:53

if you log the konva object itself, it might tell us more

jcb15:05:56

that could be it

jcb15:05:14

I remember having trouble with that in the past

jcb15:05:21

thanks very much

lilactown15:05:53

if you import it like: ["konva" :as konva] and log it, and it looks like: {"default": ...} then you should use :default instead

jcb15:05:33

that's a great tip cheers!

solf17:05:01

When using swap! (or anything else that requires a function) but I want to always return the same value, should I use constantly? i.e.:

(swap! foo update :bar (constantly 42))

solf17:05:55

Bad example, as it's not swap! but update that requires the function, but the idea is the same

manutter5117:05:41

If I know I’m always going to want to put the same constant value there, I’d just use assoc instead of update.

solf17:05:35

Yeah, I just thought about that. I was misusing assoc + update

RafaMedina17:05:09

Hi all, I'm stuck, certainly lost (I'm a noob in this ecosystem) I have a SPA with CLJS and now I'm trying to migrate de backend from nodejs to clj, well I chose luminous, for testing (No sure if the parameters produces a good result hahaha) lein new luminus myapp +sqlite +cljs +re-frame +swagger +service +auth -> lein run runs the clj part, Question, how do I run/load the front/cljs part?

noisesmith18:05:46

by connecting a browser to the port it opens

noisesmith18:05:42

if you add figwheel to the project, you can connect an interactive cljs repl in a terminal to the app in the browser

RafaMedina18:05:59

Will it run independently? different port front and back

noisesmith18:05:10

what does that even mean?

noisesmith18:05:18

if you write your webserver to only load static files, you can host it from a test server without running the back end, if that's what you mean

RafaMedina18:05:56

I understand what you mean, in this case i'm looking for, I think it's possible, using the CLJ make accessible the CLJS code from "browser"

fabrao22:05:47

Hell all, how do I convert this interop launch(String... args) to clojure?

fabrao22:05:04

String.. args ?

noisesmith22:05:23

(.launch O (into-array String args))

noisesmith22:05:32

where O is the object with a launch method

noisesmith22:05:40

and args is some sequential

solf22:05:15

I was using clojure.set/difference on somewhat large sets (around 10k items) and the performance was noticeable, so I modified it to use transients. Is there a reason why clojure.set doesn't use transients in the first place? Lower performance for small sets or something like that? Or is it purely for philosophical reasons , "don't use transients unless you really need them"?

noisesmith22:05:09

@dromar56 it could be as simple as clojure.set being older than transients, and nobody doing the work to make it use them

noisesmith22:05:29

arguably you shouldn't be doing set operations in hot paths anyway

4
noisesmith22:05:15

maybe that's overstating things

solf22:05:01

Thanks, I wanted to be sure there wasn't a downside to transients I hadn't taken into account. I could do something more optimized than set operations, but for now with the use of transients is fast enough

noisesmith22:05:44

@dromar56 also, since this is is #beginners I should point out that there's a common newbie error of using transients but not using the return values of transient ops, which risks dropping data on the floor

noisesmith22:05:11

always use the return values of transient conj! etc. just like you would with a normal immutable

andy.fingerhut23:05:09

I would expect that modifying clojure.set functions to use transients very well might make them a little bit slow when invoked on small sets. I haven't measured that recently.