This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-11-28
Channels
- # admin-announcements (1)
- # aleph (3)
- # bangalore-clj (4)
- # beginners (167)
- # boot (14)
- # cider (4)
- # cljs-dev (24)
- # cljsjs (21)
- # cljsrn (33)
- # clojure (214)
- # clojure-greece (2)
- # clojure-italy (2)
- # clojure-korea (16)
- # clojure-russia (29)
- # clojure-spec (31)
- # clojure-uk (86)
- # clojurescript (144)
- # core-matrix (2)
- # cursive (37)
- # datascript (5)
- # datomic (104)
- # devcards (2)
- # events (2)
- # jobs (2)
- # luminus (8)
- # midje (1)
- # nyc (4)
- # off-topic (1)
- # om (39)
- # om-next (1)
- # onyx (47)
- # perun (1)
- # planck (6)
- # proton (2)
- # re-frame (25)
- # reagent (40)
- # spacemacs (9)
- # vim (71)
- # yada (3)
(all-ns) returns a sequence of Namespace objects, e.g. "#object[clojure.lang.Namespace 0x3e44f2a5 "http://clojure.java.io"]. Is there a way to simply extract the string portion of the object?
More generally I'm curious what generic methods are available for inspecting data of type #object – are there others besides (meta obj)?
#object is just how the printer represents the object to you, a clojure.lang.Namespace is just a Java object
Hmm. That gives me "(clojure.core/map (fn* [p1__35809__35810__auto__] (.getName p1__35809__35810__auto__)) (clojure.core/all-ns))"
Hi all, Anybody got tips on making http requests to a unix socket file?
ie in curl you do curl —unix-socket /var/run/docker.sock
I’ve worked out how to do it in python. but not sure how in clojure
@underplank well, im not sure whether that would be possible at all using the current setup of http client libraries. i think this should be doable writing a clj-http interceptor, though
@lmergen hmm.. I havent heard of interceptors before. Do you have a link to more information about them and what they can do?
Do Namespace objects (as returned by clojure.lang.Namespace) implement any of the other methods defined on the Java "Class" API, or just .getName?
@underplank an interceptor is nothing more like a middleware pattern that some libraries use -- clj-http happens to use them.
basically, it allows a third party to 'intercept' certain parts of execution of a library, allowing for very flexible extensibility of libraries, and/or separation of concerns
hmm.. ok i’ll have a look at it. thanks for the pointers.
however, i'm not sure whether clj-http's interceptors are actually modeled well enough to support what you want; i see they have a 'response interceptor', but what you actually need sounds a lot more like a 'request interceptor'
with assoc, I can do (assoc m :a 1 😛 2 :c 3); is there a way to do (assoc-in m [:a :b] 1 [:c :d] 2 [:e :f] 3) ?
the rationale is probably just “we didn’t do it, and there hasn’t been a compiling need to yet either"
There is an enhancement ticket for it somewhere
Although it brings up some interesting questions
@martinklepsch http://ioavisopretty.readthedocs.io/en/latest/ansi.html I was thinking of this ns in pretty
Is there no built-in function that returns the current ns? Any representation would do (object, string, symbol...)
I can import a library if necessary, I just haven't found a solid way yet to do this by any means
hello can anyone tell me how i would create a new instance of a Memory Stream given
object MemoryStream {
protected val currentBlockId = new AtomicInteger(0)
protected val memoryStreamId = new AtomicInteger(0)
def apply[A : Encoder](implicit sqlContext: SQLContext): MemoryStream[A] =
new MemoryStream[A](memoryStreamId.getAndIncrement(), sqlContext)
}
do you guys use record
to represent your business domain objects or just a map ? I’m using records to represent my domain objects and don’t know if it is appropriated ! any objections/thoughts ? User / Account / Tasks / Jobs
are some of my records. and each one implements his own protocol ( in OO would be the business methods )
records are good for that. generally, you probably don’t want/need a protocol for each record though, so I’d say don’t start in that direction. just write functions.
use protocols when you need class-based polymorphism
Bit of a novice question maybe, but... I use @weavejester 's aws library to download a zipfile off AWS S3. Now i have an S3ObjectInputStream, but how can I write this to a .zip file on disk?
@kah0ona it is idiomatic to use java streams via the
namespace which is included in clojure itself.
You can open an output stream on a file via io/output-stream
, wrap that in a java.util.zip.GZIPOutputStream
, and then use io/copy
to copy the bytes from the input stream to the output stream. Look at the with-open
macro to make sure your streams get closed correctly
thanks @alexmiller
the hassle of using record, on my point of view, is that from every database query I have to map the result to the record
. @alexmiller is it normal that , the mapping or should I use maps on that cases ?
if you don’t need a record, then a map is perfectly fine too
well, I co-wrote Clojure Applied and the first chapter is about exactly this. not sure I can fully cover it in chat :)
it’s on sale right now at 40% off the ebook
yeah, use code turkeysale2016
@oliv let us know if it answers your question - i’ve been programming toy projects in clojure for five years and to my eternal shame i still have not yet written a single record or protocol, i just use maps and functions
records are faster and more memory efficient in some ways (but have tradeoffs in others). records give you a known type, which is useful for polymorphic dispatch (if you need it).
i guess i just don’t often need polymorphic dispatch, or at least i haven’t so far - i’m sure it’ll come up one of these days
I’m handling websockets events and I’m using defmulti/defmethod to dispatch properly the message to be handled
dispatching with protocols on records types will be faster than multimethods on maps
that’s probably what i’ve been missing - since i only work on my own tiny projects, the extensibility factor isn’t always super apparent
either of those approaches is extensible, so no real difference there
> i can see how records/protocols would fit in in that situation I can’t yet @jrheard 😞
i think you’d have each websocket event be a different type of record, and each record type would implement your IWebsocketEvent
protocol’s handle-event
function (i’m sure that these names are bad and that pros would use better names)
then you just invoke handle-event with your event and it will choose the implementation based on the type
I just have to create the record based on the event type field and call the handle-event function
the JVM is highly optimized to do type-based dispatch (b/c Java) and this taps into that
multimethods require you to a) invoke the dispatch function (class), then b) find the best matching method and c) invoke the method. so they are slower
http://insideclojure.org/2015/04/27/poly-perf/ https://github.com/puredanger/poly-timing
if interested in more info along those lines
sure, thank you @alexmiller
Is there a channel here for spectrum? The project trying to check at compile time, portion of spec?
@alexmiller and @jrheard that is how I’m doing
(defn fetch-record [msg]
(let [type (msg "type")]
(case type
"message" (map->Message msg)
"reply" (map->Reply msg)
"upload" (map->Upload msg))))
(defn on-message [msg]
(let [record (fetch-record msg)]
(handle-message record)))
is there any way you can simply construct msg as a record in the first place?
as you have it written, this is a closed system as the case encodes the valid message types. replacing that with a multimethod would make it an open system where new message types could be added later.
the on-message
is the callback from my websocket , I’m receiving the raw message there
yeah, just (defmulti on-message [msg] #(get % “type”))
or something like that
then add defmethods for each type that returns the record
(defmethod on-message “message” [msg] (map->Message msg))
thanks for your time @alexmiller
Is there any Socket REPL clients yet? Besides Tubular?
What is the best way to read json from a external api , Use slurp or use clj-http.client ?
how would one match a "
in a string?
(re-matches #"\"(.*)\"" "this is a \"quoted text\"")
i've tried to (re-pattern "\"(.*)\"")
but it still doesn't workHello guys, I'd like to write a spec that validates that validates that if one specific key is present, an other one should not be in the map example:
{:id 1234, :name "Baptiste", :surname "Newbie"}
If :name
is present, my spec should validate that :surname
is notI looked at or
but could not find a way to make it work
re-matches
tests whether the entire string matches the pattern; re-find
searches for matches in the string for the pattern.
@fellshard actually my example does work, here's the full problem,
text to be parsed:
100.100.101.200 - - [28/Nov/2016:12:32:48 -0500] "GET /pro/p/0004f26b0ff8 HTTP/1.1" 404 5 "-" "FDM 3.x" "-"
want to pull the IP, the timestamp and the request, they are apache log stats, unfortunately can't use [apache-log-parser "0.1.0"]
as i don't know the access log format. i need to pull the IP, timestamp, request and the response code, this is what i'm using
(re-matches #"([^ ]*).*\\[(.*)\\].*\"(.*)\".*([0-9]*).*" line)
but it returns nil
, tried to double/triple escape "
but no luckHave you tried temporarily replacing the string-capture portion with wildcard to see if that is the root cause?
I can actually see that regex getting greedy at the end and grabbing all the way from "GET ...
to ..."-" "
Too much freedom, needs more precision on where you're using wildcards and where you're using whitespace...
i've replaced "
with
and doesn't work, seems to be a problem with my regex not
"` , thank you all, will sort my buggy regex
Wierd, I requird cheshire like this in my repl : `(:require '[cheshire.core :refer :all]) => nil `
then I do (parse-string ..... ) and I see this error message :
Unable to resolve symbol: parse-string
at the repl you need to use the require
function, not the :require
keyword from the ns form
the reason you didn't get an error is because keywords are functions that take collections and try to look themselves up in the collection
I wonder if with clj 1.9 you may eventually get an error because a list collection is not associative...
yeah I just checked with my version of 1.9 and it's not currently an error. doesn't mean it won't eventually be though
although.... keywords being functions is probably implemented in java, so maybe not doable/easy
Next problem with parsing :
(parse-string(client/get ""))
ClassCastException clojure.lang.PersistentArrayMap cannot be cast to java.lang.String cheshire.core/parse-string (core.clj:206)
(parsed-seq(client/get ""))
ClassCastException clojure.lang.PersistentArrayMap cannot be cast to java.io.Reader cheshire.core/parsed-seq (core.clj:310)
client/get is returning a response that isn't what either parse-string or parsed-seq requires. I would look at the output of client/get and figure out how you need to modify it to suit the requirements of parse-string
oke, I make a lpaste for it. It's a big reponse. you can find it here: http://lpaste.net/347571
then you need to call :body on the response to client/get before you pass it to parse-string
yep, then I see this :
{"elapsedMilliseconds" 52,
"count" 4736,
"artObjects" [{"longTitle" "Portretten van Giuliano en Francesco Giamberti da Sangallo, Piero di Cosimo, 1482 - 1485",
"hasImage" true,
"principalOrFirstMaker" "Piero di Cosimo",
"id" "nl-SK-C-1368",
"headerImage" {"guid" "76249f29-b230-41ac-aa38-1dcb8476b614",
"offsetPercentageX" 50,
"offsetPercentageY" 50,
"width" 1919,
"height" 459,
@roelofw have you gone through clojure for the brave and true and some of the clojure koans?
ok cool. it's just pulling parts of a map out, mapping over internal parts of a map etc are all covered in those sources
mm. I think you probably want:
(-> (client/get ...)
:body
parse-string
(get "artObjects")
(->> (map #(get % "id"))))
that last bit is probably trickier than necessary and would maybe be better assigning the result of (get "artObjects") to an intermediate variable
oke, I see this output :
(get "artObjects"(parse-string(:body (client/get ""))))
=> nil
@roelofw one thing I do with cheshire is parse for keywords. Personal preference, but I find keywords to be more versatile. Then (-> (get ...) (parse-string true) :body :artObjects)
is a second way to make it work.
@slipset I see it here https://github.com/clojure/clojure/blob/clojure-1.8.0/src/clj/clojure/core.clj#L1691
@tom: I also looked at the source, but it didn’t really tell me that much, hence my question.
if you look at the corresponding defmacro
for defmulti
, docstring
is mentioned, and handled.
Thinking a bit further, it’d be kind of hard to do, since doc
would then have to take a dispatch value to be able to show the right documentation.
or (doc my-multi-fn)
would have to show docs for all known defmethods
with their corresponding dispatch value
Defmethods don't have docstrings
@alexmiller: thanks for confirming (and by waiting a bit, you let me figure out at least one reason why it is so)
hi guys, im getting a pretty confusing error message trying to load a clj file in the lein repl:
CompilerException java.lang.Exception: Found lib name 'datomic-schema.schema' containing period with prefix '[datomic.api :as d]'. lib names inside prefix lists must not contain periods
The offending code looks like this:
(ns stickar.auth-setup
(:require [[datomic.api :as d]
[datomic-schema.schema :refer :all]
[stickar.common :as c]]))
Any thoughts on what I'm screwing up here?Hi! could you help me translate this java code
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator("{0}", NumberFormat.getInstance())) ;
into clojure?My attempt:
(.setBaseItemLabelGenerator renderer (StandardCategoryItemLabelGenerator. "{0}" (.getInstance (NumberFormat.))))
ah nice!
thanks!
What's a good library for authenticating users of a web application against facebook and/or google?
Hmm, that doesn't look maintained