This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-01-27
Channels
- # alda (21)
- # announcements (7)
- # beginners (70)
- # boot (95)
- # braid-chat (28)
- # bristol-clojurians (2)
- # cider (22)
- # clara (4)
- # cljsjs (13)
- # cljsrn (40)
- # clojure (93)
- # clojure-argentina (1)
- # clojure-art (1)
- # clojure-miami (3)
- # clojure-norway (1)
- # clojure-portugal (2)
- # clojure-russia (39)
- # clojure-sg (3)
- # clojurescript (25)
- # clojurian-chat-app (4)
- # community-development (5)
- # conf-proposals (20)
- # cursive (48)
- # datomic (39)
- # devcards (5)
- # dirac (4)
- # editors (2)
- # events (11)
- # funcool (65)
- # hoplon (95)
- # jobs (12)
- # ldnclj (4)
- # lein-figwheel (2)
- # leiningen (1)
- # om (311)
- # onyx (20)
- # philosophy (4)
- # proton (41)
- # re-frame (83)
- # reagent (49)
- # ring-swagger (3)
- # spacemacs (8)
- # yada (5)
Best destructuring tutorial
@udit; In the example above you are dereferencing the var a
to get the map it contains. The :keys
keyword is trying to pattern match keywords that match the symbols height
, weight
and bmi
and if they match will get their values from the map and bind them to local vars associated with those symbols. Any symbol that doesn't match a keyword (like bmi in this example) will have a value of nil. The whole map is also bound to data
. All of these symbols are local to the scope of the let
. BTW this map destructuring works in other binding forms (like loop
for example)
The easiest way to learn this is to play with it in a REPL
@agile_geek: I understand the simpler bindings in let
and loop
. I saw this example in the Reagent’s wiki page and couldn’t make out its head from tail. I am still not clear on how this is working. Typically I break down things into smaller steps, but this here is not making any sense if I take out things from the let
binding.
Also @delaguardo Thanks! I have bookmarked the link and going through it.
Have a look at the tutorial but basically destructuring only works in binding forms like let
so it won't be valid outside.
(let [my-map {:name "Chris"}]
(let [{:keys [name]} my-map]
(println name)))
Will match the symbol name
in the inner let
to the keyword :name
in my-map
and bind the value "Chris"
to the var name
. So this will print Chris
Thanks for the help 👍 I hope things will appear natural when I practice them over and over
Hi all For a web app, I’m slightly frustrated at reading about assorted settings for project.clj rather than getting on with putting things together - is Chestnut (https://github.com/plexus/chestnut) a good starting point?
Hi @nigel, yes Chestnut is a good starting point if you want a somewhat complete setup that works out of the box
I recommend using --snapshot
to get the latest version, the current stable version is a bit out of date, and has a lot of boilerplate that is no longer needed
everything should still work the same, except that we now leverage the Figwheel ring server, so your app runs at localhost:3449
, and not on localhost:10555
as in older versions
ping me here or drop into the mailing list if you have any questions http://clojureverse.org/c/chestnut
Do you know if the REPL can be used from within Cursive at all? (i.e. when switching between CLJS/CLJ)
Hey guys, this isn’t completely a clojure question, but thought people here might know: when I’m representing some data in clojure as a hash-map of key/value pairs, this matches well to a table with columns/vals. However, when one key holds its own map, or is a list that holds more maps, and so on, what is the best way to represent this in a sql db (postgres)? What is the equivalent nested data type?
The simplest answer is a blob of edn or fressian or transit or the like, but only if you don’t need to query into it
Otherwise, you might want to look at postgres’s native json type
donaldball: Assuming i dont need to look into it, by blob of edn do you mean just a varchar string that looks like edn?
text or blob types would probably be better, but yeah, that’s the gist
Note edn is actually pretty darn slow to marshall, so if performance is a factor, fressian or transit will be useful to you
But if not, edn is definitely more scannable to the eyes
Or pr-str, yeah
Exactly. Marshaling means converting to and from a different representation, generally a serialized form.
So all I have to do is set the type to BLOB in the postgres table, and when I give it in, just pr-str on the whole collection
Yeah. You’ll probably want to use clojure.edn/read-string instead of clojure.core/read-string tho
No, clojure.edn/read-string is restricted to edn, while clojure.core/read-string reads clj, a superset of edn
Ok. Also, could you elaborate a bit on the differences between varchar text and blob?
varchar is usually limited to a (smallish) max length
text stores a (large) number of characters
blob stores a (large) number of bytes
I haven’t used postgres in a dog’s age, so ymmv, but that’s the general breakdown
No problem
hey @sooheon: postgres also has an "hstore" type that can be used for nested key-value pairs: http://www.craigkerstiens.com/2013/07/03/hstore-vs-json/
@jonahbenton: thanks I’ll look at this
@jonahbenton: On an unrelated note, one of my columns for a user activity table is user_id
of the type UUID. These don’t need to all be unique, because a user can have more than one activity, but pg gives me this:
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "activities_user_id"
Detail: Key (user_id)=(fc7eab4f-00c4-4479-8711-105e0aba52c8) already exists.
Do you know how I can use a value of type UUID without a unique constraint?So i just learned about the expression problem (http://c2.com/cgi/wiki?ExpressionProblem). One of the high level take away was that 'its hard to add data types in a functional language'. I understand Clojure has two concepts 'protocols' and 'multimethods' that address this specifically. However, i'm curious in general how often you need to define new data types in clojure programming. Maybe i'm not clear on what a 'data type' is. I assume its something like a vector or list...
@jonahbenton: please ignore my last, user error :p
@drewverlee: There's a deftype function that will create a Java Class. I think of it as a more formal map. But I'm still new and haven't had a ton of uses for types. https://clojuredocs.org/clojure.core/deftype
@chadhs: I have used it many times in rails is it the same with Clojure? Do I have to have a local copy of all lib or can I use an CDN? I noticed this [org.webjars/font-awesome "4.4.0"] does that do anything on it's own?
@jcomplex good Q, i’m not sure, i’ve just usually included it in my main template (via a CDN url)
i’m using it in my blog which is powered by a static site generator built with clojure
hey @drewverlee so, yes, datatypes are different from vectors and lists.
@jcomplex: yeah i’m just including it in my base.html selmer template (should work the same if using hiccup or any other templating)
@jcomplex though including it in your project.clj like you were showing could be beneficial since you’d have it “locally” to your app. probably not a showstopper either way.
@drewverlee and one uses them extensively in clj. defrecord is the most common tool for creating what the Expression Problem description refers to as a datatype. It's basically an item or object that is a "bag" of attributes
@drewverlee raw maps can also play this role; defrecord offers a little more precision.
and also plain old java types also play this role.
in clojure terms, datatypes "participate" in protocols, and re: the Expression Problem, in clojure, can be defined to do so without having a big case statement matching a datatype to a particular behavior, or having to modify existing functions to accept new participating types