Fork me on GitHub
#beginners
<
2016-12-18
>
gowder00:12:12

I just wrote a beginner-focused blog post on how destructuring works (one of many, I know). feedback welcome! https://paultopia.github.io/posts-output/destructuring/

roelofw09:12:15

now I hope someone can think with me how to make this a function and a test.

roelofw09:12:21

``````(defn let-it-be
"Can you bind x, y, and z so that these are all true?"
[]
(let [x 7
y 3
z 1]
(and (= 10 (let x y (+ x y)))
(= 4 (let y z (+ y z)))
(= 1 (let z z)))))  ``````

roelofw09:12:09

I could do

``(defn let-it-be  "Can you bind x, y, and z so that these are all true?"  []  (let [x 7        y 3        z 1] ``

roelofw09:12:23

but then x y z are still not resolved

roelofw09:12:50

and I have no clue how to inject the solution in the test then

kokos10:12:13

is there a clojure fn that would convert

``````{:x {:y {:z 1}
:z 2} :z 3} ``````
into
``````{[:x :y :z] 1
[:x :z] 2
[:z] 3} ``````

roelofw10:12:36

@kokos have you tried partition-by ?

kokos10:12:34

@roelofw, cant say i see how i could use partition-by for what i need to accomplish

wilcov11:12:46

@kokos perhaps you can create a walker that traverses the map from top to bottom? That way you could put the keys in a vector i believe

wilcov11:12:38

It has some nice pre and post level functions as well

kokos11:12:47

``````(defn prepred [pk r [k v]]
(if-not (map? v)
(assoc r (conj pk k) v)
(reduce (partial prepred (conj pk k)) r v)))
(defn transf [x]
(reduce (partial prepred []) {} x))

(transf {:x {:y {:z 1} :z 2} :z 3})``````
probably will blow up stack for deep nested maps but works , thank you all

pvinis12:12:11

how can i make `boot repl` start in my main ns instead of the user ns?

roelofw13:12:47

some expert who can help me ?

dpsutton16:12:10

what unresolved error are you seeing?

roelofw16:12:10

At some wierd way I cannot find the function ` list-conj ` in the test file @dpsutton

dpsutton16:12:41

is it defined above your tests?

roelofw16:12:17

no, in another file which I require

roelofw16:12:48

all the other files before this where found in the tests

dpsutton16:12:51

can you tell me what the error is?

dpsutton16:12:00

exactly the error message?

roelofw16:12:20

Wierd, when running the tests there are no error messages

roelofw16:12:57

but I see on the code that the functions have another color indicating that there is a error.

roelofw16:12:13

and on the tooltip there is a non-resolved error

roelofw16:12:31

oke, problem is solved. There were some parentheses missing

roelofw17:12:09

are sets not part of core ? When I test a function I see this error message :

``java.lang.ClassNotFoundException: clojure.set ``

roelofw17:12:49

it is in a test file which only ns line is this :

``````(ns forclojure.elementary2-test
(:require [clojure.test :refer :all]
[forclojure.elementary2 :refer :all])) ``````

roelofw17:12:17

``(is (= (intro-to-sets) (clojure.set/union #{:a :b :c} #{:b :c :d})))) ``

kyle_schmidt18:12:12

@seancorfield I’m still a little confused and looking for a definite answer on where I should create my database environment. Should I create all of my db environment through my application or is this something that you typically create beforehand and your application sits on top/references?

roelofw18:12:05

is this a good way to require set :

``````(ns forclojure.elementary2-test
(:require [clojure.test :refer :all]
[forclojure.elementary2 :refer :all]
[clojure.set :refer :all])) ``````

roelofw19:12:00

I have this function :

``(= [2 4] (let [[a b c d e] [0 1 2 3 4]] __))  ``

roelofw19:12:19

I know the answer is [c e] . But when I make a function like this `(identity [c e] ) ` I see a message that [ c e] cannot be resolved

roelofw19:12:40

How can I solve this in a function which do not take any parameters

dpsutton19:12:39

why do you need a function?

roelofw19:12:52

and how to I make a function that takes two arguments `[ v m]` and `[ v, m ] ` does not work

dpsutton19:12:11

why not just `(let [[a b...][0 1 ...]] [ce])`

roelofw19:12:37

I try to make my private repo of 4 clojure challenges and I try to make one file with the solutions and one with tests

roelofw19:12:53

but maybe it is not handy here

dpsutton19:12:28

oh, so you're putting in a different file to return [c e] where c and e are not defined

dpsutton19:12:56

maybe just skip this one then

roelofw19:12:00

but as I said it not the best way to store this challenge

dpsutton19:12:01

there's really no benefit to it

dpsutton19:12:12

unless you put the whole let binding into the function

dpsutton19:12:34

why not `(= [2 4] (answers/question5)`

dpsutton19:12:12

and `(defun question5 [] (let [[a b c d e] [ 0 1 2 3 4 5]] [c e])`

roelofw19:12:38

oke, I can do that also

roelofw19:12:35

@dpsutton can you also help me with this

roelofw19:12:57

I have a function which takes a map and a value

roelofw19:12:09

How do I make this work on the parameters part `[ v m ] ` or `[ v , m ] ` does not work

roelofw19:12:34

``````(defn map-defaults
"When retrieving values from a map, you can specify default values in case the key is not found:
\n\n(= 2 (:foo {:bar 0, :baz 1} 2))\n\nHowever, what if you want the map itself to contain the default values?
Write a function which takes a default value and a sequence of keys and constructs a map."
[]
[v m]
(zipmap m (repeat v)))  ``````

joshkh19:12:44

Could someone help me understand what I'm doing wrong with Java interop? I'm trying to translate this:

``````java
``````
Into clojure...
``````clojure
``````
But I'm getting the following error `java.lang.IllegalArgumentException: No matching method found: loadTTF for class java.lang.Class` Do I need to cast the result to a PDFont?

roelofw19:12:52

I see a message on my cursive ide that v and m are not resolved

roelofw19:12:50

I saw it . I had two parameters parts, Sorry for the question

roelofw20:12:59

Why do I see this error in this test file :

``````(testing "rearrenging code"
(is ( (rearranging_code) (sort (rest (reverse [2 5 4 1 3 6]))))
(-> [2 5 4 1 3 6] (reverse) (rest) (sort) ((rearranging_code)))))  ``````

roelofw20:12:27

I try to test this function :

``````(defn rearranging_code
"The -> macro threads an expression x through a variable number of forms. First, x is inserted as the second item
in the first form, making a list of it if it is not a list already. Then the first form is inserted as the second
item in the second form, making a list of that form if necessary. This process continues for all the forms.
[]
(identity 5))  ``````

roelofw20:12:55

and the error message is this :

``actual: java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn  ``

dpsutton20:12:42

you are missing an equal

dpsutton20:12:57

you are trying to call the return value of `rearranging_code` which is 5

dpsutton20:12:32

also, note that `(identity x)` is x. so the function doesn't do anything, so you can just return x instead of `(identity x)`

dpsutton20:12:03

and you call rearranging code twice, once so that it is the head of the list so therefore a function in the first form after the is, and second at the end of a threading macro but you are invoking it and threading the result, which is 5

dpsutton20:12:13

and your is form has no test in it like equals

dpsutton20:12:30

use the repl to figure out what the forms should look like

roelofw21:12:11

@dpsutton can you give me a hint where the = must be .

roelofw21:12:22

I tried

``````(testing "rearrenging code"
(is ( =  (rearranging_code) (sort (rest (reverse [2 5 4 1 3 6]))))
(-> [2 5 4 1 3 6] (reverse) (rest) (sort) ((rearranging_code))))) ``````

roelofw21:12:42

and

``````(testing  "rearrenging code"
(is (   (rearranging_code) ( = (sort (rest (reverse [2 5 4 1 3 6]))))
(-> [2 5 4 1 3 6] (reverse) (rest) (sort) ((rearranging_code))))))  ``````

roelofw21:12:49

but both does not work

dpsutton21:12:36

you're asserting that two things are equal

dpsutton21:12:42

`(is (= A B)`

dpsutton21:12:48

or `(is true)`

dpsutton21:12:55

so pass to the is form something that is true

dpsutton21:12:10

`(is (= (my-crazy-contraption) (their-crazy-contraption)))`

roelofw21:12:27

then this schould be working I think

``````(testing "rearrenging code"
(is ( =  (rearranging_code) (sort (rest (reverse [2 5 4 1 3 6])))
(-> [2 5 4 1 3 6] (reverse) (rest) (sort) ((rearranging_code)))))) ``````

roelofw21:12:54

but still the same error message : ` actual: java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn `

dpsutton21:12:18

((rearranging_code)) = (5) => cannot cast long to function

dpsutton21:12:24

you are calling (5) which is meaningless

dpsutton21:12:38

also, based on indentation, your equal doesn't include the second form with the threading macro

roelofw21:12:16

sorry, I do not understand what you mean

dpsutton21:12:00

`(is (= thing thing) ... more stuff to the is that are not in the equal form)`

roelofw21:12:01

when I do only `rearranging_code` then it will also not be 5 because the function is not applied to my knowlegde

dpsutton21:12:24

can you call rearranging code from the repl?

dpsutton21:12:28

it should return five

dpsutton21:12:39

it is `(identify 5)` from your definition above

roelofw21:12:47

yes, and I need ( is (= thing thing thing))

dpsutton21:12:23

ah then the identatoin is misleading

roelofw21:12:40

no, it is `(identity 5) ` not `(identify 5) `

dpsutton21:12:20

yes, which returns 5

dpsutton21:12:24

evaluate that on the repl

roelofw21:12:37

I did already and it does

roelofw21:12:53

and this does also returns 5 `(sort (rest (reverse [2 5 4 1 3 6]))) `

dpsutton21:12:25

(sort (rest (reverse [2 5 4 1 3 6]))) (1 2 3 4 5)

dpsutton21:12:30

how could it?

roelofw21:12:34

even as this : `(-> [2 5 4 1 3 6] (reverse) (rest) (sort)) `

dpsutton21:12:37

it reverses it, takes rest

dpsutton21:12:41

and then sorts the vector

roelofw21:12:41

oke, back to the beginning : this is the challenge I have solved

roelofw21:12:47

``````(= (__ (sort (rest (reverse [2 5 4 1 3 6]))))
(-> [2 5 4 1 3 6] (reverse) (rest) (sort) (__))
5) ``````

roelofw21:12:06

the answer is `last`

dpsutton21:12:03

looks like it

dpsutton21:12:51

instead of returning `(identity 5)` why not just return `last` from your function then

roelofw21:12:11

I tried to make a function that returns last

roelofw21:12:20

but I did not get it work

roelofw21:12:47

so I thought I make a test where I test that both returns 5

roelofw21:12:06

then I could make a function that returns 5

dpsutton21:12:38

`(defn returns-last [] last)`

dpsutton21:12:10

defn returns-last [] last) #'cljc-bug.core/returns-last cljc-bug.core> ((returns-last) [1 2 3 4 5]) 5

roelofw21:12:51

Thanks, and now this test is also working

``````(testing "rearrenging code"
(is (= ( (rearranging_code) (sort (rest (reverse [2 5 4 1 3 6]))))
(-> [2 5 4 1 3 6] (reverse) (rest) (sort) ((rearranging_code)))
5))) ``````

roelofw21:12:40

Finnaly I can put al my rewritten challenges to github

roelofw21:12:48

and can make new ones

roelofw21:12:12

@dpsutton thanks, you saved my day