Fork me on GitHub
#clojurescript
<
2022-02-16
>
eploko06:02:47

Hey folks, is there a way to figure out if a sequence is a lazy one?

p-himik07:02:30

Depends on what you mean. If you know that x is a lazy sequence and you want to check whether it's been realized, then there's realized?. If you don't know anything about x except that it's a collection and you want to know whether more work will be done when you iterate over it, then there's no simple solution. E.g. you can make a lazy seq by using something like (map identity (range 100)). It's a proper lazy seq and realized? will return false unless you iterate over it. But you can then do cons on that seq, and now it suddenly becomes a not-so-lazy seq that wraps a lazy seq, calling realized? will throw an exception here.

Rambabu Patina10:02:55

I am new to Clojure script and I was trying to add a value to the json payload.

//page-data payload
{
    "title": "Test page",
    "type": "page",
    "version": {
        "number": 12
    },
    "body": {
        "doc_format": {
            "value": "{\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"content\":[{\"text\":\"Updated by API\",\"type\":\"text\"}]}],\"version\":1}",
            "representation": "doc_format"
        }
    },
    "metadata": {
        "properties": {
            "editor": {.     // This is the one I was trying to add
                "key": "editor",
                "value": "v1"
            }
        }
    }
}
This is the clojure script I was trying
(let [editversion {:key "editor" :value "v1"}]
  (assoc-in page-data [:metadata :properties :editor] editversion)
)

Rambabu Patina10:02:01

It's not working, What I am missing? your help will be much appreciated!

azimpel10:02:22

Hi, your data structure has string keys, so you can use something like this: (update-in page-data ["metadata" "properties"] assoc editversion)

p-himik10:02:18

Also note that it's unclear from the OP what page-data value actually is. A JSON can be in a serialized form - as a string. It can be a JS object. Or it can be a CLJS data structure. update-in will work only in the last case. If you have the data as a JS object and you need to turn it into CLJS data, you can use the js->clj function for that.

Rambabu Patina10:02:30

It's a CLJS data structure. Let me try with update-in

Rambabu Patina11:02:47

Using update-in now it's adding as below:

"editor":{"{\"key\":\"editor\",\"value\":\"v1\"}":null,
"nil":null
}

p-himik11:02:52

If that's not what you needed to get, you have to provide us with the exact code.

p-himik11:02:11

Also, there's no need to use update-in + assoc as suggested above since there's assoc-in. And now that I gave it a closer look, the initial suggestion by azimpel is wrong - it doesn't provide any key for assoc.

šŸ™Œ 1
Rambabu Patina11:02:41

Thanks @U2FRKM4TW, here is the actual code snippet

(let [editversion {:key "editor" :value "v1"}]
        (->
          (http/request
            (assoc request
              ::http/endpoint (str "/xxx/xxx/xxx" (:id page-data)) // page-data is the cljs data structure as mentioned in the question
              ::http/method :put
              ::http/data
                (->
                  page-data
                  (select-keys [:title :type :body :metadata])
                  (assoc :version new-page-version)
                  (assoc-in [:body constants/page-content-format :value] (utils/clj->json new-body-value))
                  (assoc-in [:metadata :properties :editor] editversion) // This is the line trying to set editor value
                )
            )
        )
    )

p-himik15:02:07

The :properties map will have a bit strange structure where the word "editor" is repeated: {:editor {:key "editor" :value "v1"}}. But maybe that's intentional.

Rambabu Patina05:02:14

Yes it's added intentionally

Rambabu Patina06:02:09

Great @U2FRKM4TW It worked like a charm. Thanks for your help

šŸ‘ 3
timo14:02:57

Hi, how do you retrofit url-rewriting into a reagent/re-frame-app? I want the url to update when I change the tab and I want to be able to enter the url and get to the tab directly. I am newly in charge of an app that does not have this.

timo14:02:16

Would you use something like kee-frame?

p-himik15:02:20

Yep, either kee-frame directly or just check out how it works and roll out your own implementation.

John Bradens17:02:55

How do I get my web app to show up on Google? Like if I have articles on the app, and someone searches for something similar to a headline? How would it rank? Right now it's just hosted on heroku

p-himik17:02:50

Not #clojurescript related at all. It's called Search Engine Optimization or SEO for short. There are plenty of materials on the topic. It's a whole area and different search engines behave differently and require different approaches. A good starting point is, well, Google's documentation on what to do.

John Bradens17:02:43

Ok thanks. I'm a total beginner so if my app is written in clojure/clojurescript, I can still just google the basic stuff, and then translate that into my app? And it won't be any different for functional programming?

John Bradens17:02:42

Also how do I know if I have SSR?

p-himik18:02:06

Yes, it's not related to the programming paradigm at all. > Also how do I know if I have SSR? Uhhh.... Have you set it up? Are you using a framework that somehow magically sets it up for you? If not, then you don't have it.

John Bradens18:02:29

Ok I'm using a luminus template. Would I see if that does SSR in the documentation? If it doesn't, can I just use a framework to do it & then it's taken care of?

John Bradens18:02:36

Thanks for the help @U2FRKM4TW

p-himik18:02:19

Sorry, no clue about Liminus. But yeah, it should be documented. An easy way to check though - just curl your web page address and see whether it has the right markup. Or disable JS on your web page and reload it. If the markup is not that of a complete page (for SPAs it usually has a single div and some script) or if the web page is completely empty or has nothing but some loader, then you don't have SSR.

p-himik18:02:22

And whether you can "just enable" SSR depends on how you're writing your UI. But more often than not, the answer is "no, you can't just enable it, you have to work for SSR to be there."

John Bradens18:02:51

Ok thanks. I have an SPA (that I made following a clojurescript tutorial), so I'm pretty sure I don't have SSR, but I'll do the check like you suggest

John Bradens18:02:02

What kind of work do I do to enable it?

p-himik18:02:27

Given that you're a total beginner, I'd suggest to steer away from either SSR or SPA. Using only one of them is relatively simple and easy. Using both is neither.

John Bradens18:02:11

Ok so if I want to create a web app that shows up in google, I should aim for SSR and not worry about SPA yet?

John Bradens18:02:53

Also do you think I should just use python and javascript instead of trying to learn clojure & clojurescript? Since I don't really know anything

John Bradens18:02:17

Let's say I want to tackle the challenge of doing SSR & SPA. Are there any resources you think I could look at as a jumping off point?

p-himik18:02:06

> I should aim for SSR and not worry about SPA yet? Yes. > do you think I should just use python and javascript instead of trying to learnĀ clojure & clojurescript? IMO Python + JS might be easier to get into but it won't be a good thing in the long run. I myself have been using Python for quite some time and it used to be my favorite language. Well, not anymore. :) But of course you're on Clojurians Slack server so people here will be biased, including me. But overall, the language choice doesn't really matter as long as it's a general purpose language that has web-related libraries. > Let's say I want to tackle the challenge of doing SSR & SPA. Are there any resources you think I could look at as a jumping off point? When learning how to drive, the instructor doesn't just put you in a car and let you drive anywhere. There are steps that gradually build up to the knowledge and skill level that's required to drive a car. Same thing here. Don't think about doing SSR+SPA just yet. Build a small static app using SSR, understand its constituents, understand how it works. Then build a small SPA app without SSR, and understand it as well. Maybe write a few apps, try different UI libraries and approaches, see how they're compared to each other, see how you like them, and keep on learning. Only then think about how to combine SSR and SPA.

p-himik18:02:42

Also, regarding JS specifically - when writing CLJS apps, you will invariably have to use JS functionality every once in a while, especially if you rely on NPM libraries. So, while a perfect knowledge of JS isn't required, a decent understanding will be of immense help.

John Bradens18:02:30

Ok thanks, that's solid advice. I think what I have right now is an SPA without SSR from the tutorial/book I've been following. I'll learn SSR in isolation with a small static app, and try to understand SPA & SSR separately. Then I'll try to combine them. I appreciate the help.

John Bradens18:02:47

And ok, good to know about JS! I have been dabbling a bit to understand the basics as it pertains to CLJS

John Bradens18:02:37

Hypothetically, if someone knows what they are doing, how much time would it take to implement SSR in an SPA? If they work full time... could it be a weekend project? A day? A whole week? Just trying to understand the scope of difficulty, assuming someone has the knowledge already

p-himik18:02:16

Very much depends on the kind of SPA and the kind of SSR, what particular things and approaches you're using. E.g. you can write a CLJS app that's mostly using some JS framework without many new abstractions. Suppose it's pure Vue.js without any third-party libraries - then it should be relatively simple, by just following this page: https://vuejs.org/guide/scaling-up/ssr.html I write data-heavy Reagent apps that rely on a lot of NPM libraries that do things differently (some are just React components, some have their own vDOM implementation, some manipulate the DOM directly, some manipulate HTML string,...) - SSR here is possible but incomparably harder to achieve.

John Bradens18:02:45

Ok cool, thanks for the info. I'm trying to make a web app that's similar to quora, and have it show up in google if people search similar questions. The tutorial that I've been doing is like a twitter clone & uses a lot of reagent... do you think I should start over & go about things differently if I want SEO to work out?

John Bradens19:02:45

Also out of curiosity, are the reagent apps you write for work or personal projects? I'm just doing personal projects right now but might try to get a job in the future & am curious to hear about Clojure & Clojurescript being used in professional settings!

p-himik19:02:52

By the sound of it, you don't need SPA at all given that a Quora-like website doesn't have an innate need for dynamic functionality. Some elements of your web page could be dynamic, but the majority can remain within a framework that statically generates HTML pages. > are the reagent apps you write for work or personal projects? I don't separate it that much since I'm in a good position where I don't have to work on things I dislike. But I'm getting paid for what I'm doing, for the most part, yes. :) You mind find #jobs-discuss a nice place for such discussions.

John Bradens19:02:11

Wow if I don't need an SPA that will simplify things for me a lot, I think! That's a big relief. I'm going to start diving in to static sites a bit more, I think

John Bradens19:02:20

Thanks, I'll check out #jobs-discuss!

šŸ‘ 1