This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-01-31
Channels
- # architecture (5)
- # beginners (35)
- # boot (150)
- # cider (1)
- # clara (7)
- # cljs-dev (131)
- # cljsrn (10)
- # clojure (76)
- # clojure-austin (3)
- # clojure-berlin (1)
- # clojure-brasil (1)
- # clojure-chicago (2)
- # clojure-dusseldorf (1)
- # clojure-italy (30)
- # clojure-nl (2)
- # clojure-russia (40)
- # clojure-serbia (2)
- # clojure-spec (25)
- # clojure-uk (13)
- # clojured (2)
- # clojurescript (106)
- # core-async (29)
- # datascript (65)
- # datomic (38)
- # emacs (8)
- # funcool (8)
- # hoplon (6)
- # jobs (3)
- # klipse (93)
- # luminus (16)
- # lumo (4)
- # off-topic (2)
- # om (11)
- # onyx (13)
- # pedestal (4)
- # protorepl (3)
- # re-frame (40)
- # reagent (31)
- # ring (6)
- # ring-swagger (4)
- # slack-help (5)
- # spacemacs (13)
- # untangled (17)
- # vim (2)
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!
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 >.<
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
😕
I was afraid of that. alright.
If data
is a hash map, look at reduce-kv
it is actually. I'm listening...
(reduce-kv (fn [m k v] (if v (assoc m (keyword type (name k)) v) m)) {} data)
gives you a hash map back directly.
(assuming you want to omit v
false
values too?)
hmm actually no just specifically nil. I'm inserting into datascript, and false
is actually a proper value
Sometimes the paradigm shift from Javascript makes my brain hurt ever so slightly
Then (if (some? v) (assoc …) m)
inside that expression.
some?
is the opposite of nil?
Yes I thankfully figured that one out.
looks better than (complement nil?)
Actually (filter some? (map (fn [[k v]] (if (some? v) [(keyword "message" (name k)) v])) message))
isn't adding too many parentheses >.<
Are you then turning that sequence of pairs into a hash map again?
Let me show you the function, that might simplify things
(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)))
))
(also, when
is more idiomatic than an if
with no “else expression”)
I'm basically turning an existing hashmap into one I can insert into datascript
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}
I’d probably write (reduce-kv (fn [m k v] (if v (assoc m (keyword type (name k)) v) m)) {:db/id -1} data)
(for inserts
)
Hmm. Yes, but you'd also understand that line if you read it in a week though. I wouldn't ^_^
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.
(also (ffirst (:tx-data result))
== (first (first (:tx-data result)))
— not sure how you’d feel about that?)
hahaha, There's always a little trick isn't there 😄
clojure.core
is full of great tricks! 🙂
I'm starting to learn them, one by one. ffirst
is intuitive enough that I can safely use it now
Well then, don't let me keep you! Thanks again Sean 😄
I love reduce
and reduce-kv
, they are my go-to tools whenever I’m transforming a map conditionally
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.
And might I say, it's always refreshing in clojure that these things are available for more than one data type