This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-09-22
Channels
- # beginners (104)
- # bitcoin (1)
- # boot (5)
- # clara (3)
- # cljs-dev (14)
- # cljsjs (5)
- # cljsrn (1)
- # clojure (242)
- # clojure-italy (17)
- # clojure-news (13)
- # clojure-norway (3)
- # clojure-russia (101)
- # clojure-spec (41)
- # clojure-uk (87)
- # clojurescript (38)
- # core-async (38)
- # cursive (6)
- # datomic (11)
- # defnpodcast (3)
- # docs (14)
- # editors (8)
- # events (1)
- # fulcro (7)
- # hoplon (25)
- # leiningen (4)
- # luminus (7)
- # off-topic (25)
- # onyx (1)
- # portkey (14)
- # random (1)
- # re-frame (7)
- # reagent (4)
- # rum (4)
- # schema (8)
- # shadow-cljs (257)
- # spacemacs (10)
- # specter (4)
- # unrepl (3)
- # yada (1)
Anyone suggest coding kata sized challenges (1-4 hours long) for those just starting clojure / clojurescript? Links to simple solutions a bonus. Thanks.
I always like to plot a mandlebrot or julia set when I first start in a language - you can do that in under an hour, but you can also spend more than 4 hours on it.
it does require some maths and maybe a headstart with graphics code for beginners
final solution is usually quite simple; one nested for loop over the coordinates (x & y), one iteration loop doing the (in fact quite simple) maths, and one drawing command + language-dependant overhead for actually spitting the fractal out somewhere
Morning ☀️
So, lovely UK Clojure people, I have a question… If you had this function:
(defn init-noaa-canonical-db-pt
"Command Function to create PostgreSQL databse for NOAA daily observations data - PT"
[]
(let [st-data (rest (ingest-csv-data "/rawdata/NOAA_DailySummaries_Portugal_1992_to_2016_normalised.csv"))
st-data-prepared (get-station-data-prepared st-data (:station-keys ingest-cfg))]
(doseq [row st-data-prepared]
(sql/populate-noaa-stations-pt! {:? (ordered-values-vector-from-map
(:station-keys ingest-cfg)
row)}))
(doseq [row st-data]
(sql/populate-noaa-daily-measurements-pt! {:? (ordered-values-vector-from-map
(:noaa-measurement-keys ingest-cfg)
(first (noaa-prepare-measurements-data
row)))}))))
and you wanted functions that would do the same thing, but for other database tables, would we be in macro club territory?(I am using Yesql, so there are different functions for each table Insert, i.e. sql/populate-noaa-stations-pt! as above for Portugal, and as an example sql/populate-noaa-stations-ci! for Côte d’Ivoire)
i.e. Can one use a macro to re-write the symbol of a function call or several function calls?
@conan - Not that I am aware of, and I have spent some time spelunking in the docs, but I may__ be wrong…
you could probably just read the files from your sql directory actully, the naming is based on those, right? then you could generate the symbols
In the “here and now” I am prepared to create 5 other, practically identical functions, but overall that much repetition feels wrong.
@conan - I am using the “one file” approach, with annotations / comments that name__ the queries / functions
could you put all the country codes in a db table and iterate over them with the same sql function?
Right, so because Yesql ONLY allows for parameterised replacement of values, i.e. you can’t parameterise a tablename, that would not help - using Yesql I HAVE TO have a query per table.
yeah, yesql gives you back the names of the functions it has defined in defqueries
might it be possible to look through those and find the right one?
if you have a convention?
It might be, I am not familiar with how to get that info back out, but I will re-check the docs… I am also not sure how to invoke a function using a value returned from a function… (I realise that this might seem supremely n00b-ish, but I’ve never had to do it before and I have no idea how one would go about doing it in Clojure)
(defn query-with-name [s query-vars]
(some #(when (= (:name (meta %)) n) %)
query-vars))
I am going to have to unpack that, but thanks 🙂 (full disclosure, I think I understand about 70% of that expression)
I typed it wrong which didn't help 😄, fixed now
so when you do defqueries you get the vars back
I think
(defqueries "somefile.sql") => [#'insert-country #'insert-something]
if you store those vars in a variable
(def query-vars (defqueries "somefile.sql"))
you could look in it for a var with a matching name
(defn query-with-name [s query-vars]
(some #(when (= (:name (meta %)) n) %)
query-vars))
(query-with-name "insert-country") => #'insert-country
(let [x "england"
q (query-with-name (str "insert-country-" x)] (q ...)
I see, I think… So I would run the query like this:
((query-with-name "populate-noaa-daily-measurements-pt!") valuescoll)
yeah exact;y
Just one thing… What is “n” doing / representing on the second line of that function “query-with-name”?
I tried to implement the function and I am getting an “Unable to resolve symbol…” error on “n” and so I looked at it again and I thought, huh?
It should be - also you need to wrap
(:name (meta %))
with str, like this:
(str (:name (meta %)))
else it never matches, ‘cos the result of pulling :name out of the meta result is NOT a string, it’s a symbol… 🙂ha sorry yeah was just typing without testing
I am having a data integrity problem now, with my CSV files… 😞 You approach works a treat - thanks again 🙂
programming_lyfe.txt
Basically the HUGE CSV for Indonesia has a broken field in it somewhere, and as such I am buggered. Still the other 5 countries work! 🙂
I am starting to be of the opinion that I need to migrate to HugSQL as that allows for parameter replacement of SQL Identifiers, like table names.
I don’t really want to write a macro - I mean my question boils down to “can one write a macro that will, based on input params, rewrite the invoked functions?”
i was thinking of doing the parameterisation in the SQL query inside a transaction, by loading the data from a table; I might be off here though. That said, you don't have to use Yesql for everything, in this case you could swap it out
@conan - Indeed, and I am starting to think that HugSQL is a better fit for my use cases
HugSQL would allow me to have a generalised query that can do these Inserts on any of the tables that conform to the structure of the data. All my munging functions are already generalised, and are being re-used, so as long as the CSV data is in the right format / structure I already have re-usable functions that can deal with all the CSVs / data sources.
It’s the problem of having a discrete function to INSERT into each of 6 tables that leads to the repetition.
If I swap Yesql out and put HugSQL in I have a (small) hill to climb in terms of adoption and so even temporarily having a macro seems better as I have a deadline to hit…
Workflow help: I have some code which I'm writing hand-in-hand with some extensions to a library. How best to update the library and have it reflected in the REPL (my code) that uses it?
@mattford - I am sad to say that my experience doesn’t go that deep, but that I’ve got a feeling that the lein install dance may be required…
other two options are:
decide what you need from the lib and build in isolation from the actual code
throw in an extra (ns)
form or similar on your side and write the lib code inline until it’s stablised
@conan - In case you are interested, @danieleneal has offered a really interesting take on my question above ^^ which I am certain will work and is very elegant. Ping me if you’d like to see it and I’ll send you the “solution” 🙂
Goes to read https://github.com/technomancy/leiningen/blob/master/doc/TUTORIAL.md#checkout-dependencies
@danieleneal - Just wanted to say “Thanks!” one more time - my code is working very nicely on live and is currently loading LOADS of data into my production databases 🙂
Aw thanks! Glad to hear :)