This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-09-17
Channels
- # 100-days-of-code (4)
- # announcements (4)
- # aws (2)
- # beginners (88)
- # cider (1)
- # cljsrn (9)
- # clojure (126)
- # clojure-conj (4)
- # clojure-dev (8)
- # clojure-greece (1)
- # clojure-italy (37)
- # clojure-nl (3)
- # clojure-spec (13)
- # clojure-uk (91)
- # clojurescript (392)
- # clojurewerkz (1)
- # clojutre (10)
- # core-async (6)
- # cursive (5)
- # data-science (1)
- # datomic (41)
- # emacs (21)
- # events (1)
- # figwheel-main (52)
- # fulcro (2)
- # hyperfiddle (4)
- # jobs (3)
- # jobs-discuss (9)
- # luminus (3)
- # lumo (9)
- # mount (1)
- # nyc (1)
- # off-topic (73)
- # other-languages (6)
- # pedestal (8)
- # portkey (2)
- # re-frame (9)
- # reagent (8)
- # rum (17)
- # shadow-cljs (38)
- # sql (19)
- # tools-deps (18)
- # yada (4)
If any of you happen to be in Manchester, UK, we're running a beginner Clojure workshop this evening (with free pizza/booze): https://www.eventbrite.com/e/lambdalounge-clojure-workshop-tickets-49803651061
an instant doesn’t have a day, you need to convert it to a LocalDateTime or ZonedDateTime in the appropriate zone and then get the day from that
Also, an instant could really be any value that satisfies inst?
, so one approach would be to inst-ms
and then use some means to convert that value to a day.
People learn from mistakes. There is nothing in it to be ashamed of. 😀
i keep running into a ‘no matching constructor found’ issue when trying to instantiate a new object. as far as i can tell i’ve imported the core library correctly
assume I have a seq (list of vector) of strings, what would be a concise way of creating an english language list string of them, e.g. elem1, elem2, and elem3
? I know cl-format
can do this but lets for the sake of argument assume I want to accomplish this without cl-format
. I.e. the goal is to have commas in between except for the last one which should have either , and
or just and
(defn english-list [coll]
(let [j clojure.string/join]
(j [(j ", " (drop-last coll)) ", and " (last coll)])))
@gtzogana java allows overloading on type. for instance if new Foo(3)
and new Foo("baz")
are both constructors, if you try (Foo. 3)
it might throw an error saying there is no Foo (Object)
method. Can you read the error message for which constructor it is trying to find? Possibly a typehint might work. (Foo. ^long 3)
unfortunately all i get is CompilerException java.lang.IllegalArgumentException: No matching ctor found
.. not particularly illuminating for me
@mbjarland That's a nice problem. This can do it, but it produces an odd result if the sequence is of length 2
(defn english-list [coll]
(-> coll
reverse
(interleave (cons ", and " (repeat ", ")))
reverse
rest
clojure.string/join))
unfortunately all i get is CompilerException java.lang.IllegalArgumentException: No matching ctor found
.. not particularly illuminating for me
@mfikes :thinking_face: thanks, I’ll mull that over for a bit. I was going with interpose
and then replacing the last comma with an and…but I don’t like how it looks
@mfikes the “replacing the last comma with an and” turned ugly (or rather I could not cook up anything pretty), you have to handle special cases like the list only containing one item etc
(defn english-list [coll oxford] (if (= (count coll) 1) (apply str coll) (let [j clojure.string/join] (j [(j ", " (drop-last coll)) (when oxford ",") " and " (last coll)]))))
ill be waiting for those bonus points
I guess if you don’t want to use join you could replace the innermost j
with interpose and the outer one with (apply str
?
(apply str instead of the first j works yeah
Maybe something based on assoc
? Partial soln:
(defn english-list [coll]
(let [v (vec (interpose ", " coll))]
(assoc v (- (count v) 2) ", and")))
@coinedtalk would that not fail for one item lists?
let me check
oh no
well I guess I didn’t explicitly state that so incomplete problem description, but yes, it should also work for one item lists
@mfikes that’s pretty much what I was going with and then I ended up with (if
handling for the one item case
If you go with assoc
you can also work the transducer variant of interpose
in the mix and probably end up with a wicked fast solution for vector inputs.
like I said above, this is possible with cl-format:
(clojure.pprint/cl-format nil "~{~a~#[~;, and ~:;, ~]~}" ["alice" "why" "is"])
=> "alice, why, and is"
but I was hoping there was a non-cl-format wayin fact it seems that for this to work properly for 2 element lists you need to go neanderthal with the cl-format format string:
(clojure.pprint/cl-format
nil
"~{~#[~;~a~;~a and ~a~:;~@{~a~#[~;, and ~:;, ~]~}~]~}"
["alice" "why"])
=> "alice and why"
That's cool. Many ClojureScript devs will avoid clojure.pprint
owing to its effect on code size. But, cl-format
is curiously strong, leading to nice compact solutions.
we are essentially asking the reader to learn a whole new language, the cl-format “format string language”
cl-format seems to have some serious pedigree, from what I can glean the code has more or less been around and transplanted into the following languages / envrionments:
BOS -> multics -> lisp machine lisp -> common lisp -> clojure
for an explanation search for the format string at: http://www.gigamonkeys.com/book/a-few-format-recipes.html
well since we are gorging on write-only code, the following does word wrap using cl-format:
(defn wrap-line [size text]
(clojure.pprint/cl-format nil (str "~{~<~%~1," size ":;~A~> ~}") (clojure.string/split text #" ")))
(defn english-list [args]
(let [pa (fn ([] args)
([word & rests] [word ", " (last rests)]))]
(clojure.string/join ""
(flatten
(take (dec (* (count args) 2))
[(reduce pa (butlast args)) ", and " (last args)])))))
look at this monstrosityan attempt:
(defn english-list [args]
(condp = (count args)
0 ""
1 (first args)
2 (str (first args) " and " (last args))
(let [xs (interpose ", " args)]
(apply str (flatten [(butlast xs) "and " (last xs)])))))
I wanted to make one using map and cycle
why not (concat (butlast xs) [" and " (last xs)])
- flatten actually works here but has so many gotchas
Like what?
like performing very slowly, and turning general code into special case code that breaks with collection inputs
And it might split up a string into chars
(of course you only get strings here, so the collection input case which is the real gotcha, doesn't apply)
flatten does ignore strings
@noisesmith duly noted, result:
(defn english-list [args]
(condp = (count args)
0 ""
1 (first args)
2 (str (first args) " and " (last args))
(let [xs (interpose ", " args)]
(apply str (concat (butlast xs) ["and " (last xs)])))))
(english-list ["alice" "bob" "mary"])
=> "alice, bob, and mary"
reduce would be weird, as it can't peek forward
(so you end up making a cache of values to simulate a forward peek, and a loop would be simpler than that)
Has anyone gotten Ring Session to work with luminous? I've only seen examples where they use flash sessions.
@mbjarland It is now just a short step from your last version to a transducer / vector based version that runs twice as as fast in ClojureScript and thrice in Clojure
(defn english-list [args]
(case (count args)
0 ""
1 (first args)
2 (str (first args) " and " (last args))
(let [xs (into [] (interpose ", ") args)]
(clojure.string/join (into (pop xs) ["and " (peek xs)])))))
- case
does a constant-time dispatch
- (interpose ", ")
is a transducer and if args
is a vector, then things can be directly reduced via into
(no intermediate sequence)
- pop
and peek
are faster
- I think clojure.string/join
is faster under ClojureScript
Guys, do you have some article or kind of exercices about core.async
? I’m in trouble with that, I couldn’t understand it yet (I came from a world without threads)
Oh, I’ll look it
@mfikes, yep, Chapter 5
@caio.cesar.g.souza there are a couple of videos here on core.async: https://cognitect.com/videos.html
Oh my god! Thanks so much!
I just published a short beginner-friendly blog post that introduces the Clojure CLI tool: http://blog.klipse.tech/clojure/2018/09/16/hello-clojure.html
Thanks @UAX2MK4PP Were you already familiar with the CLI tools?