Fork me on GitHub
#beginners
<
2023-01-25
>
Naor Yaakov Harel10:01:57

Hi! I'm trying to get data from a database (PostgresSQL), and I getting back each row with a function, how I can receive only the value? my code: (def db {:dbtype "XXX" :dname "XXX" :host "XXX" :user "XXX" :port "XXX" :password "XXX"}) (def ds (jdbc/get-datasource db)) (jdbc/execute! ds ["select * from my-table"])) It return: {#my-table-name {key val, key val,..] {#my-table-name {key val, key val,..] {#my-table-name {key val, key val,..] .... ??

daveliepmann10:01:01

What jdbc lib are you using? If next.jdbc then perhaps you want one of the options from https://github.com/seancorfield/next-jdbc#usage > you can specify {:builder-fn rs/as-arrays} and produce a vector with column names followed by vectors of row values. rs/as-maps is the default for :builder-fn but there are also rs/as-unqualified-maps and rs/as-unqualified-arrays if you want unqualified :<column> column names (and there are also lower-case variants of all of these).

✌️ 2
Naor Yaakov Harel11:01:16

Thank you Dave!

💯 2
seancorfield16:01:44

I would strongly recommend you get used to qualified keys in maps -- they are idiomatic in Clojure.

seancorfield16:01:53

user=> {:foo/bar 1 :foo/quux "two"}
#:foo{:bar 1, :quux "two"}
☝️:skin-tone-2: Not a function -- that's just shorthand notation for the hash map with qualified keys

🙏 2
slk50012:01:42

How to flatten parent-child map? I have: [{"name" "father", "children" [{"name" "son", "children" [{"name" "grandson" "children []"}]]}}] And I need flat structure like this: [{"name" "father", "children" []} {"name" "son", "children" []} {"name" "grandson" "children []"}]

slk50013:01:26

that was exactly what I was looking for, thx. I probably missed the solution when I was reading it prev

patclanett18:01:47

Hey, everyone! Working on a kind of coding challenge where I implement sorting of a given vector of integers without using the built in sort. Thought I had a good thought going until I hit this roadblock! Here's my code first: (defn loop-sort [initial-coll] (loop [remaining-coll initial-coll final-coll []] (if (empty? remaining-coll) final-coll (recur (rest remaining-coll) (into final-coll (vector (apply min remaining-coll))))))) Running this with an input of [2 3 1 4 5] gives me an output of [1 1 1 1 5]. An input of [2 3 4 5 1 6 9 8 7] gives me an output of [1 1 1 1 1 6 7 7 7]. It's not obvious to me why exactly this is happening, but I expect it to be a problem stemming from using apply. What am I doing wrong here? Happy to answer any questions on anything unclear, as well!

adam-james18:01:46

Hello! This seems like a fun exercise 🙂 I think it would help if you considered the shape of what you’re passing into each step of your loop. One way to gain some insight is to use a println in a version of your function at the repl. For example:

(defn loop-sort-with-println
  [initial-coll]
  (loop [remaining-coll initial-coll
         final-coll []]
    (println remaining-coll) ;; add this here, and run a small sample
    (if (empty? remaining-coll)
      final-coll
      (recur (rest remaining-coll) (into final-coll (vector (apply min remaining-coll)))))))

dpsutton18:01:24

your algorithm is to kick out the first item of remaining-coll (rest remaining-coll) and add in the lowest element of remaining-coll. Which means you choose one element and discard a random element, not the element you chose

dpsutton18:01:17

so sorting [2 3 1 4 5]: • [1] and [3 1 4 5][1 1] and [1 4 5][1 1 1] and [ 4 5] etc

patclanett20:01:32

Oh, that makes sense now! Thanks!

Benjamin J. Thompson20:01:15

Is http://clojuredocs.org the closest thing to Haskell's Hoogle? I want to know where to park my browser for when I need to find appropriate functions and libraries for the things I want to do.

didibus23:01:37

You can also use apropos from the repl

metapredicate00:01:24

I know your message was answered, but I thought I should share. I asked this previously in this same channel and got some responses.

❤️ 2
skylize19:01:06

Re-find will not help you find any 3rd party libraries. No fancy lookup-by-arguments or anything like that, but this will help for locating relevant libraries. https://phronmophobic.github.io/dewey/search.html