This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-10-06
Channels
- # beginners (32)
- # boot (17)
- # cider (4)
- # clara (112)
- # cljs-dev (3)
- # cljsjs (2)
- # clojure (222)
- # clojure-germany (3)
- # clojure-greece (1)
- # clojure-italy (4)
- # clojure-losangeles (4)
- # clojure-russia (46)
- # clojure-spec (24)
- # clojure-uk (71)
- # clojurescript (78)
- # community-development (5)
- # component (88)
- # cursive (6)
- # datomic (7)
- # duct (5)
- # figwheel (2)
- # fulcro (21)
- # graphql (22)
- # leiningen (3)
- # luminus (9)
- # off-topic (1)
- # om (16)
- # onyx (46)
- # portkey (30)
- # re-frame (47)
- # reagent (5)
- # remote-jobs (1)
- # ring (12)
- # ring-swagger (13)
- # rum (1)
- # shadow-cljs (81)
- # spacemacs (1)
- # specter (33)
- # sql (2)
- # test-check (2)
- # vim (16)
- # yada (11)
my java/clj interop test is giving me: java.lang.ClassCastException: Cannot cast android.graphics.Point to android.graphics.Point at java.lang.Class.cast (Class.java:3369) clojure.lang.Reflector.boxArg (Reflector.java:427) clojure.lang.Reflector.setInstanceField (Reflector.java:282) I have this class in the classpath only once.. so this is a classloader issue? I'm trying to set a field in java class with clj
yeah, a classloader issue: point classloader from clj #object[clojure.lang.DynamicClassLoader 0x20c7622 clojure.lang.DynamicClassLoader@20c7622] , classloader for SurveyMap #object[sun.misc.Launcher$AppClassLoader 0x4554617c sun.misc.Launcher$AppClassLoader@4554617c] how the heck do you guys do clj/java interop development? Is there a trick to make all java classes load through DynamicClassLoader?
or perhaps it's the Virgil plugin I'm using (for reloading java) that causes the issue?
no, looks like this is at least somewhat of a known issue (e.g. https://github.com/xiongtx/lein-trampoline-aot). I don't have circular dependencies, I'm in control of loading my plain java classes so I feel like I should be able to instruct my clj to load them all through DynamicClassLoader
;; When conjoining into a map, vector pairs may be provided: (conj {:a 1} [:b 2] [:c 3]) ;;=> {:c 3, š 2, :a 1} ;; Or maps may be provided, with multiple pairings: (conj {:a 1} {:b 2 :c 3} {:d 4 :e 5 :f 6}) ;;=> {:f 6, :d 4, :e 5, š 2, :c 3, :a 1} ;; But multiple pairings cannot appear in vectors: (conj {:a 1} [:b 2 :c 3]) ;;=> IllegalArgumentException Vector arg to map conj must be a pair... ;; And pairs may not be provided in lists: (conj {:a 1} '(:b 2)) ;;=> ClassCastException ...Keyword cannot be cast to ...Map$Entry...
Why would a println affect something like evaluation order? Iām getting a test that passes when I run a macro as a side effect wrapped with println, but fails without. Iāve tried replacing with dorun and doall but still fails
Fails with eval also
If you have a side-effecting function that you want to apply consecutively to a lazy sequence use doseq
.
@the-kenny for
and map
are both lazy, so the side effects wonāt happen until the results are realized
then Iām unsure of you question
@bostonaholic he's asking @alex-dixon
ah, sorry. I just realized that
havenāt yet had my coffee
Itās a thread last macro that threads the result of a macro through a filter and two maps
Thereās a function in one of the map fns that does the side effect
@alex-dixon I'd run it in a repl, to see the result of the macro when it ends with the side-effecty call, to see if what comes back is what you expect.
Forgot to mention it works fine at a REPL without println or dorun or anything
@alex-dixon you might be running into a chunking issue depending on the sample size. I would wager that your sample size in the repl is smaller than your data size outside of the repl.
This could be helpful: https://stuartsierra.com/2015/08/25/clojure-donts-lazy-effects
!!!. @jstew Thank you! replaced map with run!
, works and learned something š
Didnāt know it existed
Neither did we until we found that exact article you were linked to above, after running into lazy-sequence processing issues š
anyone familiar with hara project by Chris Zheng? http://docs.caudate.me/hara/hara-event.html#entry__hara_event__raise
ok thx @sodominic Can you make sense of the sentence on the hara.event website which goes likeā¦ āIt can be noted that the try/catch paradigm can implement sections and . Other clojure restart libraries such as errorkit, swell and conditions additionally implement sections , and .ā , below section 5 - Strategies??
https://github.com/zcaudate/hara/blob/431451dc81215e93b2efcaaf05ed9009ee6eb0d3/test/documentation/hara_event/strategies.clj#L5 this is how it is supposed to read
It makes code need to become aware of exception classes of any library, at any stack depth
so if I want a single-responsibility function I don't wanna handle wrong cases inside the function, I just want to allow someone to handle this
I will say exceptions are a way of life on the JVM, you can't avoid them. Usually I pick a spot in the code where I turn a specific concrete exception into a datastructure... then send the datastructure around the program
exceptions are like side effects, IMO. Unintended side effects that make it hard to write pure functions.
lets say I have a use case "signup user". I have email, password and confirm password. Firstly I need to check that fields are filled, then -- password and its confirmation match. then I need to check that the same user does not exist yet in DB. Then, after creating user, that the user was successfully created
Usually I try to just use some->
if I don't care about which part of the process broke.
and if I do care? I just want to separate the main flow of the use case from the alternative flows
Something like this works well if you do care: https://adambard.com/blog/acceptable-error-handling-in-clojure/
{:user-info {:email ...} :reasons-preventing [:already-present :missing-email]}
and then (if (seq (:reasons-preventing user-analysis)) (bad-response) (good-response (create-user user-analysis))
If you have to branch n times you're doing it wrong. I've played with this too: http://funcool.github.io/cats/latest/
i wouldn't trust anything beyond trivial usage of monads in a dynamic language. i would absolutely hate to pick up someone else's code like that
so I'm getting back to my question -- how to separate main flow and alternative flows without exceptions?..
i like my example. build up an object with the inputs and analysis. you can put reasons preventing creating a user in the db, whatever. and then afterwards a function that takes that analysis object and has a simple cond statement examining the reasons-preventing the creation, and otherwise do the thing you want
(def token-verification-functions [not-breeze-token?
not-recent?
not-verified?
not-user-id?])
(defn- analyze-token*
[decoded-token]
(let [reasons (into []
(keep #(% decoded-token))
token-verification-functions)]
{:valid? (empty? reasons)
:reasons reasons}))
Hi. Iām trying to find a short macro expression for the following:
(-> x
(foo a b)
(foo c)
(foo d e))
Any suggestions? I have the following in mind:
(foo-all x
a b
_ c
d e)
or
(foo-all x
[a b]
c
[d e])
@dbushenko Off the top of my head I would try something like this:
(let [[result err] (-> [[:success, nil] {:name "name" :password "pw"}]
validate-user
check-exists
create-user)])
itās basically a hand-rolled monad
I don't consider exceptions to be that evil, now that we have ex-info
and ex-data
.
OTOH, if you can reduce
all your steps you can short-circuit evaluation by returning (reduced {:fail ...})
...
(try (some complex pipeline that throws ex-info anywhere) (catch clojure.lang.ExceptionInfo e (ex-data e)))
-- one top-level catch
to return whatever data was thrown...
A bit like having transients inside a process and making them persistent to return the value š
Which book?
(sorry if you said earlier and I missed it)
@dbushenko scala hacker? š
Page 304 onward and page 447 onward cover some good stuff about exceptions (assuming JoC 2nd Ed)
It lives by my left hand š
@seancorfield it was the friendliest to common lisp refugees I found. š
@seancorfield looks like ex-info/ex-data is a good workaround for my case
@mseddon Yeah, JoC 1st Ed was my first Clojure book -- but I'd done Lisp and a fair bit of FP in various academic languages back in the early 80's.
Modern clojure/clojurescript is truly refreshing, I didn't think I'd be able to do this seriously. The community is doing an amazing job and I am in awe. Just wish I'd made the jump sooner!
I picked up clojure a bit early on, but it was rather fringe and iirc, there were some nasty bugs in maps that made me decide to sit it out for a while
I picked up Clojure to play with in 2010, been doing it in production since 2011, and it became our primary back end language a few years ago.
@seancorfield maybe you can give us a talk about it on our conference?
I've been very happy with ring, and it looks like spec will get me some way to functional dependant types without the pain of blowing up intellij you get with scala's shapeless š
@dbushenko Pretty easy to mimic Either and related monads with first
/`second` and vector
for the values. A few def
aliases for syntactic sugar and you're golden! š
yeah, but monad is about the syntactic sugar, otherwis it looks like even more complex solution than the problem it tries to solve
Type systems are overrated š (I've had a love/hate relationship with Haskell since it appeared and I worked with a number of its predecessors in the 80's)
Yeah, I'm in two minds with types, I think spec can hopefully find the middle ground. It looks so great until one of your nodes restarts and you goofed and push the rest of the cluster over. š
@seancorfield can't agree with you more. I feel like I'm much more productive in Clojure than in Haskell and even in Java. But I don't know why
sometimes I think that clojure just offers such simple solutions that we don't need to spend the time for investigating complex frameworks
that said i did some nodejs consultancy a few years ago and the scars drove me towards the Haskell/Scala camp š
I hope someone can help me, for some reason I'm unable to get the (reset)
working for a reloaded workflow. I've used this on multiple projects in the past two years now, but it always seems very unstable. Quite often I'm unable to use the reset-fn since it errors that a namespace isn't found... I have checked that the source-paths in project.clj are correct. Anyone with similar troubles?
Ah, yes, JS can inspire a fear of dynamic languages / love of type systems š I've managed to avoid JS almost completely...
@stex There's a #component channel that may be able to give more specific advice. I must admit I've also had problems trying to use that workflow on projects...
@dbushenko initially I was a hairy undocumented sbcl hacker, so lisp is an old friend. My recent desire to learn it fluently is driven by discovering in production Scala is not delivering on types, and cemented by figwheel and general repl development
@seancorfield Ah, thanks, didn't know that. Will ask it there š.
@dbushenko some of what you get with CIDER repl development is 'meh' to a CL user, NASA literally debugged the pathfinder probe at the repl while it was on Mars using lisp- yet my initial forays into what clojure offers has wowed me, which just means I'm here to stay š
let's face it, it's going to be a while before the lisp communities are going to top that anecdote
@dbushenko but if you're of a scala/haskell persuasion, spec being able to emulate scalacheck/quickcheck and provide static checking seems like a massive step towards closing any gap there may be.
it's dependent typing and structural typing in a common definition language, as far as i understand it (please anyone better qualified jump in at this point :))
the only checking it does for functions is generative(generating inputs and checking outputs, no analysis of the code), which isn't suitable for use in production
but for example, it could easily be used to declare structural types, ala typescript, no?
the type system is obviously far to strong to be totally enforced at compile time, but ten years from now, couldn't we see linters using the common format to statically check certain aspects?
absolutely, it would not even be possible to statically enforce the full level of specs
@dbushenko did you ever use property based testing?
hmm. come to think of it, specs structural language may be itself too powerful to do typechecking, predicates or no š
is there a good collection compare for testing when order doesn't matter besides set, or this essentially what any collection compare would do anyways?
i guess that would destroy duplicates in teh general case which doesn't affect me here but presumably this is a standard problem?
my naive answer would be coerce both args to sets, and now look, we don't handle bags š
question while I'm stuck at work late. Why doesn't clojure have a <-
macro? something that takes the last form and puts it at the beginning
it would be really helpful for working with the other threading macros, like cond->/cond->>, some->/some->>, and as->
which require the in threaded value to be first. often you're working with a collection in a thread, so you're using map and filter (which are mostly worked with via the ->>
macro), but you cant do anything conditionally with them, etc. You can easily hop from thread first to thread last, but you cant go the other direction. Let me give an example.
user=> (defmacro f [body] (prn 'f body))
#'user/f
user=> (defmacro g [body] (prn 'g body) body)
#'user/g
user=> (g (f 1))
g (f 1)
f 1
nil
user=>
or (-> (retrieve...) (->> (map ...) (map ...)) (cond->> ...) (cond->> ...))
right, you have nesting options that are less easy, like that @noisesmith. you could also do this (if you wanted to keep each step in the pipe easily re-orderable):
It becomes complex to follow I think, with too many flow rewrites. I find nested -> a bit much at times
@hiredman only if show-inactive? doesnāt need data added by the condition side of show-internal-data?
what I mean is if it needs the change the previous cond made
likewise, if I had a symmetrical <-
, I could have self contained filter steps thrown into the mix.
it just seems to me that, since there is no form of cond->
or some->
or as->
that can possibly be used in a thread last, the existence of a <-
would be an obvious thing.
I may not know what there needs to be, I remember pasting a link to https://gist.github.com/hiredman/184831 in '#clojure' years ago and then ten days later '->>' showed up in core
@seancorfield yeah, that's alright, but I really prefer my pipes to be flat, so I can reorder them easily as requirements change.
maybe I want to filter earlier on to avoid running transforms on all the records, etc.
If you think your code is too complex / hard to read, refactor it into smaller functions.
Yes, a lot of people combine too much code when threading.
could be, but I happen to believe they are often a much better choice to represent a program, if you can keep them flat.
Transducers are often an even better choice.
bad bot
well, anyway, I think my point is pretty well made here. when working with collections, it is often useful to use cond->
or some->
or as->
(insofar as it is useful to use those macros in any context). the existence of a <-
would make that possible, without having to reorganize your thoughts around core's limitations.
you should write <-
then
oh, I have. I was just wondering why it wasn't in core, and thought I'd make the case.
you would give it transforms (as functions) and dependencies between transforms (as metadata on the function if I recall) and it would do the sorting and piping to give a function did what you wanted
Iāve been meaning to try plumatic/plumbing some day
data transforms are almost never linear, you end up wanting something more graph like
(I was a bit surprised comp
osing identity
into the transducer pipeline worked)
in this case clojure is evaluating show-internal-data? as a condition - I donāt think that is what was intended
oh, never mind, it was
I had misread
so you end up with some cross between that and a for comprehension it is very list monad
https://gist.github.com/hiredman/71b71e4e07e7666fe1b9def9a476c765 that took a while to dig up