This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-11
Channels
- # aws (6)
- # beginners (105)
- # boot (6)
- # cider (50)
- # cljsrn (10)
- # clojure (41)
- # clojure-brasil (6)
- # clojure-italy (25)
- # clojure-nl (17)
- # clojure-russia (4)
- # clojure-serbia (1)
- # clojure-spec (8)
- # clojure-uk (242)
- # clojurescript (27)
- # core-async (10)
- # cursive (5)
- # data-science (9)
- # datomic (43)
- # emacs (6)
- # fulcro (6)
- # graphql (1)
- # javascript (3)
- # juxt (4)
- # lein-figwheel (1)
- # mount (1)
- # onyx (19)
- # parinfer (2)
- # portkey (15)
- # protorepl (1)
- # re-frame (30)
- # reagent (3)
- # ring-swagger (1)
- # shadow-cljs (22)
- # sql (6)
- # tools-deps (23)
- # vim (13)
The Clojure Applied book has a lot of material on using core.async
hi people, just a newbie question about Clojurescript : I'm not a front-end developer (I mean not at all) but somehow I would like to give clojurescript a shot to get in. Do I have to be good in JS already or is it okay to just dive in CLJS right away ?
@kaffein depends a bit on what you want to do, a bit ok knowledge about JS in general can come in handy, but is not really needed. I’m no front-ender myself, but using re-frame combined a css-framework like bootstrap or bulma makes it easy to do front-end without knowing much about either JS or css.
i assume there is a brick wall you may hit at some point, if you don't familiarize yourself with the host platform
@kaffein that’s fine then, neither am I. Sometimes it come sin handy to know your way around the chrome debug tools, of which a large part is unknown to me.
Well I will probably have to start slowly then but I'd have to be ready for a little more on the JS side in the future
Hey guys, is there an easy way to, given a map with namespaced keys and given a specific namespace, create a view of this map with all keys related to supplied namespace? Like so:
(filter-namespace {:project/name "Proj Name" :project/id 1 :person/name "Joao Gomes" :person/id 1} :person)
{:person/id 1 :person/name "Joao Gomes")
(defn select-ns [n-s m]
(let [target (name n-s)]
(into {}
(filter #(= target (namespace (first %))))
m)))
(select-ns :a {:a/b 3 :a/c 4 :d 7})
#:a{:b 3, :c 4}
hi everyone, what’s the best book/way to learn Clojure if you would like to do scientific computing with it? Also, has there been a valid benchmark of Clojure against other languages? thank you in advance.
are you looking to learn clojure from the start or do you know clojure and want to apply it to scientific computing?
I am looking to start clojure from the start, have very limited knowledge of it now. But I do want to apply it to scientific computing because of all good things I have read about it.
No problem, I have your same issues and I'm trying to spur a discussion around the topic
yeah, there seems to be a lot of potential with this language but there is not an easy way to get mastery in it. Which is fine, just a not a lot out there like python, c++, etc!
Yeah, on #data-science we were talking about the tooling, and there isn't much available
my two favorite books are clojure for the brave and true and programming clojure third edition. the former is available for free online and available for purchase. the latter is by alex miller and on prag prog
I would learn the language and how it works and then look into what you want. Immutability and functional paradigm can take a bit of adjustment to internalize how the language likes to work. And then work on whatever you want :)
I would also take a look at the http://4clojure.com site. You can learn by doing exercises and also by seeing the other developers solutions.
What mutable data should we use in a stateful transducer if we feel that we do not necessary need a volatile data? (that volatile: https://github.com/clojure/clojure/blob/clojure-1.9.0/src/jvm/clojure/lang/Volatile.java#L15)
@vincent.cantin I believe volatile is there mainly to propagate changes to multiple threads in a core.async pipeline context (and tangentially perf). If you plan to never use it in there, you could go atoms. (Perhaps @alexmiller could confirm). But what's wrong with the volatile though?
I thought that atoms were doing more than volatiles.
Apparently they are doing more stuffs (https://github.com/clojure/clojure/blob/clojure-1.9.0/src/jvm/clojure/lang/Atom.java#L18)
Is there a variant of volatile without the Java "volatile" attribute that can be used from Clojure? Something like a pure wrapper of an Object with just its init and reset functions?
Let's suppose that I design some kind of stateful transducers which I know will only be used by the same thread. What (efficient) mutable data type should I use in Clojure?
volatile is one of the cheapest thing in single thread context https://stackoverflow.com/questions/4633866/is-volatile-expensive
@vincent.cantin your options are :
- a plain old unsynchronized mutable java collection
- an array
- a custom deftype
with :unsynchronized-mutable
fields implementing clojure.lang.IFn
- use an external library to get your variables back
@vincent.cantin The advice I'd broadcast in this channel is to pretend that volatile
doesn't exist. That being said, volatile is pretty much atom minus the atomicity guarantee during swap
> a plain old unsynchronized mutable java collection this doesn't use clojure equality semantics, fyi. If you use a map or set you may be surprised.
@reborg @ghadi Thank you for your answer, it seems that volatile should not be a huge concern. However, since I am curious, I would still be very happy to know if there is something faster, even slightly.
You could do a one-off “mutable box” of your own @vincent.cantin
Just if you’re curious, something like:
(defprotocol IMutable
(set-it! [this value])
(get-it [this]))
(deftype MutableBox [^:unsynchronized-mutable holder]
IMutable
(set-it! [this value]
(set! holder value))
(get-it [this]
holder))
I am wondering if (set! holder value)
is fast or not. Do you know if this is the implementation of set!
? https://github.com/clojure/clojure/blob/clojure-1.9.0/src/jvm/clojure/lang/Var.java#L214
No. That’s for a var. set! Is a special form. It’s implemented in the compiler in Java. Pretty sure it should be about the same bytecode as = assignment in Java.
@vincent.cantin I’m on phone GitHub which is annoying. But look at https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java look up the AssignExpr static inner class
Also you can checkout the InstanceFieldExpr looking at evalAssign and emitAssign impl here for an example like I showed you above. emitAssign you’ll see ends up as a putField eventually which relates to that bytecode.
Thank you ! I am going to read more.
@vincent.cantin I’ll add that emitAssign
would be the path taken in cases where you are calling set!
in a pre-compiled context, like a fn body (typical). The evalAssign
, which is slower, should pretty much only happen if doing some runtime eval and/or like doing (set! <instance field here> 10)
in the repl
Thx again.
I am not sur if set!
could be used for local variables, the documentation for it is not clear to me. I will give it a try tomorrow.https://clojure.org/reference/vars#set
Apparently, Var is a no-go compared to Volatile. https://github.com/clojure/clojure/blob/clojure-1.9.0/src/jvm/clojure/lang/Var.java#L214
hi, why do functions in a call like map foo bar
not need to be quoted? (i.e map 'foo bar
)
foo is a symbol, it’s evaluated to find the function instance which is passed to map
you want it to be evaluated, so it’s not quoted
but wouldn't I need to quote it in common lisp? Sorry if I'm confused, new to the lisp world
in common lisp you'd need to actually do (lambda (x) (foo x))
, #'foo
is just a reader macro for doing that
sometimes common lisp is also less restrictive than the standard and just 'foo
works too, but that's often just undefined behaviour
there should be a page in http://clojure.org with differences between clojure and other lisps fyi
Common Lisp has 7 namespaces
1) functions, 2) lexical variables, 3) special variables (distinct from the lexical variable namespace), 4) types, 5) goto labels, 6) block names, 7) quoted expressions containing symbols
You can have the same symbol for 7 entirely different things.
@bronsa Also that's not correct, about being undefined behavior. It is clearly defined what the difference is between 'foo
and #'foo
There is no undefined behavior in that regard
When you call a function, you supply a function designator. The language defines a regular symbol to refer to the function of the same name in the global environment. A function, denoted by either #'func or its expansion of (function func), refers to the function of that name that is searched for starting in the current scope and working outward.
CL-USER> (foo) "inner" "outer"
Sure 🙂 I'm still learning Clojure, but I have been using CL for a decade
thanks, so just so that I'm sure I have understood it, if I call map foo in common lisp, then foo is interpreted as a variable and 'foo refers to the function, whereas clojure lacks that distinction? If I got that right, how does clojure determine the difference?
map
in Common Lisp is something else entirely. Clojure's map
is like Common Lisp's mapcar
.
clojure only has vars for global symbols and local bindings for local ones, vars and locals can hold any type of value, be it an integer or a function, local bindings can shadow vars
and you cannot call (map foo ..)
unless foo
is a symbol whose variable points to a function object. Typically you'd do (mapcar 'foo ...)
or (mapcar #'foo ...)
Example of shadowing in a Lisp-1 like Clojure that you would not find in Common Lisp: (defn f [x] (let [+ -] (+ x x)))
then (f 2)
returns 0, not 4. You are unlikely to write that particular example by accident, but shadowing names of Clojure functions like list
and map
is easier to do accidentally.
The Clojure lint tool Eastwood will warn about such name shadowing.
(in the specific case of using a name in function call position, and that name is a local binding that shadows one in the outer environment, IIRC)
More details on that Eastwood warning here, if you are curious: https://github.com/jonase/eastwood#local-shadows-var