Fork me on GitHub
#beginners
<
2016-11-22
>
mikepjb12:11:25

(take 3 [8 2 6 3 0 0 0 9 0]) will return the first three numbers of a vector - how would I do the same for the middle three numbers?

mikepjb12:11:47

(take 3 (last (split-at 3 [8 2 6 3 0 0 0 9 0]))) this works

mikepjb12:11:14

however it doesn’t seem like the correct way to perform this action

mikepjb12:11:26

for now I’ve put it down as a function - pretty happy with this but any 2 cents will be appreciated 🙂

(defn take-at-point [point n col]
  (take n (last (split-at point col))))

(take-at-point 3 3 [8 2 6 3 0 0 0 9 0])

roelofw13:11:50

anyone who can answer my question ?

dpsutton13:11:41

@roelofw the best thing that I have done to make webapps in clojure and learn is to run lein new compojure <appname>

dpsutton13:11:49

this sets up a scaffold and you are off to the races

dpsutton13:11:55

then just lookup whatever you need

dpsutton13:11:26

but you don't validation to try out a tutorial or article; just read it and try it

dpsutton13:11:31

experiment a lot

dpsutton13:11:36

feel free to make thousands of projects

dpsutton13:11:42

if a tutorial sucks, abandon it

dpsutton13:11:51

if it is good, follow it and make a bunch of things following it

dpsutton13:11:10

then when something advocates a different way, you might be in a position to evaluate if you think that that is good advice based on experience

roelofw13:11:57

oke, thanks for the very long answer

roelofw13:11:18

I asked because Im afraid to learn bad practices

dpsutton13:11:34

that's an excellent mindset to have

dpsutton13:11:38

always evaluate everything

dpsutton13:11:45

and you can start to "feel" when things are bad

dpsutton13:11:54

like hardcoding stuff, brittle code, etc

dpsutton13:11:13

but you can always learn from tutorials, even if you learn that that style/technique is bad, etc

roelofw13:11:15

@dpsutton another time thanks for a long answer

tokenshift14:11:09

@mikepjb I would combine drop and take:

tokenshift14:11:31

(take 3 (drop 3 [8 2 6 3 0 0 0 9 0]))

tokenshift14:11:23

Though if your input list is very long, and you know it’s a vector (as opposed to some kind of sequence), subvec is better, since drop actually reads through (and discards) the first N values.

Alex Miller (Clojure team)14:11:18

subvec is O(1) b/c it just creates a view on the original vector, so prob preferred a) if you are using only vectors and b) you understand that this can potentially cause memory problems (as the full old vector is still reachable as live objects)

mikepjb14:11:25

okay thanks @tokenshift and @alexmiller I haven’t come across subvec before

tokenshift15:11:22

@alexmiller I didn’t realize subvec kept the original vector around (though that makes sense, since everything’s immutable); thanks.

tokenshift15:11:45

Is there a good way to duplicate (part of) a sequence to break that link?

tokenshift15:11:40

Like (dup (subvec [8 2 6 3 0 0 0 9 0] 3 6)), returning a new sequence (no longer a view of the input vector)

tokenshift15:11:19

I suspect calling vector on the result would do the trick, not sure how to test that though.

Alex Miller (Clojure team)15:11:29

you could apply vector to it

Alex Miller (Clojure team)15:11:49

(but not vec - that optimizes by just returning what you give it if it satisfies)

roelofw16:11:15

I had to re-implement range :

(fn [begin end] (take (- end begin) (iterate inc begin))) 
. How can I improve this one ?

dpsutton16:11:56

you could have a single arity that assumes starting at 0?

roelofw16:11:05

I could , but the 4clojure challenge gives a starting point which can be other then zero

dpsutton16:11:18

right, have multiple arities

dpsutton16:11:23

a cool thing about this particular lisp

roelofw16:11:10

yep, and the lazyiness is also cool. I can make a range and take the part I want

roelofw17:11:38

/clj (apply str(set("aaabbbccc")))

roelofw17:11:54

/clj (apply str(set("aaabbbccc")))

dpsutton17:11:21

@roelofw if you need to view something quickly, you can message the cljbot from your direct message with slackbot

dpsutton17:11:33

but you probably shouldn't use this channel as your own personal repl

roelofw17:11:28

It is possible to convert this (\a \b \c) to ( "a" "b" "c")

roelofw17:11:47

I have to delete duplicates in a seq so ( "aaabbbccc") will be ("abc"). I thought about using set but I think I can better use another approach

roelofw17:11:00

maybe using a map instead of a set

dpsutton17:11:10

(->> "aaaaaabbbbbbbcccccc" seq distinct (map str)) ("a" "b" "c")

Alex Miller (Clojure team)17:11:29

I don’t think 4clojure is using a new enough version of Clojure, but there is now dedupe

Alex Miller (Clojure team)17:11:43

user=> (dedupe "aaabbbccc")
(\a \b \c)

roelofw17:11:28

@alexmiller thanks, why do I see this error message : ClassCastException java.lang.String cannot be cast to clojure.lang.IFn user/eval1496 (form-init1114920531766215249.clj:1) after the right answer

Alex Miller (Clojure team)17:11:27

looks like you’re using a list with a string as the first element and it’s trying to invoke it

roelofw17:11:35

I do this in a repl

roelofw17:11:57

I copy exactly what dpsutton does

Alex Miller (Clojure team)18:11:33

If you copied the (”a” “b” “c”) part, that was the result, not part of the answer

Alex Miller (Clojure team)18:11:38

and it would yield that

roelofw18:11:17

That is the problem, stupid of me

roelofw18:11:50

The only thing I would do is rewrite it , I have not learned the ->>

Alex Miller (Clojure team)18:11:33

if you want to see it rewritten, use macroexpand: (macroexpand '(->> “aaaaaabbbbbbbcccccc” seq distinct (map str)))

roelofw18:11:26

@alexmiller I think I see another macro : (clojure.core/->> "aaaaaabbbbbbbcccccc" clojure.core/seq clojure.core/distinct (clojure.core/map clojure.core/str))))

Alex Miller (Clojure team)18:11:22

Well, that's not what i get :)

roelofw18:11:18

I do when I do it in my cursive repl

roelofw18:11:02

aha, I have copied a ' with it

roelofw18:11:26

Now I see this : (map str (distinct (seq "aaaaaabbbbbbbcccccc")))

roelofw18:11:31

Hmm, My idea fails on this case : (= (__ [1 1 2 3 3 2 2 3]) '(1 2 3 2 3))

roelofw18:11:52

I think I have to use a if then when the input is a string

mathpunk23:11:23

I'm starting a new devcards project. I'd like to write some server-side routes for my UI to get data from. The devcards template has no server. I'm going to examine the figwheel readme to try and figure out how to modify my build to start a server, too, but pointers are welcome 🙂