Fork me on GitHub
#beginners
<
2016-12-17
>
justinr02:12:47

Hey all, I could use some assistance with a function I'm writing. The input is a string containing digits (e.g. "12345") and an index; output is a the digit in the string at that index. E.g., (get-digit "12345" 2) --> 3.

justinr02:12:58

I have written this as (defn get-digit [x k] (read-string (.substring x k k+1)))

justinr02:12:23

However, this results in `Unable to resolve symbol: k+1 in this context ...`

justinr02:12:56

Have I missed something fundamental here?

chromaticgliss02:12:06

Addition like that is done (+ k 1) probably some other programming experience bleeding into your clojure learning 🙂

justinr02:12:53

Thank you, again!

chromaticgliss02:12:04

by the way, (get "asdf" 2) would return \d

chromaticgliss02:12:29

not sure what the context of your function is though, so that may not be helpful haha

seancorfield03:12:59

That was unexpected...

seancorfield03:12:59

Anyways, per @chromaticgliss you could just use the built-in function get @justinr : (get “12345” 2) will give you “3"

seancorfield03:12:28

(works in my REPL, doesn’t work in clojurebot for some reason)

chromaticgliss03:12:30

bot seems under the weather 🙂

seancorfield03:12:39

I was a bit surprised that (get “12345” 2) didn’t produce \3 (the character) which is why I went and tried it myself.

seancorfield03:12:46

Ah, it does, my REPL was buggy.

chromaticgliss03:12:48

Ah, yeah... I guess I should have written it as a character earlier

seancorfield03:12:12

You get the third character of the string. I wonder if @justinr wants the digit value?

seancorfield03:12:09

(it’s well past beer o’clock on Friday so I’m off!)

jussi15:12:54

What does error Wrong number of args (2) passed to: core/assoc—4371 mean?

jussi15:12:10

(defn create-session! [{:keys [params session]}]
  (if-let [user (db/get-user-by-username params)]
    (-> (if (= true (hashers/check (:pass params) (:pass user)))
          ((response/found "/")
           (assoc :session (assoc session :user (:id user))))
          ((response/found "/")
           (assoc :flash (assoc params :errors '("Wrong username or password."))))))))
throws it

roelofw17:12:44

A few days ago there was a page with projects like a blog, a weather page and more for beginners to make for a resume. Does anyone know the url of that page ?

ceales18:12:20

Hi, a quick question of style - I hope this is the correct place?

ceales18:12:55

I’m implementing the partition function from quicksort and the return of the function is the obvious 3 values

ceales18:12:11

1) Those element less-than-or-equal the pivot (a vector)

ceales18:12:19

2) The pivot value (an atom)

ceales18:12:32

3) The elements that are greater than the pivot

ceales18:12:17

Should I return those 3 values as a map with keyword keys, or should I just use a vector with the meaning implicit in the position of the value in the vector?

ceales18:12:47

I would like to use the map, but I’m worried about the efficiency of making lots of little maps. and the destructuring seems less natural

donaldball20:12:02

For expressivity, you can’t beat a map. For a private helper fn, I myself would probably reach for a vector. If you’re wanting to optimize for performance, you probably need to look at transients or native arrays. But if you really need to optimize for performance, you’re probably better off looking at java.util.Arrays#sort anyway.

roelofw21:12:42

How can I make this in a function and a test :

(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)))))  

roelofw21:12:22

I tried :

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

roelofw21:12:15

and as test :

(testing "let-it-be"
    (is (= 10 (let-it-be x y (+ x y))))  

roelofw21:12:36

but then I see a lot of not resolved errors

ceales21:12:18

@donaldball thanks, I’m doing a bit of both and I’m probably doing what you suggest.

ceales21:12:03

I’ll link you the blog post when I’m done and maybe you could cast an eye over it and see if I’ve just lost the plot 🙂

roelofw21:12:28

and how to solve this one :

(defn local-bindings
  "Clojure lets you give local names to values using the special let-form."
  (identity 7))   

roelofw21:12:01

test :

(testing "local bindings"
    (is (= (local-bindings) (let [x 5] (+ 2 x))))
    (is (= (local-bindings) (let [x 3, y 10] (- y x))))
    (is (= (local-bindings) (let [x 21] (let [y 3] (/ x y))))))  

roelofw21:12:24

error message : Parameter declaration "identity" should be a vector,

kokos21:12:11

use macroexpand on your -> see what that expands into, your assoc calls don't get the var to which you want the key association to happen. https://clojuredocs.org/clojure.core/assoc#example-542692c9c026201cdc326acd

seancorfield23:12:53

@roelofw Think about what exactly that error is telling you: on this line (identity 7), the compiler is expecting a “parameter declaration” as a vector.

genmeblog23:12:09

@ceales the other option is to define type with these fields, it's will be fastest structure