This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-01-05
Channels
- # admin-announcements (1)
- # architecture (29)
- # beginners (244)
- # boot (5)
- # cider (2)
- # clara (8)
- # cljs-dev (58)
- # clojure (93)
- # clojure-australia (1)
- # clojure-dusseldorf (4)
- # clojure-france (1)
- # clojure-greece (16)
- # clojure-italy (9)
- # clojure-norway (1)
- # clojure-romania (1)
- # clojure-serbia (3)
- # clojure-spec (68)
- # clojure-uk (103)
- # clojurescript (41)
- # code-reviews (4)
- # community-development (4)
- # cursive (11)
- # data-science (2)
- # datascript (6)
- # defnpodcast (4)
- # docs (21)
- # duct (4)
- # emacs (118)
- # fulcro (120)
- # graphql (1)
- # jobs (1)
- # jobs-discuss (43)
- # leiningen (12)
- # off-topic (39)
- # onyx (11)
- # parinfer (13)
- # perun (1)
- # re-frame (2)
- # shadow-cljs (4)
- # spacemacs (5)
- # unrepl (6)
- # yada (1)
@scallions -- so you were able to get Array Manipulation to pass without timeouts, without special tricks? Hm. I might need more of a hint than that...but will think more about it in the meantime.
Yea. I don't think I came up the with algorithm myself. I definitely went there first.
Bewarned though, they spoil the solution in that tab.
Well, yes, they do! I shoulda thought of it, but was taking the Description too literally I guess.
An earlier problem with timeouts, I "solved" via (apply print ...
or some such thing, versus looping. Probably missed an opportunity to think more algorithmically there as well (and I later found at least one place where I had some clunky code that could easily be replace via use of fnil
.)
So this is really good news for me, because I feel I'm weaker algorithmically than I am "programmatically", given my backgound -- and that I haven't really worked on compiler internals in a couple of decades, where thinking algorithmically becomes the norm.
It is a hard problem though, no big deal about not getting it. Usually, these problems have some obscure trick to it which drastically cuts down the time complexity.
Sure, except I'm supposed to be thinking in terms of O() (time/space/bandwidth/latency) complexity, given my background and where I'd like to go, career-wise. That's why I think it might be "good news" if HackerRank actually challenges me to do more than just express "obvious" algorithms in Clojure.
FWIW, my transients version is several times faster than my naive version(s) (now that I have a test-case-creator function plus my new handy I/O-redirection macros per the #cider channel).
(The transients version still times out on several test cases though -- despite no longer doing so on a few.)
@james-clojure Your next step will likely be mutable arrays, possibly mutable arrays of primitives 🙂
Hmm, improved algorithm (replacing O(N) behavior with O(1)) passes 7 test cases but still times out on 7.
@scallions -- sorry to bother you again, but did your solution use any mutable arrays either? As mentioned above, my new implementation (using the delta algo to go to O(1) from O(N)) is lots faster, but still times out on 7/14 test cases, and I'm having trouble identifying any further big-O-level gains. (I even tried replacing the (apply max (reductions + ...
at the end with just (apply max ...
, to see whether that would still timeout all 7 tests -- and yes, it did, while of course failing the 7 that previously passed. So that extra massaging of the delta vec into an absolute vec, for consumption by max
, seems to not be the issue.)
I did try re-introducing transients (making the vector transient, that is) -- but that made no visible different on my 10Kx10K test case.
Yea I was curious why you where having trouble so I'm trying out the O(n) algorithm.
I'm encountering the same problem as you. Timeouts at tests 7+.
The original solution I used to pass has a complexity of O(m * log(m))
The biggest problem I see in the O(n) algo is allocating that zeroed out vector/array.
(It seems to actually be faster than v (vec (replicate 0))
, as was suggested on a website I found.)
I'll think about how to elegantly avoid allocating the large vector in the first place then.
I've tried (vec (int-array n 0))
. and its still too slow.
Well the C solution has the advantage of not repouring a zero array back into a vector.
and n is pretty big.
I meant C++ solution. (Ditto the Python, Ruby, C, C#, Java, and JavaScript solutions. I haven't finished skimming the thread, but haven't noticed a Fortran solution. ;)
Anyway, so far, they all seem to start out allocating and zeroing-out a v[n]
representing the final result!
Alrighty, I managed to pass the test cases with O(n).
but I had to use an array and mutate on it directly.
Oh, okay. I haven't learned about that yet (I guess it's different from using a transient vector?).
I'm still not sure how you can arrive at an O(m * log m)
solution without allocating a complete vector up front, or essentially risking ending up with one.
Do you need a hint?
I used a sorted data structure instead of a vector.
That's what I thought might be the case, but I guess I'm not sure how to "fold" the results of lines effectively without ending up with a large vector anyway. Gotta think about it more I guess....
Does it really use less memory and take less time, on large-enough cases, than the straightforward implementation of the O(m)
solution everyone seems to have adopted in the Discussion tab?
Well the O(m * log(m)) solution worked first try for me, so I didn't bother with the O(n) until just now.
Oh, interesting! I would think your first-try solution would have been more complicated...is that the case?
My solution is pretty simple.
Okay, I'll give that a shot. Maybe tomorrow though, it's late for me. Thanks again for all the help!
Wow, using a sparse vector (i.e. a map with integer keys) worked! Even though it took a little more time than my previous approach on my test cases, which were "square" (`n = m`). I'll try a very rectangular test case next on my own machine to see the difference more clearly, but I've officially submitted working code now! 🙂
Yep, that's basically the trick.
I used a sorted map. Or you can use a regular map. Either way, you need to sort, so therefore O(m * log(m))
@scallions-- indeed, huge difference:
app.core> (time (bash/< "test-10M-10000.txt" (runit-v4)))
186149886
"Elapsed time: 2135.874544 msecs"
;; => nil
app.core> (time (bash/< "test-10M-10000.txt" (runit-v5)))
186149886
"Elapsed time: 50.339815 msecs"
;; => nil
app.core>
And, not nearly as complicated as I'd thought. Not doing any transients, mutable arrays, hints, etc.!I used a regular map, because I figured there's no need to maintain it in sorted order.
I didn't figure out myself either. I guess were comrades in discussion tab peeking.
Not that I haven't figured out some pretty interesting optimizations in my past...just, I'm outta practice.
Strangely, one that I found obvious at my last job involved how bits were numbered. They had various code strewn throughout libraries to handle the fact that much of their code was written in machines where bits in bytes were numbered so 0 was the highest-order bit, 7 the lowest-order -- the opposite of most modern machines.
So they had various bits of code that pulled apart long bit numbers (those that address more than just one byte, e.g. an array of bits) into "byte" and "bit-within-byte" offsets, then subtracted the latter from 7, then jammed them back together.
I thought that was hilarious, and the first time I sent out a "fix" of a library for code review, where my code simply did an XOR of the long bit number with 0x7
, two of our experts had to work through it carefully to convince themselves I wasn't on drugs or something.
That's not much of an optimization, but it shows how even some pretty good programmers (one of the reviewers actually contributed to the C standard and helped create UTF-8) couldn't see that solution. Me, I'd learned bit-twiddling at a tender young age, so it was kinda automatic.
Just gotta do stuff like that regularly for it to become anything near to automatic. Hence my desire to increasingly immerse myself in Clojure and FP.
Well, I found Clojure to be a long, but enjoyable learning curve. Good luck!
Question: why can't we have that syntax in a defn? (defn my-fn [a b & rest :as args] ...)
is there some way to dispatch to different functions based on the type of the parameters? maybe some macro in the core package?
interfaces are Java interfaces, but are the fastest when implemented over Java types (records etc.) you control.
protocols can do interface dispatch on your types, but you can also extend them over other Java types so they're more general at some speed cost.
There must be somewhere some people implementing macros to spare them then manual cond/if dance.
@arrdem btw, thanks for the help
Yeah so multimethods tend to be the usual way to spare yourself the cond dance, but it's got some limitations obviously.
There is a pattern matcher (clojure.core.match) but it's a fairly uncommon library to see. I like it, together with https://github.com/arrdem/guten-tag (which I wrote, </plug>)
is there a simple way to create a vector with different elements at the beginning ?
#(vector %1 %2 %3 :my-constant-element)
, alternatively you could use a constant together with update
or assoc
. Vectors are designed to be efficient when pushing to or popping from the back. There are other datastructures which relax this constraint and are efficient at pushing to either end, but they aren't in core with the exception of the oft-forgotten and not well documented clojure.lang.PersistentQueue
.
I found (vec (list* 1 2 3 [4 5]))
Yeah. If this is really what you want, you should probably be using lists / seqs not vectors.
hum .. I am not aware of the difference yet.
I may not use #(vector %1 %2 %3 :my-constant-element)
as the :my-constant-element
in a vector which I want to stay flat in the result.
@vincent.cantin You can also just say (into [a b c] [4 5])
@rauh indeed, it seems to work.
is there a way to destructure an map entry into a couple (key value)?
never mind, I found it.
Anyone aware of a way to install leiningen through a wrapper, the way you can with Maven and Gradle?
Hi all! I am a student who is going to apply for GSoC. I have checked the previous idea lists page http://clojure-gsoc.org/project-ideas/. It seems not updated for this year.
for the sake of argument, how might i go about immediately taking from a channel that i just put to? in clojurescript this never prints, presumably because >! doesn't return a channel?
(go (println (<! (>! (chan) "test"))))
this works but feels verbose: (go (println (<! (let [c (chan)] (go (>! c "test")) c))))
I feel like this is kind of a dumb question, but what's the difference between figwheel and sidecar?
IIRC, figwheel is the leiningen plugin, i.e. what happens when you call lein figwheel
, and sidecar is a dependency that allows you to start, stop, and script figwheel from code (as opposed to from project config)
can anybody help me how to fetch from clojurescript in a reagent environment a remote json and display it?
You'll want to look at these libraries: https://github.com/JulianBirch/cljs-ajax https://github.com/yogthos/json-html
Use cljs ajax to fetch the json and convert to edn. Place the edn in an atom. Use json-html to make a component that reacts to the atom.
@scot-brown i tried something like this, it didn't work, is there anywhere a working example i could look at?
What's the best environment to be doing clojure development in, coming from a vim background? I'm open to learning emacs, or putting together Atom/VSCode plugins. I recall that Emacs is a favourite of Lisp programmers. Is there anywhere I can learn to set up an effective development environment?
fireplace, vim-parinfer and vim-surround provides a very nice experience, at least IMO
@zack.mullaly either spacemacs or vim with fireplace
@zack.mullaly I use Neovim and tmux for Clojure. If you’re down with a psuedo-repl it’s a great way to go. If you’re interested I can show you some more of my set up
I tried moving from Vim to Spacemacs but was struggling because I open and close my editor a ton and Spacemacs is pretty slow spinning up. It’s more of a open once and live there for like 4 days kind of deal. It’s totally a personal thing though!
@admay I don't do this myself, but you could try starting up spacemacs once, and using emacsclient
instead of emacs
so that every time you open and close emacs you aren't restarting the whole thing
@scot-brown i don;t understand your example
@scot-brown Some one told me about that approach too. Another great approach to Clojure development! I heard about it after moving to Neovim though so I wasn’t looking to move back. I like the psuedo-repl approach though. It just goes to show how flexible the Clojure development world is though! So many great ways to write code
(defn make-remote-call [endpoint] (go (let [response (<! (http/get endpoint))] ;;enjoy your data (js/console.log (:body response)))))
@mjo324_56 Since this seems to be a bit more involved, can you put the project up on GitHub (or something like it) so we can check it out with more context? I think we’ll be able to help out a bit more if we can see some more of the context
but if you are just logging your data, a nil will be returned and it will not be there anymore
if you'd like to use json-html, follow the example on the github page to get styling working
I think the main source of complexity here is the use of async which is a bit excessive for a simple ajax call
this gives me a dev.cljs?rel=1515173992040:10 Uncaught TypeError: Cannot read property 'init_BANG_' of undefined
If i have have two vectors x
and y
, how do I create a vector z
that contains all of the elements from both x
and y
?
(into x y)
@scot-brown i copied your code, the component does not show up, the html is no generated, i get the default "ClojureScript has not been compiled! please run lein figwheel in order to start the compiler" and beforementioned typeerror in the js console
Can I just say how cool https://github.com/venantius/ultra is?
It just told me hint: lists seem to contain the same items, but with different ordering.
and was spot on
@mjo324_56 Without seeing your project I can't diagnose exactly what the type error is. It sounds like there may be some configuration issues. I pushed my dummy project if you want to see what it looks like working. Just clone, call lein figwheel
from terminal, and go to localhost:3449
https://github.com/SVMBrown/json-test
@hans378 There are a ton of great open source tools available to build a Clojure development environment! Ultra has been in my profiles.clj
for a good 4 months now and I’m never going back!
https://clojure.org/community/resources -- Great list of resources for folks new to Clojure!
(since I took that link out of the topic -- I've pinned that message)
I'm little confused about Clojure/ClojureScript. I've seen a lot of library to do things in Clojure and also in ClojureScript. But I can't imagine when I use clojurescript or clojure...
@caio.cesar.g.souza clojure targets the jvm and clojurescript targets javascript. If you want to use the language on a browser, you’ll use clojurescript. On the backend, I imagine clojure is more popular.
but, I can also use cljs in backend, right?
sure, but it’s usually a bad idea
perhaps you have a definite need to use node, but the performance and resource usage with jvm clojure is much better
Does anyone know someone willing to give detailed feedback on someones clojure knowledge, maybe by reviewing some code or in another way. This is something I would be willing to pay for.
@drewverlee are you hiring clojure developers?
No the opposite, I'm trying to get hired as one
There is a #code-reviews channel here
sure, really appreciate the help btw.
a macro is defined in one namespace.
in another namespace, I want to alias the macro so I can invoke it with a different name.
so in ns 1 there’s something like (defmacro my-macro ....)
and in ns 2 I’d like to do something like (alias new-name other-ns/my-macro)
alternatively, you can use require with :refer :rename (ns my.ns (:require [other.ns :refer [my-macro] :rename {my-macro new-name}])
@admay using a library and the way I’m using a macro doens’t quite make sense with how it’s named. would prefer to litter my code with something more readable/understandable
@scot-brown basically what I was looking for, appreciate the deep cut!
not to hijack the thread, but I’ve had a longstanding question, which I think is related to this discussion: the require-macros
documentation states There is a nicer alternative if the Clojure macros file has the same name as a ClojureScript file in the same directory, which is a common pattern. In this case, you can just use the :include-macros or :refer-macros flag of the require form.
So far I’ve just been cut-n-pasting whatever magic string I need from the readme of the library into my ns
expression. Is there a simple way of understanding what this means and when I need to use require-macros
vs. refer-macros
.
@lee.justin.m afaik *-macros is outdated. cljs now magically handles that for you in a regular require and you can pretend they're functions
@scot-brown so I should be able to just make the obvious transformation of (:require-macros [cljs.core.async.macros :refer [go]])
to (:require [cljs.core.async.macros :refer-macros [go]
?
what is the preffered way on using bootstrap with reagent? is all of bootstrap functionality supported?
and https://github.com/reagent-project/reagent-cookbook describes only a few components
or should i simply use hiccup to generate the html and everything else will be done automatically?
@mjo324_56 I’ve never actually done it, but from what I remember, most of bootstrap is pure css, so all you need to do is pull it in and produce the right elements with the right classes. There is a bit of javascript to enable some features, like dropdowns and modals, if I recall, and so you might have to do a bit more to interact with those elements.
@mjo324_56 Here’s a reagent cookbook recipe that uses bootstrap to create a modal https://github.com/reagent-project/reagent-cookbook/tree/master/recipes/bootstrap-modal
The index.html page imports the boostrap js and css allowing you to use the classes and whatnot in the Cljs.
And a date picker, https://github.com/reagent-project/reagent-cookbook/tree/master/recipes/bootstrap-datepicker
this date picker example is from 2015, this together with the missing index.html made me confused
right - something needs to be sent by the back end
but it could be generated in code instead of being a file in your project
(defn loading-page [] (html5 (head) [:body {:class "body-container"} mount-target (include-js "/js/app.js")]))
that’s server side code, right?
@mjo324_56 another alternative is using react bootstrap components... You could do that with https://github.com/gadfly361/baking-soda
@mjo324_56 the server side code you posted and the index.html should functionally be equivalent, so i do think translating the html to hiccup is the way to go if you wanted to use that recipe
the enlive
library can take html and output hiccup that would render to that html, btw
you could do it once to generate the form to paste into your code, or use enlive inside loading-page to generate the initial page structure
if i'd like to use some bootstrap templates from the net, more complicated ones, then translating each little bit into hiccup is not so good
alternatively you can add a script tag that loads js/app.js into the bootstrap file
or use eg. selmer to take something that is mostly an existing html file and inject computed contents into it
(defn head [] [:head [:meta {:charset "utf-8"}] [:meta {:name "viewport" :content "width=device-width, initial-scale=1"}] (include-css (if (env :dev) "/css/site.css" "/css/site.min.css" "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" ))]) (defn loading-page [] (html5 (head) [:body {:class "body-container"} mount-target (include-js "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" "/js/app.js")]))
@gadfly361 or can i do like (:require [baking-soda.bootstrap3 :as b3] and all necessary bootstrapstuff will be included and i can use bootstrap tags with [b3/some-bootstrap-tag {...}] ?
Yeah, that should work .. baking soda wont bring in the css for you tho, so you'll need to add that
About bootstrap, I found that I much prefer using https://bulma.io/ instead. It has only css, leaving the javascript up to the user.
To answer the question, yes I did indeed have a bad experience with the reagent/bootstrap combination...