Fork me on GitHub
#beginners
<
2016-01-27
>
udit11:01:13

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

delaguardo11:01:48

Best destructuring tutorial

agile_geek11:01:01

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

agile_geek11:01:28

The easiest way to learn this is to play with it in a REPL

udit11:01:21

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

udit11:01:48

Also @delaguardo Thanks! I have bookmarked the link and going through it.

agile_geek11:01:23

Have a look at the tutorial but basically destructuring only works in binding forms like let so it won't be valid outside.

agile_geek11:01:07

(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

udit12:01:49

Thanks for the help 👍 I hope things will appear natural when I practice them over and over simple_smile

nigel12:01:46

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?

plexus12:01:21

Hi @nigel, yes Chestnut is a good starting point if you want a somewhat complete setup that works out of the box

plexus12:01:02

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

plexus12:01:53

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

plexus12:01:16

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

plexus12:01:51

ping me here or drop into the mailing list if you have any questions http://clojureverse.org/c/chestnut

nigel12:01:41

Thanks so much @plexus simple_smile

nigel12:01:11

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

plexus12:01:39

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

plexus12:01:15

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

nigel12:01:35

thanks, I shall explore and find out

sooheon21:01:15

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?

donaldball21:01:21

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

donaldball21:01:44

Otherwise, you might want to look at postgres’s native json type

sooheon21:01:09

donaldball: Assuming i dont need to look into it, by blob of edn do you mean just a varchar string that looks like edn?

donaldball21:01:45

text or blob types would probably be better, but yeah, that’s the gist

sooheon21:01:02

ah blob is an actual type

donaldball21:01:26

Note edn is actually pretty darn slow to marshall, so if performance is a factor, fressian or transit will be useful to you

donaldball21:01:45

But if not, edn is definitely more scannable to the eyes

sooheon21:01:50

by marshalling you mean when i read-string?

donaldball21:01:55

Or pr-str, yeah

sooheon21:01:56

or the opposite

donaldball21:01:30

Exactly. Marshaling means converting to and from a different representation, generally a serialized form.

sooheon21:01:04

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

sooheon21:01:14

and read-string on the way back

donaldball21:01:51

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

sooheon21:01:04

Ok, for aforementioned performance reasons?

donaldball21:01:26

No, clojure.edn/read-string is restricted to edn, while clojure.core/read-string reads clj, a superset of edn

sooheon21:01:07

Ok. Also, could you elaborate a bit on the differences between varchar text and blob?

sooheon21:01:20

I just used varchar for every other field

donaldball21:01:32

varchar is usually limited to a (smallish) max length

donaldball21:01:42

text stores a (large) number of characters

donaldball21:01:46

blob stores a (large) number of bytes

donaldball21:01:08

I haven’t used postgres in a dog’s age, so ymmv, but that’s the general breakdown

sooheon21:01:09

Ok thanks very much!

jonahbenton22:01:39

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/

sooheon22:01:22

@jonahbenton: thanks I’ll look at this

sooheon22:01:42

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

jcomplex22:01:13

has anyone used fontawesome in their development?

sooheon22:01:08

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

chadhs22:01:54

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

chadhs22:01:16

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

jeff.engebretsen22:01:30

@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

chadhs22:01:08

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

jcomplex22:01:11

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

chadhs22:01:49

@jcomplex good Q, i’m not sure, i’ve just usually included it in my main template (via a CDN url)

chadhs22:01:13

i’m using it in my blog which is powered by a static site generator built with clojure

chadhs22:01:23

let me look really quick at my template

jonahbenton22:01:35

hey @drewverlee so, yes, datatypes are different from vectors and lists.

chadhs22:01:40

@jcomplex: yeah i’m just including it in my base.html selmer template (should work the same if using hiccup or any other templating)

chadhs22:01:51

^screenshot

chadhs22:01:21

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

jonahbenton22:01:15

@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

jonahbenton22:01:03

@drewverlee raw maps can also play this role; defrecord offers a little more precision.

jonahbenton22:01:14

and also plain old java types also play this role.

jonahbenton23:01:24

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