clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
Helins 2021-02-15T16:34:37.180300Z

Can Closure figure out that type extensions are never used and remove related code? (eg. using extend-type with a protocol in a library but the user ends up not using that).

Helins 2021-02-15T16:35:25.180700Z

I would say no based on what I see but who knows

thheller 2021-02-15T16:38:40.181400Z

yes in theory. in practice unlikely though. CLJS isn't "typed" enough for this to work reliably

Helins 2021-02-15T16:41:21.182800Z

And I guess there is no way to help the process? Some type annotation somewhere?

thheller 2021-02-15T16:42:32.183500Z

depends. can't explain the process really. yes, typehints and eliminating dynamic uses of the protocol

thheller 2021-02-15T16:44:21.184500Z

basically you'll have to spend a bunch of time going through pseudo-named code looking for reasons why the code stays alive when you think it should be removed

thheller 2021-02-15T16:44:42.185Z

closure is surprisingly good but many of the more advanced features really only work in fully typed code

thheller 2021-02-15T16:45:07.185400Z

but thats like tweaking the last 10% when you already get 90% of the benefit

👍 1
Helins 2021-02-15T16:50:51.186300Z

So essentially, when extending protocols, dead code is rather to be expected on native types and custom ones that are used at least once somewhere

thheller 2021-02-15T16:52:12.187100Z

hard to say. really depends on how you use it but I'd say that is accurate yes.

Helins 2021-02-15T17:10:04.187500Z

Makes one slightly paranoid about overusing protocols

thheller 2021-02-15T17:11:38.188100Z

well you are using protocols with the intent of using them no? I mean you don't intend to write dead code from the start?

Helins 2021-02-15T17:17:35.189Z

In the context of an app yes indeed, in the context of a lib it's best to think twice and try no to be too clever

isak 2021-02-15T18:06:28.189700Z

This is a good way to do it: https://github.com/andrewmcveigh/cljs-time/blob/master/src/cljs_time/extend.cljs#L1-L2

👍 1
fsd 2021-02-15T20:18:45.200300Z

Hello there, I am new Clojure(script) world. I needed a little with clojurescript, so I am fetching information from data api which contain html tags <br/> \r\n that are in hashmap as values. This is so that lines breaks after at desired sentence in HTML dom. I expect the line to break everytime this <br/> \r\n , however this is what I am getting

Whenever has this tag <br/>
This is what i get 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. <br/> Quisque nisl eros, pulvinar facilisis justo mollis.
this is when I have \r\n
This is what i get 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nisl eros, pulvinar facilisis justo mollis.
Thanks in advance

2021-02-15T20:22:48.202300Z

A lot of HTML rendering engines are reluctant to render “external” strings as HTML due to the danger of hacking, so they render “</br>” as a literal string &lt;/br&gt; instead of as a break. I would expect the \r\n to work though. What are you using to generate the HTML for your DOM?

fsd 2021-02-15T20:24:54.202900Z

I have a shadow cljs react application with helix dom

2021-02-15T20:32:03.205Z

Ok, React definitely won’t let you inject a “</br>” into your DOM. At the javascript level it will let you call dangerouslySetInnerHtml() to do something like that, but I wouldn’t take the chance myself. I haven’t used helix, let me look that up real quick and I’ll make a suggestion.

2021-02-15T20:34:24.205200Z

Oh derp, no, HTML treats \r\n like a single space character unless you’re inside a &lt;pre&gt; block or similar.

fsd 2021-02-15T20:36:59.205400Z

Ah I see

2021-02-15T20:46:58.206900Z

Ok, quick and dirty, here’s an example with my fake d/div function

(def test-string "Lorem ipsum&lt;/br&gt;dolor sit amet\r\nQuisque nisl eros.")
=&gt; #'user/test-string
(defn my-block
  [from-api]
  (let [lines (clojure.string/split from-api #"\r\n|\n|&lt;/br&gt;")]
    (apply div (for [line lines] (div line)))))
=&gt; #'user/my-block
(print (my-block test-string))
&lt;div&gt;
  &lt;div&gt;Lorem ipsum&lt;/div&gt;
  &lt;div&gt;dolor sit amet&lt;/div&gt;
  &lt;div&gt;Quisque nisl eros.&lt;/div&gt;
&lt;/div&gt;
=&gt; nil

fsd 2021-02-16T14:30:17.210300Z

Ah I see thanks Man

2021-02-15T20:48:24.208100Z

Basically what I did was use clojure.string/split plus a regular expression to split the incoming string into separate strings wherever there was a \r\n or a &lt;/br&gt;, and then render each string separately.

2021-02-15T20:48:43.208400Z

Couple extra lines, but it makes React feel safer.

fsd 2021-02-15T21:05:36.208500Z

Thanks @manutter51 for taking your time helping me out :)

👍 1
fsd 2021-02-15T21:12:38.208800Z

I’ve tried using there function I am getting Use of undeclared Var div

dazld 2021-02-15T21:32:18.209Z

did you try https://developer.mozilla.org/en-US/docs/Web/CSS/white-space ?

dazld 2021-02-15T21:46:40.209400Z

ah, nevermind, saw you were processing html line breaks too.

2021-02-15T22:11:52.209600Z

Oh, sorry, whatever you’re using to generate your DOM HTML. I was looking at the docs for helix, and I saw they were using a function d/div, so I made a fake/demo version of it because I didn’t want to build a whole helix project just for a quick demo. 😄

👍 1