beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
mbarillier 2021-03-11T01:01:46.268800Z

how do I map a lazy sequence? suppose I have a function that generates a lazy sequence, e.g. if (ints) generates 1, 2, 3 ..., but I want to map each returned value by inc, so something like (take 5 (some-function-here inc (ints))) generates [2 3 4 5 6], how would I code that?

aratare 2021-03-11T01:03:07.268900Z

perhaps (map inc (ints)) is what you’re looking for?

mbarillier 2021-03-11T01:07:05.269100Z

yeah, I was over-complicating it in my head. thx.

2👍
alexmiller 2021-03-11T01:10:37.269500Z

you already wrote it! :)

11👆
2021-03-11T06:28:55.271700Z

Hi, I'm using (clojure.string/replace "yes we use clojure" " " "...") or (clojure.string/replace "NULL" "NULL" "...") if i want to specify " " or "NULL" to replace with "..." how to write the syntax? There won't be both " " & "NULL" in one string

2021-03-11T07:30:46.273500Z

#"( |NULL)" should work. Note the leading # - that means it’s a regular expression.

1✅
Sithabiso Makhathini 2021-03-11T09:33:13.274100Z

Okay a bit of context might be a step in the right direction. I am currently going through the "web development with clojure 3rd edition" book and thats how I ran into this error. I went through all the steps and rewrote the code but I am still unsure of where I went wrong, heres a screenshot of what I get when I remove the ws namespace

2021-03-11T11:13:07.275100Z

Thank you @anders152, bullseye solution!

1🙌
2021-03-11T11:17:30.275500Z

Can I update the CLI tools from the CLI tools or do I need to re-install the newer verswion over the top?

2021-03-11T11:18:54.276300Z

nevrmind, just installed latest.

2021-03-11T11:40:33.277900Z

I'm wondering why this become nil

(def myvar
{:converted_date "5/31/2011", :SubTotal "6122.082", :TaxAmt "587.5603", :Freight "867.2389", :TotalDue "7576.8812", :sales_name "Michael Blythe", :AreaName "Others", :CountryName "Others"})
when i use
(myvar :converted_date)
=> nil
it supposed to show 5/31/2011, anyone can explain why?

dharrigan 2021-03-11T11:41:37.278200Z

That's strange, for it works for me...

dharrigan 2021-03-11T11:41:42.278400Z

user=> (def myvar
  #_=> {:converted_date "5/31/2011", :SubTotal "6122.082", :TaxAmt "587.5603", :Freight "867.2389", :TotalDue "7576.8812", :sales_name "Michael Blythe", :AreaName "Others", :CountryName "Others"})
#'user/myvar
user=> (:converted_date myvar)
"5/31/2011"
user=> (myvar :converted_date)
"5/31/2011"
user=> 

11➕
dharrigan 2021-03-11T11:42:20.278700Z

Have you eval'ed myvar yet?

2021-03-11T11:43:23.279100Z

cleaning-adv.core> (eval myvar)
;; => {:converted_date "5/31/2011", :SubTotal "6122.082", :TaxAmt "587.5603", :Freight "867.2389", :TotalDue "7576.8812", :sales_name "Michael Blythe", :AreaName "Others", :CountryName "Others"}

dharrigan 2021-03-11T11:43:35.279400Z

not quite like that

dharrigan 2021-03-11T11:43:48.279800Z

I mean, did you type this into a repl and try first?

dharrigan 2021-03-11T11:43:58.280100Z

or are you doing this in an editor?

2021-03-11T11:44:26.280500Z

in editor, then i run it into repl

dharrigan 2021-03-11T11:45:46.282Z

Right, okay, so most editors allow you to eval the var in their editing pane and then in the same window pane to do (myvar :converted_date) and evalutate that. For what I think could be happening is that your repl is in one namespace (user?) and your editor window pane is in another namespace

dharrigan 2021-03-11T11:46:02.282300Z

and vars are local to each namespace.

dharrigan 2021-03-11T11:46:15.282600Z

Only a guess, as I can't see what you're doing 🙂

dharrigan 2021-03-11T11:46:43.283Z

you see in my example above, I'm in the user namespace

2021-03-11T11:49:04.284700Z

no, i work in 1 namespace cleaning-adv.core , i've also c-x c-e in the editor as well, return into nil but

(:SubTotal myvar)
"6122.082"

dharrigan 2021-03-11T11:52:06.285100Z

if you type (keys myvar) what do you get?

2021-03-11T11:54:34.285500Z

this is all the keys

cleaning-adv.core> (keys myvar)
;; => (:converted_date :SubTotal :TaxAmt :Freight :TotalDue :sales_name :AreaName :CountryName)

dharrigan 2021-03-11T11:55:52.285800Z

and what happens if you do (:converted_date myvar)?

2021-03-11T11:59:29.287600Z

nil ... I wonder if it because i pull those data from .csv, does it have some effect?

(def csvopener
 (with-open [reader (io/reader "resources/AdvWorks_Sales_Report_Dirty_sample.csv")]
  (doall
   (csv/read-csv reader))))

(defn csv-data->maps
  [csv-data]
  (map zipmap
       (->> (first csv-data)
            (map keyword)
            repeat)
        (rest csv-data)))

(def dirtydata (into [](csv-data->maps csvopener)))

(def myvar (get dirtydata 3))

javahippie 2021-03-11T12:01:48.288200Z

I’m wondering if there are any strange characters in your keyword? What happens if you try this: (map #(% myvar) (keys myvar)), or better: (map #(= :converted_date %) (keys myvar)) ?

2021-03-11T12:03:22.289200Z

i checked with class, only the converted_date return nil

cleaning-adv.core> (class (myvar :converted_date))
;; => nil
cleaning-adv.core> (class (myvar :SubTotal))
;; => java.lang.String
cleaning-adv.core> (class (myvar :sales_name))
;; => java.lang.String 

dharrigan 2021-03-11T12:05:30.290Z

Could there be something screwy with the quotes on the date field? Sometimes those quotes aren't real quotes - a few times I ran in that on other things.

2021-03-11T12:07:25.291300Z

@javahippie here's the result,

cleaning-adv.core> (map #(% myvar) (keys myvar))
;; => ("5/31/2011" "6122.082" "587.5603" "867.2389" "7576.8812" "Michael Blythe" "Others" "Others")
cleaning-adv.core> (map #(= :converted_date %) (keys myvar))
;; => (false false false false false false false false)
@dharrigan hmm... what format should I change the date? the / symbol might messing up the string?

javahippie 2021-03-11T12:08:15.292Z

Your keyword :converted_date is not what it seems, the first entry would have been true otherwise in the second call

dharrigan 2021-03-11T12:08:39.292800Z

yes, perhaps the _ is not a real underscore?

javahippie 2021-03-11T12:08:39.293Z

There are some chars in there, so that (= :converted_date :converted_date) is false

2021-03-11T12:21:29.294300Z

that's really confusing, I've changed it without _ then re-type it become ConvertedDate1 still showing nil i'll rename the header, and change the column from 1st to 2nd or 3rd column... let's see or my csv is saved in CSV UTF-8 comma delimited, does it have any effect? should I save it to another format?

2021-03-11T12:28:18.295800Z

I am quite convinced at this point that your input data has some type of strange character. A zero width space perhaps?

2021-03-11T12:28:34.296600Z

Try opening the csv in a byte editor or some such

2021-03-11T12:28:52.297300Z

Is the column you have issues with the first column in the csv?

2021-03-11T12:29:43.299Z

If that’s the case I think it’s some kind of encoding issue related to byte order marks or some such. Sorry for the handwaving explanation, on a phone at the moment!

2021-03-11T12:30:25.299500Z

it solved my problems after change CSV UTF-8 (comma delimited) into only csv (comma delimited), i wonder what's the difference between those? thanks a lot @dharrigan @javahippie @anders152, it wasn't because of _ or different/hanging <space> characters, but this also an alert, it's better to rename it without any symbols https://donatstudios.com/CSV-An-Encoding-Nightmare

2021-03-11T12:32:16.300500Z

Then it was probably an encoding issue with the first character in the file, or some other strange character artifact

Sithabiso Makhathini 2021-03-11T14:17:22.301800Z

I am using sente now but I still don't have access to "ws"

Mark Wardle 2021-03-11T17:33:47.307700Z

Hi all. Really enjoying using deps.edn, aliases and a standard main -M. I've also been experimenting with -X to execute arbitrary functions. In many cases, it seems possible to avoid parsing command-line arguments at all and remove a load of unnecessary code. It's turtles functions all the way down. Sometimes one simply needs one or two little configuration options. Is there any clever way of avoiding having to escape strings? eg clj -X:download :db /var/tmp/wibble is just so much nicer than clj -X:download :db '"/var/tmp/wibble"', particularly for non-clojure users of tools. I see non-escaped strings are passed as symbols so could I do something clever in the function itself? Or am I best sticking to -M:alias and parsing the command line arguments with tools.cli? Advice appreciated.

grazfather 2021-03-11T17:38:03.307800Z

if you’re writing cli consider using #babashka

Mark Wardle 2021-03-11T18:03:43.308Z

Thanks. This is for running or starting a number of applications (that include a number of java libraries) from the command line, rather than scripting tools per se.

alexmiller 2021-03-11T18:07:32.308200Z

I would stick with -X for now, we're continuing to think about the annoyance of string args and best answer there

alexmiller 2021-03-11T18:08:38.308400Z

in the code you could use (name arg) - name works with both strings and symbols so would work with either

2👍
Mark Wardle 2021-03-11T18:14:02.308600Z

That's a great idea. Thank you. It is only a minor annoyance - I must have deleted a page of redundant code because this is so convenient....

alexmiller 2021-03-11T18:16:03.308900Z

yep, hoping to continue adding to the convenience factor :)

seancorfield 2021-03-11T18:22:04.309100Z

For my deps-based apps that can be invoked via -X, I generally allow any argument that is expected to be a string to also be a symbol where that is legal but it's still a bit of a pain point. It would be nice to see changes in the underlying machinery to improve this. I can imagine that if the argument is unparsable, perhaps it could just be passed as a literal string and left for the executed function to validate/deal with?

Mark Wardle 2021-03-11T20:18:47.309500Z

I tried using name, but of course, there are special characters which are interpreted before being passed to the fn - so anything illegal as a symbol I guess is ruled out. e.g.

➜  clods git:(dev) ✗ clj -X:download :db ods-2021-03
test: got params {:db ods-2021-03}
Installing ODS index in  ods-2021-03
➜  clods git:(dev) ✗ clj -X:download :db \var\tmp\ods-2021-03
test: got params {:db vartmpods-2021-03}
Installing ODS index in  vartmpods-2021-03
➜  clods git:(dev) ✗ clj -X:download :db /var/tmp/ods-2021-03
Unreadable arg: "/var/tmp/ods-2021-03"

Mark Wardle 2021-03-11T20:22:47.309800Z

It’s fine. I’ll switch to using -M for a bit and I can print some help text etc… and I can see why this might be tricky. But I guess it depends on whether you think of this as tool for the developer of the software, or actually useful to a user of that software. If the latter, perhaps a simple options map to go along with exec-fn might work to help inform parsing/coercion? But just thinking out loud… perhaps I’m misusing the feature 🙂

fsd 2021-03-11T20:33:48.313Z

Hi There, I have this function, when I (println places) it prints an array but when I print inside the loop after [idx park] (println park) it does not println any objects. Can someone please tell me what is wrong with my code? Thanks

(defn featuresList[places]
;; (println places)
  (map-indexed (fn [idx park] (println park)  {:type "Feature" :geometry {:type "Point" :coordinates [(:Longitude park),(:Latitude park)]}} )places)
  )

dpsutton 2021-03-11T20:35:03.313500Z

> when i print inside the loop you don't have a loop. That's a lazy sequence that is unrealized so nothing is computed

fsd 2021-03-11T20:44:37.318400Z

Ohh I see @dpsutton Thanks. My goal is to create objects like below for the mapbox so that it can display Marks on the map dynamic depending on geo coordinates. Would please guide what could I use to get this working. I would like to loop through the array of object and set dynamic coordinates [(:Longitude park),(:Latitude park)] like this.

#js {:type "Feature" :geometry #js {:type "Point" :coordinates #js [-122.5257173,38.55322304]}}
#js {:type "Feature" :geometry #js {:type "Point" :coordinates #js [-121.6984631,38.11059503]}}
#js {:type "Feature" :geometry #js {:type "Point" :coordinates #js [-122.3400331,37.2016849]}}
#js {:type "Feature" :geometry #js {:type "Point" :coordinates #js [ -122.379692,37.713693]}}

alexmiller 2021-03-11T20:45:08.319100Z

I don’t think you’re using it wrong

alexmiller 2021-03-11T20:45:32.319900Z

It bugs me too, still thinking about it

1🙂
dpsutton 2021-03-11T20:46:10.320700Z

if you change your verbiage a bit you're doing that. From a collection of places, return a collection of that has this shape using the original collection. I think you've accomplished that right?

dpsutton 2021-03-11T20:46:33.321200Z

unless you're asking how to get these as js types rather than the clojurescript immutable types?

oconn 2021-03-11T20:47:31.321900Z

(defn features-list [places]
  ;; idx is unused (here) so you could use map
  (map-indexed (fn [idx park] 
                 (println park) ;; => {:Longitude -77.0364, :Latitude 38.8951}
                 #js {:type "Feature" 
                      :geometry #js {:type "Point" 
                                     :coordinates #js [(:Longitude park)
                                                       (:Latitude park)]}})
               places))

(let [places [{:Longitude -77.0364 :Latitude 38.8951}]]
  ;; This will force the lazy seq to be realized
  (prn (features-list places)) ;; => (#js {:type "Feature", :geometry #js {:type "Point", :coordinates #js [-77.0364 38.8951]}})
Could also wrap the object in clj->js

fsd 2021-03-11T20:56:21.322Z

correct, i am trying to get as js types because I created a sample app using reactjs and it worked on there. Following code in js

const featuresLst = data.map(
    park => ({
        "type": 'Feature',
        "geometry":
        {
            "type": 'Point',
            "coordinates": [park.Longitude, park.Latitude]
        }
    }))

fsd 2021-03-11T20:56:58.322200Z

let geojson = {
    type: 'FeatureCollection',
    features: featuresLst
}

fsd 2021-03-11T20:57:22.322400Z

Source id="my-data" type="geojson" data={geojson}>
<Layer />
</Source>

fsd 2021-03-11T21:00:44.322700Z

This is how I did it JavaScript, First created separate function variable just for mapping over array.

fsd 2021-03-11T21:06:51.322900Z

Hello @oconn 
Like this ?
(clj->js {:type "Feature" 
                      :geometry #js {:type "Point" 
                                     :coordinates #js [(:Longitude park)
                                                       (:Latitude park)]}})

oconn 2021-03-11T21:10:50.323100Z

yes, but you don’t need the nested #js literals then

DoubleU 2021-03-11T21:48:29.327100Z

Hi All, bit of a newbie question here, but I have a form component in my clojurescript, and that component contains a form-input as a child (code below). I’m trying to figure out how to dispatch an event back out to my component when the on-blur event is fired but not exactly sure how. With something like Angular, from within the component I could use the .emit(), in React, I could do something like onClick={someMethod} which would then call a method in my parent, but not sure how to do this in clojurescript (or if this is even possible). Can someone provide some direction?

[form-input
          (utils/merge-fn-props {:on-input #(reset! value (-> % .-target .-value))
                                 :on-focus #(reset! focus? true)
                                 :on-blur #(reset! focus? false)}...

blak3mill3r 2021-03-11T22:43:17.327800Z

@jcwright04 you are using reagent, but not re-frame, is that right?

blak3mill3r 2021-03-11T22:44:03.328300Z

you can call whatever code you want there... :on-blur #(dispatch-blur-event %)

blak3mill3r 2021-03-11T22:45:03.329100Z

if you share a bit more of the surrounding code I might be able to offer an idea for how to connect things together

blak3mill3r 2021-03-11T22:45:49.329900Z

the anonymous fn you pass as :on-blur has bindings for your lexical scope (anything you construct in a let binding in the component, for example) ... does that help?