Fork me on GitHub
#beginners
<
2017-01-31
>
hoopes00:01:51

hi - I'm looking for a simple example of how to model what used to be sql tables in datomic. for example, say i currently have an account table with many-to-many with a roles table. i'm looking for things like "how do i submit a map, and get the id back for the record (i know, entity) that was created" and "how do i do things like select * from account where id=1". i've run through http://learndatalogtoday.com (and i gotta do it again, at least once) , but didn't get the select * version out of it. any help or pointers in this direction would be immensely appreciated. (or directions to another slack room for datomic beginners...). THANKS!

eslachance01:01:13

I'm going to ask a much more noobish question here. Let's say (map (fn [[k v]] [(keyword type (name k)) v]) data).... but I only want to return items where v is not nil. What's the easiest way to fit it in that map? Still a little confused about the particulars of Clojure >.<

eslachance01:01:07

I tried (map (fn [[k v]] (if (some? v) [(keyword "message" (name k)) v])) message) but that actually just returns nil when k is nil 😕

mruzekw01:01:41

Map is a one-to-one operation. Filter the collection afterwards, or use a reduce

eslachance01:01:57

I was afraid of that. alright.

seancorfield01:01:20

If data is a hash map, look at reduce-kv

eslachance01:01:11

it is actually. I'm listening...

seancorfield01:01:19

(reduce-kv (fn [m k v] (if v (assoc m (keyword type (name k)) v) m)) {} data) gives you a hash map back directly.

seancorfield01:01:54

(assuming you want to omit v false values too?)

eslachance01:01:22

hmm actually no just specifically nil. I'm inserting into datascript, and false is actually a proper value

eslachance01:01:00

Sometimes the paradigm shift from Javascript makes my brain hurt ever so slightly

seancorfield01:01:02

Then (if (some? v) (assoc …) m) inside that expression.

seancorfield01:01:33

some? is the opposite of nil?

eslachance01:01:53

Yes I thankfully figured that one out.

eslachance01:01:08

looks better than (complement nil?)

eslachance01:01:07

Actually (filter some? (map (fn [[k v]] (if (some? v) [(keyword "message" (name k)) v])) message)) isn't adding too many parentheses >.<

seancorfield01:01:45

Are you then turning that sequence of pairs into a hash map again?

eslachance01:01:05

Let me show you the function, that might simplify things

eslachance01:01:08

(defn insert! [data, type]
          (let [inserts (into {:db/id -1} 
                              (filter some? (map (fn [[k v]] [(keyword type (name k)) v]) data)))
                result (d/transact! conn [inserts])]
            (first (first (:tx-data result)))
            ))

seancorfield01:01:09

(also, when is more idiomatic than an if with no “else expression”)

eslachance01:01:44

I'm basically turning an existing hashmap into one I can insert into datascript

eslachance01:01:15

Where "data" is something like

{:pinned false,
 :mention_everyone false,
 :content "with?",
 :type 0,
 :channel_id "274707618222833674",
 :id "275380583893434368",
 :timestamp "2017-01-29T21:44:07.524000+00:00",
 :nonce "275380580646912000",
 :edited_timestamp nil,
 :tts false}

seancorfield01:01:18

I’d probably write (reduce-kv (fn [m k v] (if v (assoc m (keyword type (name k)) v) m)) {:db/id -1} data)

eslachance01:01:07

Hmm. Yes, but you'd also understand that line if you read it in a week though. I wouldn't ^_^

eslachance01:01:12

Perhaps in a year I'll look back at my noobishness and end up changing it to yours but not yet. Brain still in adaptation period.

seancorfield01:01:31

(also (ffirst (:tx-data result)) == (first (first (:tx-data result))) — not sure how you’d feel about that?)

eslachance01:01:09

hahaha, There's always a little trick isn't there 😄

seancorfield01:01:22

clojure.core is full of great tricks! 🙂

eslachance01:01:59

I'm starting to learn them, one by one. ffirst is intuitive enough that I can safely use it now

eslachance01:01:29

Well then, don't let me keep you! Thanks again Sean 😄

singen15:01:10

I love reduceand reduce-kv, they are my go-to tools whenever I’m transforming a map conditionally

eslachance16:01:44

I mean, really, map, reduce and filter are just 3 of the things you should know as a programmer. They're just basically syntactic sugar on top of loops but they make beautiful code.

7h3kk1d17:01:25

I’d say they’re fundamentally simpler than loops

eslachance17:01:08

And might I say, it's always refreshing in clojure that these things are available for more than one data type