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?
perhaps (map inc (ints))
is what you’re looking for?
yeah, I was over-complicating it in my head. thx.
you already wrote it! :)
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
#"( |NULL)"
should work. Note the leading # - that means it’s a regular expression.
Thank you @anders152, bullseye solution!
Can I update the CLI tools from the CLI tools or do I need to re-install the newer verswion over the top?
nevrmind, just installed latest.
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?That's strange, for it works for me...
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=>
Have you eval'ed myvar
yet?
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"}
not quite like that
I mean, did you type this into a repl and try first?
or are you doing this in an editor?
in editor, then i run it into repl
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
and vars are local to each namespace.
Only a guess, as I can't see what you're doing 🙂
you see in my example above, I'm in the user
namespace
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"
if you type (keys myvar)
what do you get?
this is all the keys
cleaning-adv.core> (keys myvar)
;; => (:converted_date :SubTotal :TaxAmt :Freight :TotalDue :sales_name :AreaName :CountryName)
and what happens if you do (:converted_date myvar)
?
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))
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))
?
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
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.
@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?Your keyword :converted_date
is not what it seems, the first entry would have been true
otherwise in the second call
yes, perhaps the _
is not a real underscore?
There are some chars in there, so that (= :converted_date :converted_date)
is false
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?
I am quite convinced at this point that your input data has some type of strange character. A zero width space perhaps?
Try opening the csv in a byte editor or some such
Is the column you have issues with the first column in the csv?
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!
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
Then it was probably an encoding issue with the first character in the file, or some other strange character artifact
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.
if you’re writing cli consider using #babashka
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.
I would stick with -X for now, we're continuing to think about the annoyance of string args and best answer there
in the code you could use (name arg)
- name
works with both strings and symbols so would work with either
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....
yep, hoping to continue adding to the convenience factor :)
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?
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"
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 🙂
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)
)
> when i print inside the loop you don't have a loop. That's a lazy sequence that is unrealized so nothing is computed
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]}}
I don’t think you’re using it wrong
It bugs me too, still thinking about it
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?
unless you're asking how to get these as js types rather than the clojurescript immutable types?
(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
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]
}
}))
let geojson = {
type: 'FeatureCollection',
features: featuresLst
}
Source id="my-data" type="geojson" data={geojson}>
<Layer />
</Source>
This is how I did it JavaScript, First created separate function variable just for mapping over array.
Hello @oconn
Like this ?
(clj->js {:type "Feature"
:geometry #js {:type "Point"
:coordinates #js [(:Longitude park)
(:Latitude park)]}})
yes, but you don’t need the nested #js
literals then
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)}...
@jcwright04 you are using reagent, but not re-frame, is that right?
you can call whatever code you want there... :on-blur #(dispatch-blur-event %)
if you share a bit more of the surrounding code I might be able to offer an idea for how to connect things together
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?