Fork me on GitHub
#beginners
<
2016-12-08
>
dorianc.b01:12:41

@ryanlabouve: as a beginner to programing and clojure I would go with clojure the brave and clojure from the ground up

dorianc.b01:12:57

I think joy of clojure and clojure applied are great when you want to build your own projects

ryanlabouve05:12:34

Thanks! Very eager to to get to both joy of Clojure and Clojure applied. Reading their TOC makes me want to read as fast as possible 😛 By "Clojure from the ground up" do you mean https://aphyr.com/tags/Clojure-from-the-ground-up ?

agile_geek07:12:59

:+1: for Clojure Applied.. I'm not author and @alexmiller has not bribed me!

agile_geek08:12:45

oh also :+1: for @ericnormand's courses!

curlyfry09:12:10

I went with Clojure for the Brave and True, and did the 4Clojure exercises (sorted by difficulty) at the same time as reading it. Worked well!

curlyfry09:12:14

I also think the cheat sheet is great for exploring the many clojure.core functions as they are divided into categories: http://clojure.org/api/cheatsheet

ashnur13:12:59

what does (defn ^:export run [] (...)) do?

ashnur13:12:11

i see it everywhere

ashnur13:12:54

i know it has something to do with exporting (making the function available) for something else, but for what and when it is used?

gdeer8115:12:05

@ashnur it protects the name from getting minified so you can call it by name from Javascript

roelofw16:12:11

What is the best way to test the code

roelofw16:12:34

a lot is handeling json reponses from a external api

gregnwosu16:12:24

anyone know how to make a scala.collection.Seq from a clojure list?

aghiuru19:12:43

is there an idiomatic way to do this: (fn [m k] (map #(k %) m))?

aghiuru19:12:03

of course, (fn [m k] (map k m)) what was i thinking

gdeer8119:12:03

as long as k implements the IFn interface you can do that

dexter19:12:09

isn't that just map though?

dexter19:12:36

with args reversed

aghiuru19:12:46

yeah, k is a keyword in my case

dexter19:12:04

I'm still not seeing how this isn't just map though?

aghiuru19:12:11

it's just a map

dexter19:12:21

(defn wimble [m k] (map #(k %) m))
(wimble [1 2] (partial + 1))
(map (partial + 1) [1 2])

dexter19:12:41

you're writing a function not called map that uses map to reimplement map

aghiuru19:12:42

but i thought i had to write the anon function as well

aghiuru19:12:07

i wanted to provide a lil bit of context

aghiuru19:12:30

i'm not actually writing that function, just using the map

aghiuru19:12:53

forgot that keywords implement IFn

dexter19:12:37

oh right you mean like (map :mykey [{:mykey 1} {:mykey 2}]) ; [1 2]

dexter19:12:14

yeah that whole keywords looking themselves up in a map is rather useful

jeffh-fp19:12:45

speaking of brave and true -- I'm working through chapter5 trying to implement my own assoc-in with recur:

; 3. Implement the assoc-in function. Hint: use the assoc function and
; define its parameters as [m [k & ks] v].

(defn my-assoc-in
  [m [k & ks] v]
  (loop [new-map m
         k k
         ks ks]
    (if (empty? ks)
      (assoc new-map k v)
    (recur (k new-map) (first ks) (rest ks)))))
This works for the base case of just one key (ks starts empty) but the recursion only returns the deepest level of the map where we set the value. I can't seem to figure out how to drill down to set the value like it's doing but doing that with the entire original map. Suggestions?

jswart20:12:24

Your fixed point is only going to fire when ks is empty. So its only ever going to assoc the last value.

jswart20:12:57

Ah, recur is in the else position… did not see that. But you need to be “assoc’ing” at each level. In this case you are “throwing away” the nested levels as you progress inside.

jeffh-fp20:12:36

so I can put assoc in the recur, but then how do I do that final "deep/nested" assoc?

mikepjb21:12:23

hey guys, using (apply map vector <col>) produces a list e.g (a b c) what is the equivalent to return the result as a vector?

dpsutton21:12:43

(into [] <col>)

curlyfry21:12:58

@jeffh-fp I'm actually not sure if you can use recur (which needs to be in the tail position) to do that. You'll probably want to use "classical" recursion in which you explicitly call your function

curlyfry21:12:26

So you can assoc your map key to the result of calling your function again 🙂

mikepjb21:12:11

sorry my example is bad, the original data structure is a vector of vectors

mikepjb21:12:18

[[6 0 0 0 0 0 1 5 0] [9 5 4 7 1 0 0 8 0] [0 0 0 5 0 2 6 0 0] [8 0 0 0 9 4 0 0 6] [0 0 3 8 0 5 4 0 0] [4 0 0 3 7 0 0 0 8] [0 0 6 9 0 3 0 0 0] [0 2 0 0 4 7 8 9 3] [0 4 9 0 0 0 0 0 5]]

mikepjb21:12:49

the aim is to return columns by taking the 1st position of each column then the 2nd and so forth

dpsutton21:12:03

oh so you wanted to do that

mikepjb21:12:17

to get this ([6 9 0 8 0 4 0 0 0] [0 5 0 0 0 0 0 2 4] [0 4 0 0 3 0 6 0 9] [0 7 5 0 8 3 9 0 0] [0 1 0 9 0 7 0 4 0] [0 0 2 4 5 0 3 7 0] [1 0 6 0 4 0 0 8 0] [5 8 0 0 0 0 0 9 0] [0 0 0 6 0 8 0 3 5]) but as a vector

gdeer8121:12:27

@mikepjb mapv

mikepjb21:12:46

thanks guys!

gdeer8121:12:49

there's an echo in here bowtie

dpsutton21:12:04

just a question, is the type important?

dpsutton21:12:06

and should it be?

mikepjb21:12:56

probably not, the reason why I wanted it as a vector was to use subvec on it

mikepjb21:12:03

and I don't know the list equivalent

dpsutton21:12:40

looks like its a (take n (drop m col))

gdeer8121:12:27

but subvec is cleaner and having an indexed collection is probably faster

dpsutton21:12:52

have to profile

dpsutton21:12:20

if input is a seq, maybe mapv takes a while to go to vector, so there's benefit of just realizing the sequence you want from the first

dpsutton21:12:35

and since its all lazy, you could presumably not realize the tail that's not needed

mikepjb21:12:41

it's for a sudoku solving program

dpsutton21:12:42

but vectors will realize the entirety of it

dpsutton21:12:48

but its probably not super important

dpsutton21:12:50

i was just asking

mikepjb21:12:51

thanks for the information guys!

mikepjb21:12:59

interesting to talk about it for sure

mikepjb21:12:24

I will use vector mostly because subvec is simpler

gdeer8121:12:20

@mikepjb yes, that is a good philosophy to live by

mikepjb22:12:21

(map #(contains? % 6) [[6 0 0 0 0 0 1 5 0]
                       [9 5 4 7 1 0 0 8 0]
                       [0 0 0 5 0 2 6 0 0]])

mikepjb22:12:57

I was expecting the answer to be (true false true) but instead it is (true true true)

gdeer8122:12:20

because they all contain the index 3

jeffh-fp22:12:32

thanks @curlyfry I'll give it a shot

gdeer8122:12:39

try that code with a vector that only has 1 element

mikepjb22:12:40

that was meant to be a 6!

mikepjb22:12:06

(contains? [9 5 4 7 1 0 0 8 0] 6) is true

gdeer8122:12:23

yes because that vector has a 6th index

mikepjb22:12:24

if a key is present

mikepjb22:12:36

ah thanks again @gdeer81

gdeer8122:12:39

the key of a vector is its index

gdeer8122:12:16

everyone runs into that when they're first starting out

mikepjb22:12:44

(some #{3} [3 4 5])
right?

jeffh-fp22:12:02

yep @curlyfry I got it I think:

(defn my-assoc-in
  [m [k & ks] v]
    (if (empty? ks)
      (assoc m k v)
    (assoc m k (my-assoc-in (get m k) ks v))))

curlyfry22:12:42

@jeffh-fp Looks good! 🙂

curlyfry22:12:17

@jeffh-fp If you're curious, you can look in the clojure.core implementation of assoc-in to see how they did it

jswart23:12:30

its really similar, well done!

jeffh-fp23:12:15

not sure why I felt like I had to use recur -- that had me stumped for a while but I learned a bit more about recur 🙂

jeffh-fp23:12:58

doesn't seem to work with vectors:

(def users [{:name "James" :age 26}  {:name "John" :age 43}])

;; update the age of the second (index 1) user 
(my-assoc-in users [1 :age] 44)
breaks with ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn chapter5/my-assoc-in (chapter5.clj:56)

jeffh-fp23:12:41

ah I fixed it

curlyfry23:12:44

@jeffh-fp My guess would be the (k m), since you can't do (1 [:foo :bar])

jeffh-fp23:12:55

have to use get

jeffh-fp23:12:31

well, I "fixed" it by looking at how Clojure implemented it 🙂