This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-09-30
Channels
- # bangalore-clj (1)
- # beginners (9)
- # boot (51)
- # cider (20)
- # cljs-dev (419)
- # clojars (1)
- # clojure (338)
- # clojure-brasil (64)
- # clojure-dev (7)
- # clojure-greece (2)
- # clojure-italy (3)
- # clojure-russia (10)
- # clojure-spec (127)
- # clojure-uk (12)
- # clojurebridge (2)
- # clojurescript (132)
- # core-async (8)
- # cursive (37)
- # datomic (34)
- # dirac (5)
- # events (1)
- # funcool (3)
- # hoplon (39)
- # jobs (3)
- # leiningen (3)
- # off-topic (16)
- # om (44)
- # onyx (7)
- # pedestal (20)
- # protorepl (1)
- # random (1)
- # re-frame (64)
- # reagent (6)
- # specter (4)
- # test-check (9)
- # untangled (17)
- # vim (4)
So, I want a kind of inverse of defmulti
, in order to preserve Open/closed principle. defmulti
allows one to extend how values are consumed, but I want to extend how they are produced, allowing each module to register additional producers. There are clearly many ways to do this. Anyone have experience with this sort of thing?
For example, not sure whether I should use atoms or alter-var-root!
to register things as namespaces are required. I guess threads could be an issue.
I've noticed this to be a common pattern for me:
(defn update-some
"Like clojure.core/update but returns `m' unchanged if `k' is not already a
key of `m'"
[m k & args]
(if (contains? m k)
(apply update m k args)
m))
Anyone else?@pesterhazy Not myself, but I can see the utility, particularly if you don't want nil
s in your map.
@dominicm, also if your update fn
doesn't handle nil well
@pesterhazy Yep, but that might indicate problematic code. I was under the impression that functions should generally handle nil
. Even though that isn't particularly true.
no, a lot of interop-related code doesn't like nil
, e.g. clojure.core/name
, clojure.string/*
, clojure.core/inc
also number, string fns
That's true, yes. I wonder when you are supposed to handle the nil case. I've found myself solving the general nil/update problem via a function I called nilf - it checks if the argument is a nil, and just skips if so. Doesn't work so well in multi-argument cases though (which to check!)
@dominicm that sounds a lot like clojure.core/fnil
@joost-diepenmaat It's similar, but different. fnil
is a default argument in place of the nil, this just skips the f
altogether
@eraserhd big question! but i think your looking at atom to be honest, if your looking for a 'fan out' kind of solution. Also worth looking at core.asyc/mult, that's a nice way of fanning stuff. Then add a dash of Component so your namespaces have a life cycle where they can register them selfs in the atom/tap the mult. enjoy!
has anyone ever run into this error while running migratus: clojure.lang.PersistentVector cannot be cast to clojure.lang.Named? It happens when there is not schema_migrations table anymore...
found reference to it: https://github.com/yogthos/migratus/issues/68
I've got some clojure tests that tests code that generates error messages which are logged. The tests aren't failing, it's expected that the code will log errors. This makes for noisy output when running tests. Any strategies for quieting this down? Sometimes I do end up adding other log messages while debugging, so I'd rather not disable logging entirely during testing.
credmp that would be the namespace of the code that's got the log statements, correct? Not the test code.
yeah, that makes 2 of us :)… I was doing a live coding broadcast earlier today and it failed miserably… luckily it was at the end, so I could take some time
@credmp > I was doing a live coding broadcast earlier today and it failed miserably Well there is a workaround. You should always say “See? An error! And in the next episode I will explain the cause of it and how to fix it. See ya!” 😛
One of the mottos of the cast is to tune in to watch me fail, which more then often works
this really feels like a problem that clojure has already solved somewhere, but i’m not sure where to look. can someone point me in the right direction?
I’m trying to write functions that operate on either a single set of data ’(…)
or a collection of datasets ’( ‘(…) ‘(…) )
. Surely there is a better abstraction to handle this case that is not just “write specific helper functions to check seq?
versus not-seq?
at each step"?
@lwhorton the obvious solution is to let the caller decide whether they need to map or not?
for example, if i’m trying to sample a dataset, say naively take every-other item in the dataset, is there a clojure-ey way to write this sample-data function that accepts both a single dataset or multiple?
@nathanmarz with specter can you write a tree walker/transform that's also able to expand/resolve some branches? ex a tree that contains keyword values that need to be replaced/expanded by another tree as you walk it.
@mpenet yes that's easy, use stay-then-continue
here's an example:
(def TreeVals
(recursive-path [] p
(if-path vector?
[ALL p]
STAY)))
(def TreeExpander
(recursive-path [] p
[TreeVals
(stay-then-continue p)]
))
(transform TreeExpander (fn [v] (vec (range v))) [4])
;; => [[[] [[]] [[] [[]]] [[] [[]] [[] [[]]]]]]
So i'm new to clojure and now of course I want to break the rules. I'm using jna which allows me to call a system function that takes a Structure and mutates it. I'm not sure how to allow this to happen and then return the mutated object. I've looked at atoms and refs but they don't seem to allow you to use a function to mutate the object unless it returns that value. Where should i look?
I'm not too worried about the mutation because I'm creating the object being mutated inside the same function that calls the mutator so it can be a "black box."
@dominicm so a simple example of my problem is that the function i'm using is basically like the anonymous function here and I cant change it. I kinda wanna pass the hashmap by reference.
There's a subtle bug in your code, I'm not sure why it doesn't throw, but it's a mistake
With the threading macros (`->`, ->>
, doto
, etc.) there's a behaviour that sometimes people run into. It's a little confusing. I'll try my best to explain it.
(-> m
(fn [m] (do-thing-to m)))
Actually expands to:
(fn m [m] (do-thing-to m))
So you have to wrap it twice, so you get:
((fn [m] (do-thing-to m) m)
Does that make sense at all?@dominicm double-wrapping is cool, thank you! It’s obvious when I think about it, but it never occurred to me. I always defined the function (in a let
or letfn
) before starting the threading block.
@quoll Unless it's for debugging, I'd generally recommend against it from a stylistic point of view. I find it harder to read. You might find your letfn is significantly clearer as you spend time giving names to your actions.
I’m typically happy with it, yes, but sometimes it’s a trivial wrapper that’s not worth it
use it like
(concurrently {+ [- /] * [/ + inc] dec [*]} (constantly 5) (constantly 10))
Hi, as an experienced Java developer I find it difficult to solve problems the functional (recursive) way. Which books would you recommend? I have already read Brave-Clojure and Joy-of-Clojure. Now I am looking for something what focuses even more on recursive algorithms, etc. Or do you know any other good learning resources for this purpose?
SICP is also always a good choice to read, but that's a lot of book just to get better at thinking recursively
sicp site with downloadable ebooks and other things: https://sicpebook.wordpress.com/
I'm trying to convince my classmates to use clojure on our undergraduate capstone project
@witek IMO, if you've already read The Joy of Clojure, I don't think what you need is more reading. I'd throw myself into coding a real project and applying what I'd learned if I were you.
I'd probably agree with that. just try and solve normally iterative problems using for loop
, and then see if they would be a good fit for reduce
. the little schemer is nice though because you can burn through it in a plane ride
every time anyone says "little schemer" i get a picture in my mind's eye of some mischievous cartoon character rubbing his hands together and grinning menacingly.
in reality, not so menacing - https://mitpress.mit.edu/sites/default/files/9780262560993.jpg
Hi! I created bidirectional routing library based on core.match. I'll be glad to hear any feedback. https://github.com/darkleaf/router
Is there a way to tell clojure.data.xml to stop throwing errors because of "Unknown xmlns for clj ns"?
I'm coding a WebDAV implementation, and apparently I'm supposed to handle XML with unknown XMLNS.
I’m using agents in my code for the first time. Any recommendations/best practices/ things to watch out for? 🙂
i'm super comfortable with functional programming ideas, but I don't know anything about the web
You might want to take a look at Luminus @bcbradley. http://www.luminusweb.net/
i'm afraid if i concentrate too much on a single framework, i won't understand the principles or concepts that make it work
I was thinking more that it might have the intro learning material about ring you were looking for
sadly I don't know much about the clojure http world, but what web app experience I have started with rails, and it definitely meant that it was a much longer time before I really understood how all the pieces worked than it should have been
i'm looking more for like "what is a cookie" "what is a session" "whats the standard workflow between server and client" etc
(but we did get our app out there, and I was being paid, so on the whole it was still the right thing to do)
and every time i've ever had to do some kind of networking, i did it with low level sockets
Well, that probably saved you a lot of trouble. I always did my own stuff, too, and now I have to implement WebDAV. Dear gods!
this looks like what i was looking for https://player.oreilly.com/videos/9781783286157
then i'll know exactly what questions to ask google about in context of that workflow
if I wanted to learn web apps from the ground up personally, I'd implement a todo list app that had users with login and sessions in just ring, then ring+compojure, then ring+compojure+hiccup, etc etc
bcbradley - a good place to start imo would be to learn about http - you’ll want to learn about the http request/response cycle, and see some examples of what http requests and responses look like
the curl
command might be a surprisingly good learning tool - if you do man curl
you should be able to find various knobs that let you see real live http requests and responses
once you have a good understanding of what requests and responses look like, the rest of it will hopefully be a little more easy to grok
just like take a look at http://yelp.com or whatever using devtools and curl
oh lol you’re already well past that, aren’t you? didn’t see you’ve already done things with rails
i wanted to give back to the clojure community but i didn't even know it existed 6 months ago. I've been spending all my free time learning as much about it as I can, making dinky little programs and trying to compare the ideas Rich has about what makes program development difficult with my own experiences. I can confidently say that I have become more enlightened in the short time I've had with clojure than I had in the 5 years I had with java or 8 years with C before then.
Don't get me wrong, I can make stuff in plain old c. But it doesn't feel to me like i have control over how complicated the project can become. I feel like I don't have many tools at my disposal for managing incidental chaos or complexity
I've fully embraced the functional way of thinking, but I don't know alot about the web. I figured I could both give back to the clojure community and learn about the web by making something the clojure community doesn't have yet
I guess my biggest road block right now is it feels like everyone who's in the business of making web apps knows how web apps are made and can't imagine anyone not knowing how they are made 😕
Is there anything else that seems fundamental? Something that doesn't change? I don't wanna learn about some technology that is just "today's soup of the day"
and assume I know how computers and networks work at the physical or data link layer
heh, that’s a super great question but unfortunately 95% of web programming is “soup of the day"
a fundamental thing in web programming is making http requests to a url and getting a response back
websockets are a bit advanced, i’ve never used them myself, you can put them off - css is fundamental so that’s a great question
(if this is the wrong room for this, someone please speak up and i’d be happy to take this elsewere)
web programming basically involves (at least) three fundamental pieces of tech - html, css, and javascript
you’re likely familiar with html already, or at least have seen a few html tags - the html is basically the static content of the page, the words and some hints about the semantics of those words (these words are a paragraph, these other words are a large heading)
css - cascading style sheets - is a piece of tech that can be used to make your html document look pretty when viewed in a web browser
css is the reason that you see pretty colors and nice layouts when you view web pages
i haven’t yet mentioned the distinction between the frontend and backend of your web app
the three pieces of tech i mentioned - html, css, and js - are used in the frontend portion, and if the frontend portion of your webapp is a car, then js is likely the engine
it lets you write code that says “when the user clicks on this thing over here, make some side effect happen over there"
if you’ve used, for instance, a web email client like gmail, and you’ve been able to do a ton of things - send emails, view emails, etc - without leaving the web page you’re on, you’ve been using javascript
it’s an (oft-maligned) API that lets javascript code modify the contents of the html page it’s running on
so if i'm understanding this all correctly, html is like the "state" of the program in one sense-- the layout and scaffolding
it can affect layout quite a lot
https://forum.justgetflux.com/plugins/nodebb-plugin-markdown/styles/railscasts.css here’s an example css file fwiw
so it looks kind of like both css together make up the "state" of the page in a certain sense
there are perhaps several categories of state in this context, i don’t want to commit to saying that html/css are your page’s state
because yes, the page’s html is definitely a kind of state, it’s the current set of words, tags, etc that you’ve got on your page
Why seperate state like this? Is it just a coincidence in how the internet browsers developed?
here’s something you might find useful - go to http://www.csszengarden.com/
if i remember correctly, each page you are taken to has the exact same html, but is decorated / laid out by using different css files
i guess the idea is about separation of concerns - html for content, css for describing how it should be laid out / etc
html documents can also be read by screen readers - tools that folks who can’t see well use, which read web pages to them out loud
definitely, yeah - you can write arbitrary code in javascript that does whatever it wants to the webpage’s html and css
So instead of learning 3 syntaxes and 3 semantics, I could learn 1 syntax and 3 semantics
one thing to know about is that historically, pages whose html is generated entirely by javascript have been hard to find via e.g. http://google.com
(there’s this whole category of thing called SEO, search engine optimization, where you try to do certain things to your html and css to get your web page to rank highly in google)
i can definitely see the attraction of just learning JS and using it to generate all your html and css, but i don’t think i’d recommend that - html, css, and js are fundanental to web programming, it’s best to learn them all, they’re sort of first principles and understanding them / being comfortable with them will make the rest of it go easier for you
so you’ve got html, css, and js on the frontend - the stuff that’s going on in your user’s web browser
and on your backend, you’ve got arbitrary programming languages doing whatever you want, talking to databases, doing whatever - you can use clojure for this, or java, or python, or php or whatever you like
hm, again that’s a good question but i’m not sure i’d quite put it that way - let’s set discord aside briefly but return to it in a second
so i worked at yelp for a while, and what happens if you type http://yelp.com into your browser and hits enter is: your browser sends an HTTP GET request to http://yelp.com
it eventually gets routed to a particular machine that yelp controls that’s running the yelp “webapp"
it runs some python code, talks to databases, does whatever else it wants, and then eventually responds to your browser with an HTTP response
the response has some headers and then a bunch of HTML, and your browser uses this to draw pictures / etc on your screen
you’re likely familiar with the fact that html documents make heavy use of tags, like <p>hello there</p>, etc
html documents can also contain tags that say: hey web browser, i’d like you to also go get some css and javascript, and it lives over here
when the browser encounters one of these tags while processing your HTTP response’s html, it’ll kick off another http request to fetch that resource
yup! and sometimes (often) it’s not even to the same server, it’s to some other sever somewhere else
(this reminds me, you’ll encounter the concept of a “CDN” at some point, look that up at some point, it’s a pattern used to mitigate the speed of light in this context and help make web pages load more quickly)
so is this "link" to be interpreted and treated sort of like a pointer in the c memory model?
or is it to be interpreted like "inline command" where, the browser parses the command and injects whatever it got from whatever server
i have very little c experience, so i’m hesitant to commit to one of those, but the second one sounds more like it
your analogy questions are good but i worry that i’m going to give an answer when the analogy isn’t actually a precise fit and i’ll have sent you down a wrong path
in a web browser (i use chrome, but obviously you can use whatever), go to https://clojure.org/
if you’re using chrome, go to your menu bar and go to view -> developer -> view source
just, like, look at that html for a while, see the different types of tags in it, click on whichever ones look like links, notice that the page uses a bunch of different css files
then go back to http://clojure.org and (again, this is in chrome), go to view -> developer -> developer tools
this’ll let you see all the (many, many) subsequent http requests that your browser kicks off after it gets back the initial http response from http://clojure.org, the one that has all the html you saw in view-source mode
you’ll see things like “Method”, with values like GET, POST, etc - learn about what those are, what their semantics are
and if you click on an item in this network pane, you’ll be able to see info about the related http request/response
anyway i should get back to whatever i was doing when we started talking 🙂 but i hope you’ve got a slightly better picture of what web programming looks like
the frontend has html, css, js; the backend has arbitrary code in arbitrary languages; they communicate via http
i’m @jrheard on twitter if you find yourself stuck, and also i should usually be around here 🙂
@bcbradley there is a recend book "Web Developmet with Clojure, 2nd edition" that could help you
(also re: your question about why html and css are separate - imagine a website that has ten or fifteen pages, whose html is written by hand - all ten or fifteen of those pages can be made to have a consistent look-and-feel by having them all refer to the same single css file; and if two years from now you want to redesign your website’s look-and-feel, you can (ideally) do that by just changing the one css file, rather than having to modify tens or hundreds of html pages)
sort of! or about maintainability, ease of making style changes without wanting to kill yourself, etc
in reality your html and css often end up more tightly coupled than i’ve made it sound here, but reality’s always messy 🙂
so the ideals of seperation of concerns and forcing functionality to go through interfaces still applies to this as it does to any program right?
css is good for making it easier for you to change your company theme / brand painlessly
so "json through http" means instead of asking for a webpage you are making a GET request to the server for plain old text
although you’ll want to be careful re: “link” terminology - there’s also the <a href=“foo.com”>foo</a> tag in html, which is what users can see and click on and often think of as “links"
yeah, that’s more like it - there’s a field in the response called (i’m a bit fuzzy on this, please correct me anyone else who sees inaccuracies here) the MIME type, and it tells your browser (or whatever other arbitrary computer program made the http requst, eg your clojure code): “hey, you’re about to see some text, it’s JSON” or “it’s HTML” or “this blob of data you’re about to see is a jpeg file"
i think https://www.w3.org/Protocols/rfc1341/4_Content-Type.html is the documentation of that field, the content-type header
i’m less confident about the past two lines than about most of the other stuff i wrote above, though 🙂
https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html looks like an exhaustive list of http headers, could be useful
per that document, an example content-type header you might find in an http response: Content-Type: text/html; charset=ISO-8859-4
ok i’m seriously going to turn this off for a bit now, this was really fun but i should get back to what i was doing 🙂 before i forget, you’ll also want to learn about how a client can communicate information/parameters in its http requests
learn what the "query string" is in the context of a GET request, and what a POST body looks like
given how long the web has been around, they have done a remarkable job at keeping everything from becoming a c++ish mess