This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-30
Channels
- # aleph (1)
- # beginners (126)
- # cider (2)
- # clara (38)
- # cljsrn (2)
- # clojars (2)
- # clojure (49)
- # clojure-dev (31)
- # clojure-dusseldorf (1)
- # clojure-finland (1)
- # clojure-france (6)
- # clojure-italy (13)
- # clojure-nl (12)
- # clojure-russia (9)
- # clojure-sg (1)
- # clojure-spec (33)
- # clojure-uk (83)
- # clojurescript (206)
- # community-development (3)
- # core-async (40)
- # cursive (4)
- # datomic (7)
- # duct (21)
- # emacs (9)
- # fulcro (36)
- # funcool (2)
- # graphql (12)
- # instaparse (4)
- # jobs (4)
- # lumo (24)
- # mount (1)
- # nyc (4)
- # off-topic (29)
- # onyx (1)
- # pedestal (2)
- # random (4)
- # re-frame (60)
- # reagent (136)
- # remote-jobs (1)
- # schema (1)
- # shadow-cljs (20)
- # spacemacs (6)
- # specter (14)
- # tools-deps (2)
I’m currently using type
but I can’t figure out exactly how I should dispatch on e.g. if a value is a function
right now I’m using
(defmulti create-element type)
(defmethod create-element cljs.core/Keyword [v] "key")
(defmethod create-element (type (fn [] "k")) [f] "function")
but type (fn [])
seems gross@lilactown you could use a protocol for this
One of the reasons why protocol exist is that you can extend native types
so basically I would create a protocol for my create-element
fn and then extend e.g. cljs.core/IFn?
You can extend functions directly, see this docstring https://cljs.github.io/api/cljs.core/extend-type
How do I turn #js{"a" "b"}
into {:a "b"}
while avoiding js->clj
?
this is one way https://github.com/thheller/shadow-cljs/blob/master/src/main/shadow/json.cljs#L4-L37
Thanks, that's useful
For the non-recursive case, this should work
(defn js-obj->hash-map [o] (->> o js-keys (mapcat (fn [k] [k (goog.object/get o k)])) (apply hash-map)))
yeah but much less efficient, reduce will be much faster. could even add transients on top for extra boost.
For arrays there's simply vec
but hash-map
won't work for js-objects
Hi, I am trying to setup a new clojure script build using the new tools stuff (deps.edn, clj) and have run in to a few issues. When I run clj --main cljs.main -c sam.core -r
it works and builds however when I run the browser it is trying to connect to the repl on localhost. Fine, but I am running it in a vm which is on 192.168blah. So I'm trying to set the host using clj --main cljs.main -c sam.core -ro "{:host \"192blah\"}" -r
but the clj just compiles and doesn't run the repl. I have tried using clj --main cljs.main -c sam.core -r -ro "{:host \"192blah\"}"
which does run the repl, but the host is still the same.
How can I make the browser call the repl back on the 192 address and not localhost?
Trying to silence Refused to get unsafe header
when checking if header exists on the response.
(defn check-if-header-exists [xhr header-name]
(some? (try (.getResponseHeader xhr header-name) (catch Exception e nil))))
But this won't cut it, someone has idea how to silence this error, or use different way of checking if response header exists?What is the best way to package a clojurescript library so that it can be used inside a javascript project?
@viebel but mori.js had to solve this, not sure if it is the best, but maybe you can get some inspiration there[1], maybe mori-export
has some clues
[1] https://github.com/swannodette/mori
in general you will have to bake-in all parts of cljs.core which are being used by your library code
what do you mean by “bake-in” @darwin?
assuming javascript projects consuming your library do not use google closure compiler and have no awareness of cljs
I was thinking of simply defining the public functions of my library with ^:export
yes, that should work, and you compile with :advanced, so gcc will do that baking for you
mori even uses modules to split the code, as you can see here, not sure if :output-wrapper false
is needed https://github.com/swannodette/mori/blob/master/project.clj#L24-L38
@djwelch666 you could try figwheel-main
Yeah will check that out, looks good
I've just got around to trying it out now. Unfortunately work got in the way. Worked perfectly thanks very much, seems very polished. Only issue was I had to set :open-url false
since no x windows, but the config didn't validate, so I set :validate-config false
and everything was fine :thumbsup:.
@darwin do you think it will also work if I include 2 cljs libraries from javascript? I am afraid that there might be conflicts between the variable names generated by google closure compiler
by extra careful I mean compiling both cljs libraries as modules of a single gcc build (advanced mode)
Yeah. This is what I thought
Are there any community standards as to how to name functions (for example should functions that mutate atoms end with !, should js constructs be named differently than clojure ones, etc?)
@theeternalpulse when using core.async
, I try to prefix all block-returning functions to with go-
prefix, and I don’t write functions which would return mixed results, e.g. sometimes blocks and sometimes other values
@lilactown I don't think so
@theeternalpulse theres this https://stuartsierra.com/2016/01/09/how-to-name-clojure-functions
thanks
I read the naming chapters but it's not contextual so far, it's more of a high level "what you should think about when naming"
Too much sci-fi getting in the way of my programming books
@lilactown see the discussion a few hours ago here
I think this should be a core function
People reach for the recursive js->clj when they actually want a shallow conversion
(js-obj-seq #js{"a" "b" "c" "d"}
which returns the same as (seq {"a" "b" "c" "d"})
would be useful
@lilactown well there (js-obj "k1" "v1" "k2" "v2")
What is benefit of https://clojure.org/guides/spec over https://github.com/plumatic/schema ?
@rnagpal super difficult to answer quickly. spec
is a core part of the clojure eco system. so using it has many of the advantages of using standard libraries. It is also more general that plumbatic schema
and you can express more with it.
my other two cents: schema is considerably less complicated and in some sense is more complete than spec. you can add checking to a function, checked closed maps, and get readable error messages without any add-ons. the method for specifying the shape of collections pretty much looks like the shape of the data, whereas with spec it is much more verbose. i think all of these things can be fixed with add-on libraries in spec, but the ramp up time will be greater. (still, spec is the “standard” so it is probably worth the effort)
by “more complete” i mean in terms of tooling, not in terms of the type checking you can do. spec is more powerful in that regard
I haven't used https://github.com/metosin/spec-tools
nice. that library has a schema-like specification format. if i could get that + spell-spec
+ expound
and figure out how to instrument side-effecting functions i think i could switch to spec
@lee.justin.m can you talk a little more about “figure out how to instrument side-effecting functions”? I’m not sure what you mean
the last time i tried to instrument a side effecting function it called the function like 20 times, which, of course, is bad. i believe i was told in the spec channel that that is expected behavior
Hm, that is surprising. I thought check
would call the function but not instrument
itself, but I may be incorrect
@bbrinck I’m trying to find that code attempt in git but based on memory I was using fdef
and then I seem to recall there is some kind of instrumenting variable you set to get automatic checks on the arguments
@lee.justin.m There very well may be cases where this occurs, but in my simple experiments, it doesn’t occur (but I may be doing something different than you intend) - https://gist.github.com/bhb/e8328ce1c15c3fc6f5f4ea71cabfa076
certainly if you do something like exercise-fn
, that would run the code https://clojuredocs.org/clojure.spec.alpha/exercise-fn
fspecs are the only case where that happens
the function is tested by running its arg generator and invoking the function
I would think in most cases, you could spec a side-effecting function using fdef
with no issues then, you wouldn’t need to use fspec
in that case
it is specifically a case in specing functions that take a function argument spec’ed as an fspec
one workaround is to use ifn?
instead
@lee.justin.m I haven’t used it myself, but if you like the way that schema adds type data to a function, orchestra has something similar: https://github.com/jeaye/orchestra#defn-spec
and it’s also possible to swap this in as an alternate spec while doing check
I think in hind sight that the fspec behavior has been more problematic than useful
I’d be in favor of changing it and I think the last time I talked to Rich about it was in the early days and we’ve learned a lot since then
@bbrinck thanks yea I knew I’d seen something like that. I have a bit of analysis paralysis with the add-on libraries right now with spec but one day I’ll dive in and figure out a solution that serves my needs (e.g. consider how and whether orchestra
and spec-tools
work together). I didn’t realize that the fspec
issue isn’t that big of a deal when I ran into it. I think I was just frustrated at all of the complexity and I just wanted something that would tell me if I typoed the key of a map or dropped some brackets and would also be a bit more self-documenting. schema really fits the bill there at the moment
@alexmiller I can see why the current behavior is unexpected (and requires test.check to be loaded IIRC), so I can see the case for changing it. Out of curiosity, what might the new behavior look like for fspec?
I think the best suggestion I’ve heard is to also wrap and instrument the fn arg
so that when it’s invoked you could verify the args of fn arg (and maybe ret)
it’s more deferred but less surprising
seems like that would be preferable to now, which is that people don’t like the current behavior and stop using fspec :)
@lee.justin.m Maybe you can help @robert-stuttaford build this wrapper 🙂 https://twitter.com/RobStuttaford/status/1001726269040529408
@alexmiller Seems sensible to me, much more consistent with how fdef
instrumentation works. And +1 for checking return values both for fspec
and fdef
🙂
yes! or maybe I can send him a cake and a thank you card if he does it all by himself. 🙂
that’s a separate issue
Am I missing something?
clj -m cljs.main -co '{:npm-deps {"lodash" "4.17.4"} :install-deps true}' -re node
This should start a node cljs repl after installing some npm modules, right?
It just exits immediately without doing anything.
@dnolen Hmm, ok. Some reason I didn't think it was working with that either earlier. I get a repl now, but npm modules aren't installed.
Ok, I thought -ro
was for the repl-env
arguments, which didn't seem very useful.
Still no dice with -ro
though.
clj -m cljs.main -r -ro '{:npm-deps {"lodash" "4.17.4"} :install-deps true}' -re node
I guess -co
is only used when actually compiling. Oh well; I was just trying to understand why.
Does the order of arguments matter at all?
It doesn't seem to do anything with -d
when running a repl.
clj -m cljs.main -co '{:npm-deps {"lodash" "4.17.4"} :install-deps true}' -d out -re node -r
works for me
Strange
clj -m cljs.main -r -co '{:npm-deps {"lodash" "4.17.4"} :install-deps true}' -re node -d tree
doesn't work, but clj -m cljs.main -co '{:npm-deps {"lodash" "4.17.4"} :install-deps true}' -d tree -re node -r
does.
-r
after -co
fixes it.
Oh, ok. I didn't know that.
-h
-> “The init options may be repeated and mixed freely, but must appear before any main option.”
That makes sense. 🙂 Thanks I'll RTFM next time. 😉
is there a function that takes a thing, compares it via some comparator and if that thing passes the check transforms it. otherwise, it returns the original thing. if this doesn't exist, what would be a good name for it? [want to write it myself but can't think of a good name]
What were you saying about -d
? It seems to work without it. (It'll install npm deps, and I can require them.)
Nah, no temp dirs were created.
@johanatanSounds like a monoid in the category of endofunctor to me. 😛
:man-shrugging:
» clj -m cljs.main -co '{:npm-deps {"lodash" "4.17.4"} :install-deps true}' -re node -r ~/tmp/tree
ClojureScript 1.10.238
cljs.user=>
» ls ~/tmp/tree
deps.edn node_modules package-lock.json package.json
» ls -A ~/tmp/tree
.cpcache deps.edn node_modules package-lock.json package.json
» ~/tmp/tree
I did, and the output with and without -d
is the same.
One sec
@ghopper yep, i was thinking the same thing. sounds like functor.fmap from cats could do it. but this "thing" in my case is a string and wrapping it in some functor seems like overkill
Yeah, it probably is. Is there a reason you want to have a generalized function with a fancy name to do it?
just smaller, more declarative than an if/else. i.e., reads more like english: when something-is-the-case transform-something
which is 3 tokens vs the 4 it'd be w/ the else clause
@dnolen Alright, nevermind. It is copying to /tmp
. I thought you were saying it would create a temp directory in the current directory. I see what's going on now.
basically i want a when
that rather than returning nil
for the else, returns the original thing
@johanatan Like this?
(defmacro transform [x t f]
`(if (~t ~x)
(~f ~x)
~x))
(transform x #(= 2 %) inc)
=>
(if (#(= 2 %) x)
(inc x)
x)
With a separate test and transformation function
Yeah, unless you're doing this kind of thing a lot of places or trying to string them together or something, I'd stick with the simple and redundant.
fair enough. partial application might make sense w/ your transform (but then its macro-ness screws us)
You could save 3 characters, if you can find a two character name. 😉
(<> x #(= 2 %) inc)
(if (= 2 x) (inc x) x)
Yeah, there's no reason for it to be a macro.
(defn <> [x t f]
(if (t x)
(f x)
x))
Definitely. Does clojure support unicode symbol function names yet?
I want to find out what APL's symbol for this is now...
https://github.com/bhauman/lein-figwheel/tree/master/figwheel-main#working-with-nodejs
What do you use for navigation with re-frame
/ reagent
/ SPA ? Personally I like bidi
but with what do you use it?
To be precise I am talking about url#/path/to/something
or any other solution to let user copy URL to current location. The part which I have to choice is about URL change event and set new URL from the app.
for what it is worth, i use secretary + accountant and that works. i’m sure you can do it with bidi too
(defn hook-browser-navigation! []
(doto (History.)
(events/listen
EventType/NAVIGATE
(fn [event] (secretary/dispatch! (.-token event))))
(.setEnabled true)))
(defn app-routes []
(secretary/set-config! :prefix "#")
;; --------------------
;; define routes here
(defroute "/" [] ...))
ok, you convince me to check it deeper, I see accountant has update 3 months ago, not 3 years ago. My mistake.
hi all! I want to get started working on my first ClojureScript/React/Reagent app by implementing the game Hanabi. Does anyone have any suggestions for books/sites tutorials I can use to get started with React/reagent? I've played around a little with clojruescript but would be completely new to webapps/react
thank you!