Fork me on GitHub
#beginners
<
2015-08-28
>
samueldev03:08:43

i ran across some very not DRY code in a codebase im involved with

samueldev03:08:16

trying to reason about a way to improve it based on my limited clj knowledge but having no luck

samueldev03:08:22

curious to see an experienced persons opinion

potetm03:08:52

@samueldev: You have a specific question?

samueldev03:08:01

yeah lol getting a gist together

samueldev03:08:15

contrived examples ftw!

potetm03:08:06

(map (partial ->AddVegetable vegetable-id) [->SquashInfo ->GreenAppleInfo …])

samueldev03:08:13

so I want default-vegetables to be a vector, each element therein being the result of calling schema/map->AddVegetable on a different vegetable

samueldev03:08:26

so map fo sho

samueldev03:08:27

figured that much

samueldev03:08:38

dont know what partial is, pr what the -> syntactic sugar is

samueldev03:08:40

to the docs!! simple_smile

potetm03:08:08

->AddVegetable and map->AddVegetable are functions that are created automatically when you create AddVegetable records.

potetm03:08:43

I’m gonna guess schema has an equivalent schema/->AddVegetable function

potetm03:08:37

Wait, sorry, you wanted something more like (map (partial ->AddVegetable vegetable-id) [(->SquashInfo) (->GreenAppleInfo) …])

samueldev03:08:50

i thought the function was named map->AddVegetable

potetm03:08:51

i.e. create each info before you map over them.

samueldev03:08:54

but it just has AddVegetable

potetm03:08:40

So, AddVegetable is the record. Clojure automatically makes map->AddVegetable and ->AddVegetable functions for you. You can also instantiate the class directly with (AddVegetable.), but that’s generally discouraged.

samueldev03:08:39

AddVegetable is defined as such

samueldev03:08:48

(deftyped AddVegetable)

samueldev03:08:56

looking at docs for that too...

samueldev03:08:29

sorry, deftyped is a macro..

samueldev03:08:37

not to be confused with deftype

samueldev03:08:40

i need a coffee.

potetm04:08:41

or scotch simple_smile

tjg10:08:21

What’s the best way to convert a sequence/collection into a list? This is the only way I know which seems guaranteed to work:

(->> my-seq-or-coll
     (into (list))
     (into (list)))

tjg10:08:07

(I have to do (into (list)) twice, because each reverses order.

nikp11:08:37

You could try (apply list my-seq-or-coll) to maintain order

[UNUSED ACCOUNT]11:08:33

Also, why would you need to do that?

sergeylaptev12:08:33

Hi all! I'm making queries via clojure.jdbc, and when I've printed response I see that all hashes are converted in to #object[java.util.HashMap 0x3bc3ff69 {93=37, 91=38, 99=65, 89=44}

sergeylaptev12:08:11

But, if I make request instantly in repl I get correct response without converting

sergeylaptev12:08:45

{"93" "37", "91" "38", "99" "65", "89" "44"}

sergeylaptev12:08:53

What is the problem here?

sergeylaptev12:08:33

Looks like that there is difference between println displaying and if we directly select symbol with reponse inside

nikp12:08:23

@djanus: with list you are creating a new list with each of the items included. So for example, doing (list '(1 2 3)) would eval to ((1 2 3)), a 1-item list of a seq. apply essentially reduces your collection into a series of arguments -- you are basically making it into (list 1 2 3) which would eval to (1 2 3). Though after playing around a bit you can create a PersistentList using seq on a seq or coll. It's kind of what list* does (http://conj.io/store/v1/org.clojure/clojure/1.7.0/clj/clojure.core/list*)

tjg12:08:25

@nikp Ahh good point!

tjg12:08:38

(Oops accidentally hit enter too quick.)

tjg12:08:43

@djanus: Because I was playing with writing an interpreter in the browser: http://www.lispcast.com/the-most-important-idea-in-computer-science ... and I wanted to ensure I’m maintaining a list. But maybe that’s silly of me, dunno. I could treat seqs like lists.