This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-04-15
Channels
- # beginners (34)
- # boot (6)
- # cider (20)
- # cljs-dev (5)
- # clojure (7)
- # clojure-austin (1)
- # clojure-dusseldorf (10)
- # clojure-spec (11)
- # clojure-uk (3)
- # clojurescript (7)
- # cursive (1)
- # datomic (6)
- # defnpodcast (1)
- # hoplon (2)
- # luminus (32)
- # lumo (9)
- # nrepl (2)
- # nyc (1)
- # off-topic (18)
- # onyx (9)
- # portkey (3)
- # re-frame (27)
- # reagent (102)
- # shadow-cljs (70)
- # tools-deps (25)
- # vim (2)
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?
@mailmeupinside (reductions #(%2 %1) 1 [inc inc])
is pretty close to what you want
@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))
, ...Anyone know how to do something like this?
`(let [arr# [1 2 3]]
(f ~@arr#))
I've been getting "CompilerException java.lang.RuntimeException: Unable to resolve symbol: arr# in this context", and I'm not sure whyYou 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#)
Ah, thanks!
remember that things inside a quote are just data, not code - and unquoting is for bringing things outside the quote into the quote
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]))
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?
https://wiki.haskell.org/Foldr_Foldl_Foldl%27
foldl'
is more performant
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
before a stack overflow, you get an integer overflow 😂
(reduce + (range 10000000000))
java.lang.ArithmeticException: integer overflow
or you can use +'
and get the answer
user=> (reduce +' (range 10000000000))
49999999995000000000N
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?
@noisesmith In @drewverlee’s particular case, the ns
is dev
-- I wondered if that might be some special case in CIDER?
so it's a single segment namespace called dev?
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
(that function is named process-reference in the ns macro definition, and after removing gen-class it just call all these generated clauses
What’s the significance of the ^
in the code below?
(:import [javax.servlet ServletContext])
…
(.getContextPath ^ServletContext foo)
it's a metadata type annotation telling the compiler to expect foo to be of class ServletContext
thanks, it’s used a bit differently to what I found in the clojure docs? https://clojure.org/reference/java_interop#typehints
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
which can lead to less expensive bytecode (avoiding reflection)
this isnt's the same as enforcing the type though