This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-09-16
Channels
- # alda (1)
- # aws-lambda (1)
- # bangalore-clj (1)
- # beginners (70)
- # boot (24)
- # cider (1)
- # cljs-dev (167)
- # cljsjs (8)
- # cljsrn (17)
- # clojure (224)
- # clojure-android (7)
- # clojure-austin (8)
- # clojure-russia (17)
- # clojure-spec (120)
- # clojure-uk (46)
- # clojurescript (68)
- # community-development (198)
- # conf-proposals (1)
- # core-async (7)
- # cursive (6)
- # datomic (27)
- # dirac (19)
- # events (9)
- # hoplon (2)
- # jobs (1)
- # luminus (9)
- # off-topic (1)
- # om (281)
- # om-next (5)
- # onyx (50)
- # pedestal (1)
- # re-frame (19)
- # reagent (11)
- # ring-swagger (14)
- # slack-help (2)
- # spacemacs (1)
- # untangled (72)
- # yada (30)
Hmm. Before I go too far into this very long process of defining this huge hash-map I must ask. I need to store what amounts to a "tree" of API endpoints, where some parts of the URI are variable. I think hash-map is what I need but I'm not sure. In Javascript this is a simple object:
const API = ``
const Endpoints = {
login: `${API}/auth/login`,
guilds: `${API}/guilds`,
guild: (guildID) => `${Endpoint.guilds}/${guildID}`,
guildIcon: (guildID, hash) => `${Endpoints.guild(guildID)}/icons/${hash}.jpg`,
}
As you can see... each part is a build on the previous one and includes variables. I'm not 100% sure if what I'm doing is right.
(def API "")
(def endpoints
{:login (str API "/auth/login")
:guilds (str API "/guilds")
:guild (str :guilds "/" :guild-id)})
In fact I'm pretty sure it's wrong. Can anyone just give me a single example (like the guild using an ID) and how to call it?
(and also tell me if hash-map is the right data structure or if not, what I should be using)
So where it says :guild (str :guilds "/" :guild-id)
, you'd like :guilds
to have the value (str API "/guilds")
?
@eslachance in the JS version it's using anonymous functions for guild
and guildIcon
. That's necessary in order for Endpoints
to be able to reference itself. You can do the same in Clojure
(def API "")
(def endpoints
{:login (str API "/auth/login")
:guilds (str API "/guilds")
:guild #(str (:guilds endpoints) "/" %)
:guildIcon #(str ((:guild endpoints) %1) "/icons/" %2 ".jpg") })
hey guys, trying to improve my clojure by doing simple stuff and while implementing sieve prime calculation I’m getting an error that I wouldn’t expect from tail optimized calls:
(defn rem? [x y] (and (not= x y) (= 0 (rem x y))))
(defn sieve
[limit]
(loop [step 2
acc (take limit (iterate inc 2))]
(if (>= step limit)
acc
(recur (inc step) (remove #(rem? % step) acc)))))
(sieve 10000)
This returns StackOverflowError clojure.core/seq--4357 (core.clj:137)
and I’m really not sure why… could you guys help me out understand what’s wrong in my code?@filipecabaco Hm, just a quick shot after a first look, maybe remove is lazy and keeps a reference to the list?
Ok will try! Thanks guys!
Completely forgot about laziness of remove... 😕
@filipecabaco There was an interesting discussion about lazyness versus datastructures on the ML lately: https://groups.google.com/forum/#!topic/clojure/znPyDzGkBgA
Especially @alexmiller posts were very interesting
@filipecabaco some feedback and tips:
instead of (take limit (iterate inc 2))
, you'll probably want to use the range
function
Range gives me a 0 and I has a mod that would return division by zero
Not sure how rem will handle it but can also try
not if you use (range 2 limit)
... Damn nice! Thanks @val_waeselynck
(well to be completely isofunctional (range 2 (+ limit 2))
@sveri will take a look
also, interestingly, for this algorithm, step
doesn't need to iterate over all the integers (e.g step = 4 adds nothing to the work done by step = 2); it would be interesting to find a way use your partially-computed sieve to pick the next step at each iteration 🙂
True... That's wasted computation...
@val_waeselynck is right, this version is about 7 times faster for me
(defn sieve
[limit]
(loop [step 2
acc (take limit (iterate inc 2))]
(let [step (first (drop-while #(< % step) acc))]
(if (nil? step)
acc
(recur (inc step) (doall (remove #(rem? % step) acc)))))))
A small change that makes a giant difference... Awesome stuff! Really appreciate it guys 👍
if I need to run a cron job, could cljs be used for that? or should I better try lein-exec?
@willyfrog you could use CLJS or lein-exec, but that's kind of a false equivalency
CLJS is just a compiler, the question is how you will run that cljs/js. you can run a cljs script directly with Planck, or compile to JS and run it e.g. with node
I started the project with lein-exec, but seems a bit slow having to spin the jvm, so I thought that might be a good alternative
But I’m a bit new on many aspects, starting clojure and I haven’t done anything in cljs, so I prefer to ask before wasting too much time in something I’m not sure if it’s doable or if it even would save me any troubles
Doable, definitely. And once it's compiled it will start up much faster than JVM+Clojure (most of the startup time is not really the JVM booting, it's Clojure that's loading)
but... (sorry, there's a but), it does add extra complexity. You'll have to get somewhat familiar with the CLJS tooling, figure out how to do things on a JS based platform. Here it really depends on what your cron jobs tries to do.
get some data from an api, do a couple of queries on a database and show some results
right, both of those things will have to be done in a different way than on Clojure, you can't just take your existing code from one to the other
@willyfrog does startup time matter? I mean, does it make a difference if that cron job takes a few seconds to start or not?
A cron job runs every x seconds, which is exactly what it will do, just with a given startup time which is the same
Haven't tested this, but Clojure can run a good amount faster if you AOT the jar first too, which might reduce the startup time consideration
TBH, I never understood all the complaining about startup time, in almost 3 years this has not been a problem for me. That said, it could be one if I wanted to make cmd line tools with clojure. Or, if I wanted to create android apps, which I both dont do.
I've seen applications (such as https://github.com/snoe/clj-refactor.nvim) which uses the node/npm for dependencies and runs cljs within node.
@sveri One time I wanted to build a command line tool, that's where it bites I think
yeah, I might be being a bit too picky, but was wondering the options available and if it makes any sense what i was thinking
I myself would try to avoid everything that has to do with javascript interop, but again, thats just my opinion which in that case, is not based on facts
@willyfrog lein-exec/boot are good options for running scripts in that way.
@willyfrog grenchman might also be an option https://github.com/technomancy/grenchman
@plexus Would a scheduled operation inside Clojure make more sense in that case? If the JVM is up either way.
you mean scheduling it inside the JVM? you can do that, I don't have a strong opinion on that one 🙂
@dominicm you will have to consider what happens when the jvm crashes, you will need some kind of watching service that restarts it then
Thank you @plexus , I appreciate the help! (from 7 hours ago!)
@plexus Hmm just one question though if you don't mind. How do I "call" it? say I have a guild ID and icon hash (the last line for guildIcon) how would I build it somewhere else in the code? (I know I know, still a noob question)
Awesome. Thank you very much!