Fork me on GitHub

Hey folks! Can someone explain me how the de-structuring is happening in the let block?


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 simple_smile


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 ( 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


e.g. lein new chestnut my-app --snapshot


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


Thanks so much @plexus simple_smile


Do you know if the REPL can be used from within Cursive at all? (i.e. when switching between CLJS/CLJ)


I know people have used it from Cursive, although I'm not a cursive user myself


chestnut uses a standard nREPL based REPL, so I would expect it to "just work"


thanks, I shall explore and find out


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


ah blob is an actual type


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


by marshalling you mean when i read-string?


Or pr-str, yeah


or the opposite


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


and read-string on the way back


Yeah. You’ll probably want to use clojure.edn/read-string instead of clojure.core/read-string tho


Ok, for aforementioned performance reasons?


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?


I just used varchar for every other field


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


Ok thanks very much!


hey @sooheon: postgres also has an "hstore" type that can be used for nested key-value pairs:


@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?

Drew Verlee22:01:21

So i just learned about the expression problem ( 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...


has anyone used fontawesome in their development?


@jonahbenton: please ignore my last, user error :p


anyone up for discussing 4clojure problem 96? beauty is symmetry


not understanding the definition of symmetry in the context of the problem


@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.


@jcomplex: i’ve used font awesome; whats up?


@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


let me look really quick at my template


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