Fork me on GitHub
#beginners
<
2018-04-15
>
mailmeupinside10:04:53

hey guys, is it possible to achieve something like this: what happens: ((juxt #(+ 1 %) #(+ 1 %)) 1) ;; => [2 2] what I want to happen: ((juxt #(+ 1 %) #(+ 1 %)) 1) ;; => [2 3] I know it's like this because of immutability, but can I carry the result along somehow?

rauh10:04:12

@mailmeupinside (reductions #(%2 %1) 1 [inc inc]) is pretty close to what you want

sundarj14:04:41

@mailmeupinside also see iterate:

=> (take 2 (drop 1 (iterate inc 1)))
(2 3)
(iterate inc 1) generates an infinite lazy sequence of 1, (inc 1), (inc (inc 1)), ...

mailmeupinside19:04:40

@sundarj cool, I'm sure it's gonna help me in the future :+1:

daniel.gomme13:04:32

Anyone know how to do something like this?

`(let [arr# [1 2 3]]
    (f [email protected]#))
I've been getting "CompilerException java.lang.RuntimeException: Unable to resolve symbol: arr# in this context", and I'm not sure why

roosta13:04:00

You can't unquote something that's defined inside a quote. For that to work you'd either have to define your variable outside the syntax quote or do something like this: (apply f arr#)

sundarj15:04:13

remember that things inside a quote are just data, not code - and unquoting is for bringing things outside the quote into the quote

gcast16:04:41

If I want to access the elements of a vector (or list) over an abritrary set of index positions, how brutal is it to call (comp vals #(select-keys % [desired-indices go-here]))

sundarj16:04:16

@gcast vectors are functions, so you can just do (map some-vec desired-indices)

sundarj16:04:26

=> ([1 2 3] 0)
1
=> (map [1 2 3] [0 2 2 1])
(1 3 3 2)

gcast16:04:49

ahh right good call @sundarj.

gcast16:04:53

thank you

mailmeupinside19:04:13

hey so, Haskell has many flavors of fold - foldr, foldl, foldl' - which one is our reduce? do we have multiple flavors of reduce as well?

bronsa19:04:41

reduce is a left fold

mailmeupinside19:04:43

yeah but which one :D

joelsanchez19:04:50

foldl is basically a mistake, Clojure's implementation behaves like foldl', you can try the given example at the REPL, it's done instantly

(reduce + (range 1000001))
=> 500000500000

joelsanchez19:04:15

before a stack overflow, you get an integer overflow ๐Ÿ˜‚

(reduce + (range 10000000000))
java.lang.ArithmeticException: integer overflow

bronsa20:04:04

or you can use +' and get the answer

user=> (reduce +' (range 10000000000))
49999999995000000000N

drewverlee22:04:32

Can anyone take a guess why the require option on the ns macro wouldn't be able to detect something on the class path that the require function could find?

noisesmith22:04:42

it's literally the same code that gets called

seancorfield22:04:23

@noisesmith In @drewverleeโ€™s particular case, the ns is dev -- I wondered if that might be some special case in CIDER?

noisesmith22:04:57

so it's a single segment namespace called dev?

noisesmith22:04:35

I couldn't say either way about CIDER special cases, but the ns macro uses a function that turns :require into clojure.core/require and :use into clojure.core/use etc. etc. - so I don't see where clojure itself would be treating it differently between the ns macro and the require function

noisesmith23:04:38

(that function is named process-reference in the ns macro definition, and after removing gen-class it just call all these generated clauses

alexchalk1723:04:51

Whatโ€™s the significance of the ^ in the code below?

(:import [javax.servlet ServletContext])
โ€ฆ
(.getContextPath ^ServletContext foo)

noisesmith23:04:40

it's a metadata type annotation telling the compiler to expect foo to be of class ServletContext

alexchalk1723:04:09

thanks, itโ€™s used a bit differently to what I found in the clojure docs? https://clojure.org/reference/java_interop#typehints

noisesmith00:04:16

yes - the recommended style is putting the hint at the declaration, but it's also possible to put it directly where the method is used

noisesmith23:04:07

which can lead to less expensive bytecode (avoiding reflection)

noisesmith23:04:45

this isnt's the same as enforcing the type though