This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-01-13
Channels
- # beginners (99)
- # boot (2)
- # boot-dev (4)
- # chestnut (2)
- # cider (75)
- # clara (43)
- # cljs-dev (1)
- # cljsjs (6)
- # cljsrn (4)
- # clojars (2)
- # clojure (76)
- # clojure-brasil (1)
- # clojure-france (1)
- # clojure-italy (2)
- # clojure-spec (30)
- # clojure-uk (4)
- # clojurescript (39)
- # core-async (1)
- # core-logic (2)
- # cursive (1)
- # data-science (7)
- # datomic (14)
- # docker (12)
- # emacs (6)
- # fulcro (69)
- # garden (4)
- # hoplon (7)
- # jobs-discuss (46)
- # leiningen (3)
- # lumo (3)
- # off-topic (12)
- # om (2)
- # parinfer (12)
- # perun (9)
- # re-frame (44)
- # reagent (6)
- # rum (1)
- # shadow-cljs (73)
- # specter (5)
- # unrepl (10)
- # vim (2)
i have a list of vectors i want to use with (map +
but i'm getting an ArityException
because it's a list, i think
do i need to use apply
? or unroll?...
neither +
or map
will give you an ArityException (unless you somehow called map with 0 args)
if it's a list of vectors, use (apply map + l)
+user=> (apply map + [[1 2 3] [4 5 6]])
(5 7 9)
that did it, thank you!
Taking some great offline advice from @scallions, I just refactored some sample code to make it more idiomatic, especially by using the ->>
macro (for threading collections). The resulting code is about half the # of lines of the original, much cleaner, easier to read, and makes the difference among different implementations (all in the same file; it's a HackerRank practice-problem solution) much easier to see.
I'm definitely falling in love with Clojure! Been heading in this direction, slowly, for decades, including writing my own simple Lisp interpreter...and am grateful to have the time to come up to speed on this new-ish language and (I think) make it the top go-to language in my toolbox, replacing C.
Welcome to the Clojure love-fest 🙂
@kentoj a bit late to the game but I would have to say from personal experience (having built a fair number of plugins for gradle and run a successful consulting business with large scale production builds exclusively in gradle for the past number of years) that I would under no circumstances move from gradle to maven. Gradle was built in part to address a number of issues with maven. Gradle still has convention, it just doesn't shove it down your throat. Granted, gradle does have a few things which make it a tad tough to get started with (the groovy language and the configuration time vs execution time distinction are among the most common). Anyway, if you have something working with gradle I would think twice before moving that to maven. Only my personal opinion, but a fairly strong one : ) ok...done with my rant of the day.
Hello, I'm trying to implement my own assoc-in
function but having troubles, can't understand why it replaces value & doesn't update it
(def numbers [1 2 {:a {:b 4}} 4 5])
(defn my-assoc-in
[m [k & ks] v]
(case (count ks)
0
(assoc m k v)
(assoc m k (my-assoc-in (get k m) ks v))))
(my-assoc-in numbers [2 :b] 4)
returns [1 2 {:b 4} 4 5
instead of [1 2 {:a {:b 4} :b 4} 4 5]
(get 2 x) for all x is always nil - get is far too accepting of nonsense inputs
sometimes (eg. with a vector) (get x 2) will do the right thing
this
(defn my-assoc-in
[m [k & ks] v]
(cond
(= 0 (count ks)) (assoc m k v)
(vector? m) (assoc m k (my-assoc-in (nth m k) ks v))
:else (assoc m k (my-assoc-in (get k m) ks v))))
seems what you want?@gklijs it's probably strange but
(defn my-assoc-in
[m [k & ks] v]
(case (count ks)
0
(assoc m k v)
(assoc m k (my-assoc-in (get m k) ks v))))
it works like this tooI started a new Clojure web app project that needs to use a couple of web APIs. Where should I put the API keys? Inside profiles.clj?
If I put in profiles.clj, what does that look like? I tried putting an API key in :profiles/dev, but was not able to retrieve it using the env function.
Inside the repl, I was unable to use the env function defined in myapp.config to access the API key
Hello, is there a better way to do this?
(def a [1 2 3])
(apply + (concat [1 2] a))
For example without concatenation?Ah, I did not use with-profile
when running the repl. I should've asked this first, but does it make sense to put config values that don't change between dev and production in profiles.clj? Would jvm opts or system end vars make more sense in this case?
@rauh basically I'm trying to implement update-in
function using update
function & struggling with passing arguments in the sane manner, that what I'm currently have
(defn my-update-in
[m ks f & args]
(case (count ks)
1
(apply
update
(concat [m (first ks) f] args))))
@feihong.hsu all these things are kinda similar, depends on how u will be running the app.
@feihong.hsu You might want to check out cprop
if you haven’t seen it, it’s a nice system for managing config data: https://github.com/tolitius/cprop
is it possible to implement this stateful algorithm in clojure?
max = my_list[0]
for e in my_list:
if e > max:
max = e
print(max)
i know you could use max
, but i'm curious if you could use the imperative style
maybe with a recursive function that passed the max along?
why doesn't the expression (%2 > %1)
use prefix notation?
and #(...)
is sugar for make function?
nice, thank you!
(defn max
"Returns the greatest of the nums."
{:added "1.0"
:inline-arities >1?
:inline (nary-inline 'max)}
([x] x)
([x y] (. clojure.lang.Numbers (max x y)))
([x y & more]
(reduce1 max (max x y) more)))
An imperative style would be like this:
(let [my-max (atom (first my-list))]
(doseq [e my-list]
(if (> e @my-max)
(reset! my-max e)))
@my-max)
so, i have a list of unit vectors. and my goal is to find the max distance from the origin... for each unit vector, i need to find my current location, which is the sum of all preceding unit vectors
seems like i need to generate a new list from my list of unit vectors
something like [0 1 2 3 4 5]
--> [0 1 3 6 10 15]
is there a functional, conventional way to think about this sort of problem?
what you're doing is taking the starting point sp
and moving around, repeatedly -- (if this is off stop me)
that's right, yes
Normally if you use reduce, you'll end up with the ending location. (reduce move starting-point [...bunch of unit vectors])
right.
But for your use case, you need to find the farthest away one, so you can't just throw away the intermediate positions
that makes sense
thank you for your help!
is reductions
conventional in lisps or functional programming?
that's a very functional approach, there are other more imperative-y ways to do it, but this is clean
Question about using futures (and say same behavior with pmap
)… I’m seeing what seems like my program running a minute longer - but after it’s actually done…
e.g. if I have:
(let [;item1 (some-long-running 1)
;item2 (some-long-running 2)
item1 (future (some-long-running 1))
item2 (future (some-long-running 2))]
(time (do (first @item1) (first @item2)))
(println @item1)
(println @item2))
@chris.bailey (shutdown-agents)
@chris.bailey the thread pools underlying pmap
stick around for a minute ^
ah, ok!
I wondered if there was something like that
bingo, that worked, thank you!
although seems to raise exception if I do this when the code is run from a test
tests must be using threads, and now I’ve killed their threadpool
right, (shutdown-agents) says you are done with thethread pools
I'd say that code that sets up your app or shuts it down should be separate from normal program logic - it needs a different kind of testing
so if you had a function that computed something, and it could do it’s work in parallel (say with pmap
or by using a few futures), how would you test that given this?
parallel is an execution strategy, ultimately you probably just care that you got the right result
actually, it seems like the tests, if I don’t use shutdown-agents
, finish up fast
right, agreed - the test is just looking at the results, and that works great. And when run in the tests, the thing runs it’s normal execution time, so it’s only then when I go run it as a program on command line, that it takes a lot longer to return without shutdown-agents
.
I think the test runner (eg. lein test) will usually do the agent shutdown once it knows all your tests have returned
I would expect that at least
oh, I know what I can do…
of course - I can run shutdown-agents
at the end of my program (vs. this module)
yeah - a good pattern is putting testable things into a function, then having a very thin wrapper that does untestable things (or things that shouldn't be tested)
heh, ya, ok, that was simple
and yes, end of your program is the right place for shutdown-agents
perfect, ok, thank you! I’m pretty darn new to Clojure, but really enjoying it. Been working through some Rosalind (like Euler) problems to help learn, and came across this one that could be sped up by doing stuff in parallel, so was good first try using this stuff.
Guys, could someone share some oss clojure projects with component/system strategy and some tests?
@pny This will be a good starting point https://crossclj.info/fun/com.stuartsierra.component/Lifecycle.html -- that shows all projects that reference Component's Lifecycle protocol
maybe check out clojars: https://github.com/clojars/clojars-web/blob/master/src/clojars/system.clj
@U09U89Y6Q many thanks! that's what i'm looking for!