Fork me on GitHub
#beginners
<
2016-11-21
>
drewverlee06:11:26

whats the best way to walk a hashmap given a vector of keys? {:a {:b 1}} [:a :b] => 1

dangit06:11:49

(get-in {:a {:b 1}} [:a :b]) => 1

dangit06:11:16

Is that what you mean, @drewverlee ?

drewverlee06:11:09

man i must have been googling the wrong set of words

roelofw07:11:10

@jswart I use your code this way :

(map (fn [input] 
       (let [reverser 
             (if (string? input)
               (comp (partial apply str) reverse)
               reverse)] 
         (= input (reverser input)))) 
      

roelofw07:11:35

but then the first test fails here : http://www.4clojure.com/problem/27

curlyfry08:11:02

@roelofw Are you using an editor + REPL or are you typing the solutions directly into 4clojure? Working in the REPL makes it a lot easier to see what your functions do

curlyfry08:11:53

For example, what do you get when you call your function with '(1 2 3 4 5) as input?

agile_geek08:11:33

@roelofw I am guessing that's a potential soln to an anagram detector? In the snippet you've shared you don't seem to have passed a collection to map to map over?

agile_geek08:11:14

Actually returns a transducer (not sure ClojureBot can handle that!

agile_geek08:11:25

Nope must be unbalanced parens

agile_geek08:11:21

Even if you fix that - I'm not sure what problem you are try to solve but that is expecting a sequence of either sequences or strings?

roelofw08:11:49

@agile_geek how do you talk to the clojurebot ?

roelofw08:11:31

and I type direct in the 4 clojure editor

agile_geek08:11:14

Start a line with /clj followed by your Clojure code

roelofw08:11:47

thanks, this solution is too difficult for me. I think im going for : ' (fn [x] (string? x (= x (str(reverse x)) (reverse x))))

agile_geek08:11:21

Hmm, think about how many args string? takes

agile_geek08:11:27

Also what it returns

roelofw08:11:54

string have 1 argument and it schould return true or false

agile_geek08:11:00

I would try out some of these in a REPL - I think 4Clojure has a link to tryclj repl?

roelofw08:11:29

I work now in the repl of cursive

agile_geek08:11:55

How many args are you passing string? in that code you copied above?

roelofw08:11:01

you mean the code I found dfficult or my own code ?

agile_geek08:11:37

(fn [x] (string? x (= x (str(reverse x)) (reverse x))))

roelofw08:11:56

more then 1

roelofw08:11:11

I changed the code to :

(fn [x](string? x)
                (= x (str(reverse x)))
                (= x reverse x) "racecar" ) 

roelofw08:11:22

but now I get a object back

agile_geek08:11:35

Again- think what string? does

roelofw08:11:56

returns true or false ?

roelofw08:11:12

that why I comparing things wih =

agile_geek08:11:49

correct but you want to control the flow of expressions

agile_geek08:11:38

at the moment you're executing each of these in sequence but none of them are doing anything with the result of the string? predicate...

agile_geek08:11:55

the answer is easier than I'm making it

agile_geek08:11:06

you're missing a control flow statement...

agile_geek08:11:23

the simplest one there is

roelofw08:11:50

oke, what I want is if x is a string then this part must be evaluted (= x (str(reverse x))) else the other part

agile_geek08:11:35

yep... I think the most important part of your statement above is if

roelofw08:11:31

oke, so I need a if ?

agile_geek08:11:46

and I'm not sure your parens are balanced

roelofw08:11:25

I do not think so.

roelofw08:11:38

I have now this :

(fn [x](if (string? x)
                (= x (str(reverse x)))
                (= x reverse x)) "racecar" ) 

roelofw08:11:51

and still a object as output 😞

roelofw08:11:11

oke, this works :

(defn roelof [x](if (string? x)
                (= x (str(reverse x)))
                (= x reverse x)))

roelofw08:11:41

only I need to use something else then str to make a string out of a seq

agile_geek08:11:31

You are missing parens around second reverse

agile_geek08:11:07

I don't think you need to make a sequence a string as all you want as a result is true or false

agile_geek08:11:52

if x is a sequence and (reverse x) is a reversed sequence then you can just compare them

roelofw08:11:09

oke, this one works for the first two :

(fn [x](if (string? x)
                (= x (clojure.string/join(reverse x)))
                (= x reverse x)))  

roelofw08:11:33

but fails for : (true? (__ [:foo :bar :foo]))

agile_geek08:11:40

yep or you could use (apply str (reverse x))

roelofw08:11:48

so back to the drawing board

agile_geek09:11:06

see ☝️ about missing parens

roelofw09:11:10

but I have to leave. So experiment on later on the day

roelofw09:11:22

Missing parens ? Why does the first two works then

roelofw09:11:36

But I forget to thank you for all the help

agile_geek09:11:17

look at (= x reverse x)

agile_geek09:11:09

is x equal to the fn reverse and to x again?

agile_geek09:11:02

i.e. = is taking 3 arguments, two values (of x) and the unevaluated function reverse

roelofw12:11:45

ok, im back and will take a look at it

roelofw12:11:31

@agile_geek thanks, solved another challenge

curlyfry12:11:29

@roelofw I highly recommend following some people to see how they solve the challenges! I follow some of the top users: maximental, hypirion, jafingerhut, chouser, _caterpillar

roelofw12:11:35

@curlyfry thanks

roelofw12:11:40

@curlyfry do these people have good solutions where you can learn from

curlyfry12:11:41

@roelofw I'd say so, yes

roelofw13:11:58

@curlyfry thanks again

roelofw13:11:05

Now thinking how to solve the fib numbers in clojure

roelofw13:11:19

I know how to calculate them

dominicm14:11:42

@roelofw the trick to the palindrome stuff was that reverse converts it's input to a sequential, so you should do the same.

dominicm14:11:19

@roelofw a popular Fibonacci sequence solution uses recursion. It's worth trying to use recursion to solve it as it's educational

roelofw14:11:57

yep, and for recursion you can do a loop or maybe use a map or iterate

roelofw14:11:00

@dominicm yes, I saw that solution from others when solving that challenge

dominicm14:11:56

map isn't for recursion

roelofw14:11:00

@dominicm so only the loop is recursion ?

dominicm14:11:34

No, you can recurse like this

roelofw14:11:43

I was thinking to use iterate

dominicm14:11:53

=> ((fn myfn [x] (if (zero? x) x (+ x (myfn (dec x))))) 5)
15

roelofw14:11:49

oke, now I understand what you mean , thanks for the clarification

dominicm14:11:21

I think recursion is the simplest way to solve the Fibonacci problem

roelofw15:11:51

someone who can help me why I see this error message : ' ArityException Wrong number of args (1) passed to: runner/fibtill/fn--1885 clojure.lang.AFn.throwArity (AFn.java:429) on this code : `` (defn fibtill [n] (take n (map first (iterate ( fn[ a b ] ( + b (+ a b))) [0 1])))) `

bbss15:11:32

you are passing a single argument to the function in iterate

bbss15:11:40

a vector of 0 and 1

bbss15:11:51

you want to lose the vector

roelofw15:11:57

I thought I destructed the vector by using [ a b ]

fabrao15:11:33

Hello all, is there any way to use something like this? (re-seq #(str "some " thing) "some thing")

bbss15:11:36

a function already takes a vector as its argument declaration, if you want to destructure in there you need to nest another vector

roelofw15:11:45

oke, I did .

(defn fibtill [n]
  (take n (map first (iterate ( fn[ [a b] ] ( + b  (+ a b))) [0 1])))) 

roelofw15:11:14

but now this error message : IllegalArgumentException Don't know how to create ISeq from: java.lang.Long clojure.lang.RT.seqFrom (RT.java:542)

fabrao15:11:02

Sorry, it´s (re-seq (re-pattern (str "some " thing)) "some thing") 🙂

roelofw15:11:21

@fabrao have you tried it on a repl ?

fabrao15:11:41

Yes @roelofw

roelofw15:11:52

and did you get the output you wanted

pawel.kapala16:11:13

Hey, what am I missing, as can I do (require …) in user ns in repl, but I get Unable to resolve symbol require …if I switch to namespace using (in-ns ‘my-example.core)?

jswart16:11:22

When you run (in-ns ‘foo) the repl creates a new namespace for you. This namespace does not have clojure.core in it. To see what you have you can type (clojure.core/ns-map clojure.core/*ns*)

jswart16:11:46

this will show you a mapping of symbols to their values. You can type the symbol to run it in the repl.

jswart16:11:02

In order to get clojure.core in this namespace run (use clojure.core)

jswart16:11:24

now run (ns-map *ns*) and you will see the clojure.core bindings in the namespace

jswart16:11:55

sorry you have to type (clojure.core/use ‘clojure.core)

jswart16:11:31

@pawel.kapala ^

dominicm16:11:39

@roelofw important part of clojure is learning to read these error messages. You should watch the Stuart Halloway talk on Debugging with the Scientific Method (or something similar to this in name)

pawel.kapala16:11:00

@jswart wow, thanks! I didn’t know it doesn’t include clojure.core. Do you know reasoning behind that? Why wouldn’t I want clojure.core to be loaded each namespace?

jswart16:11:41

In source code it does, but in the REPL you have to do it manually.

pawel.kapala16:11:28

Yes, this assumption from the "source code” led me to this issue. But I still wonder why it is like that… reading in-ns documentation again...

jswart16:11:01

The reason it happens in source code is the ns macro does it all for you.

jswart16:11:07

so check that doc out too!

pawel.kapala16:11:31

There it its (in the example), I missed that...

;; The "in-ns" function works almost the same as "ns", but does not load
;; clojure.core

pawel.kapala16:11:06

So I guess the real culprit is that I should’ve used ns instead of in-ns.

pawel.kapala16:11:06

Thanks @jswart!

jswart16:11:48

Depends on what you are doing, if you are trying to test out some code then I usually put it into a file and run (load-file “thefile.clj”)

jswart17:11:08

this is assuming I’m not just running a REPL with a little bit of code for fleshing out some idea

jswart17:11:27

in ~5 years i don’t think I’ve ever run (ns…) at the REPL, but maybe I’m doing it wrong.

alexmiller17:11:22

to switch to a namespace that exists, I often (require foo) then (in-ns ‘foo)

alexmiller17:11:37

to switch to a namespace that does not exist, I would (ns foo)

pawel.kapala17:11:46

I usually whole load buffer in cider, but sometimes I just want to run just a function without writing it in a file, now 90% of those get saved in a test anyways...

alexmiller17:11:24

if you accidentally in-ns to a new namespace and don’t have core, the easiest way to correct is to (clojure.core/refer-clojure) (which is all ns does)

jswart17:11:36

Those are good, I’ve honestly never thought to (ns '…) at the REPL. I usually play in the REPL until the idea is done, grab the .nrepl-history with VIM and make a file.

jswart17:11:45

definitely just learned some stuff 🍻

pawel.kapala17:11:46

@alexmiller perfect!

pawel.kapala17:11:01

@jswart I learned a lot as well, thanks!

pawel.kapala17:11:34

bear in mind that (ns) does not take symbol unlike (in-ns)

alexmiller17:11:10

technically both take symbols, but ns is a macro so the symbol will not be evaluated and thus doesn’t need to be quoted

sb17:11:48

@roelofw I finished with Clojure-Nginx server setup here is the final ssh code https://github.com/damesek/clojure-nginx-setup-ssh

echristopherson19:11:21

How do I use clojurebot?

echristopherson19:11:04

@clojurebot help

roelofw19:11:33

@echristopherson /clj <command>

echristopherson19:11:48

@roelofw Thanks. Where is that documented?

roelofw19:11:59

no idea, Someone else told me that

roelofw19:11:04

If I have this ( "R" "W") why does str not making a string of it

roelofw19:11:25

also apply str does not work

roelofw19:11:02

@dpsutton thanks , now I have to find out how to use it on this part :

(re-seq #"[A_Z]" s)  

roelofw19:11:05

the quote idea

dpsutton19:11:21

just put a ' before any form you don't want to evaluate

roelofw19:11:30

That code gives ( "R" "W")

roelofw19:11:23

but the code ' (re-seq #"[A_Z]" s) ` needs to be evaluated. The outcome needs to be quoted I think

dpsutton19:11:50

it returns the sequence so you wouldn't need to quote it

dpsutton19:11:10

can you put the whole thing you are trying to do?

dpsutton19:11:20

you're a little cryptic and seem to keep changing what you working on

dpsutton19:11:55

^ i mean i misunderstand what you are working on

dpsutton19:11:04

so just putting the whole text up may save some time

roelofw19:11:04

The code I work on is this one :

(defn filter-capitals
  [s]
  (re-seq #"[A_Z]" s)

  )

(filter-capitals "Roelof Wobben") 

roelofw19:11:56

which gives now ( "R" "W") where I need the output to be RW

dpsutton19:11:48

that should be #"[A-Z]"

dpsutton19:11:01

so add the apply str to it

roelofw19:11:18

yes, you are right, typo of me

dpsutton19:11:48

you know A and you know B, now combine A and B

roelofw19:11:51

yes, it works

roelofw19:11:07

yep, everything is oke

dpsutton19:11:09

did you have a question then?

roelofw19:11:20

some old code was bugging me

roelofw19:11:36

Both thanks

drewverlee21:11:26

why does (first {:a 1 :b 2}) always return [:a 1] i would expect it to be random as there is no first in hash-maps as i understand?

dpsutton21:11:23

> Returns the first item in the collection. Calls seq on its > argument. If coll is nil, returns nil.

dpsutton21:11:06

and i believe that internally, small maps under 7 (?) are ordered maps

dpsutton21:11:00

you can see that this seems to be determined by internal steps

dpsutton21:11:25

but the answer would probably be don't depend on a "first" of a map

drewverlee22:11:45

gotcha. I have this database that the clj-http’s :as :json functionality interoperates as a hashmap {:1 0.0 :2 10 :3 20}. I think the database wants to serve me a sorted collection of [timestamp/long value] sorted by timestamp, but clj-http doesn't know its a sorted-map I’ll probably have to tell it that otherwise ill run into trouble.

ghadi22:11:46

well said dpsutton