This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-01-10
Channels
- # admin-announcements (7)
- # announcements (3)
- # avi (1)
- # beginners (222)
- # boot (184)
- # braid-chat (75)
- # business (7)
- # cljs-dev (3)
- # cljsrn (9)
- # clojars (17)
- # clojure (131)
- # clojure-dev (1)
- # clojure-portugal (4)
- # clojure-russia (39)
- # clojure-sg (1)
- # clojurescript (140)
- # community-development (563)
- # datomic (5)
- # editors (1)
- # emacs (3)
- # events (3)
- # leiningen (4)
- # off-topic (31)
- # om (84)
- # omnext (1)
- # slack-help (3)
- # yada (5)
The first example of (do…)
at clojuredocs states:
> do is used to evaluate multiple expressions in order, usually for the purpose of evaluating exprs that have side-effects (such as printing or I/O). do returns the value of its last expression.
https://clojuredocs.org/clojure.core/do#example-542692cfc026201cdc326e9f
Why group the expressions with do
? Why not just list the expressions. E.g. what is the advantage of doing this:
=> (do
(println "LOG: Computing...")
(+ 1 1))
…over just doing this:
=> (println "LOG: Computing...")
(+ 1 1)
@plaxdan, those are equivalent—they both return 2
—but you can’t do the second in a let
binding, for example.
do
is good for whenever you want to bind up multiple sexps, with all but the last returning an actionable value, in situations where multiple sexps can’t be used.
Thanks for your reply. I’m still missing something. Perhaps the (possible) wrongness of my example above will point out what I’m missing? Also what’s a “sexp” …side-effect-expression?
The example above would not be possible without the do
. I suppose I’ll come across a real-world usage at some stage.
It is a function expressed as a sexp which expects 1 or two arguments each of which is also expressed as a sexp.
- If is a function expressed as a sexp (since all of clojure is expressed as sexps) - the first argument is a sexp which is evaluated for its truthiness - when the first arg is truthy the second argument is evaluated and returned from the function - when falsey the (optional) third arg is evaluated and returned from the function
(doc if)
-------------------------
if
(if test then else?)
Special Form
Evaluates test. If not the singular values nil or false,
evaluates and yields then, otherwise, evaluates and yields else. If
else is not supplied it defaults to nil.
Please see
=> nil
I missed the part about returning nil
should then
evaluate as falsey and no else
being supplied.
Haha no that’s fine, I really appreciate you being willing to even discuss it like this. Thankful the #C053AK3F9 channel exists! It seems like you’re reminding me to conceptualize a Clojure program as simply the tree-shaped data literal that it is.
back to if
- what do you do if you want more than one thing to take place within the truthy branch
this reactji performance art was brought to you by https://twitter.com/DalaiLama
What about the concept of if being a function? Can you pass it to a function as a value?
Or, even simpler, what if you simply type if
at a REPL to see if it can be evaluated, like, say, other functions like map
or filter
?
@mfikes: is that why the source code link for if
on clojuredocs is fubar? https://clojuredocs.org/clojure.core/if
In ClojureScript there are 20 such special things. (Probably a similar number in Clojure.)
And is that also why I can find a test for it but not the definition? https://github.com/clojure/clojure/blob/bc186508ab98514780efbbddb002bf6fd2938aee/test/clojure/test_clojure/logic.clj#L21
The 20 in ClojureScript comprise { var
, if
, case*
, throw
, try
, def
, fn*
, letfn*
, do
, let*
, loop*
, recur
, quote
, new
, set!
, ns
, deftype*
, defrecord*
, .
, js*
}
OK you are meaning keyword to be the ones in that set where I was considering those that may be created by the programmer as well. So now the question: what’s special about those in the set above..?
Well, you have things that are types of values, 1
is a number, :foo
is a keyword, ”abd"
is a string.
@meow: I do. My response earlier demonstrated that I often conflate keywords and symbols.
The special forms are not values. David Sletten pointed out that some people like to pendantically refer to them as special operators
In other words, (if true 1 2)
can be thought of as a form, but if
is in operator position.
@meow: Special forms are definitely in the analyzer. (Not sure about the reader—that’s a good question.)
It seems that the keyword is used to mark a subtree of the sexp tree for special keyword-dependent contextual interpretation. A delimiter.
@meow: What is profound, is that the analyzer essentially only works in terms of special forms, in a sense, and the compiler emits things that the analyzer puts in the AST. Everything else is a glorious abstraction on top of that, mostly sitting in cljs.core
, etc.
plaxdan: none of those words resonates with me - they might be correct - let me ask it differently
@meow: where an unpredictable number of prior sexps in a list exists? If the number of prior sexps in a list was interpreted in a fixed way then you wouldn’t need a keyword?
Example: if
expects 2 or three so no keyword would be necessary since they can be interpreted by their position
Example 2: cond
may expect any number of sexps and therefore a keyword must be used as a marker.
They are not strings or anything else other than themselves so I suppose they must be a type.
At least in ClojureScript: deftype*
is a special form.. and Keyword
is a type defined in cljs.core
. What’s cool is you can see how it works: It is written in Clojure/Script.
Here: https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L2930-L2956
Yep same thing except based on what @mfikes said earlier this is a demonstration of the Keyword
type satisfying the IFn
protocol and being called with the map as an arg.
Yep. Right here: https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/core.cljs#L2941-L2943
Hmm…I was about to say that it has types that satisfy the IFn
protocol and that we refer to this subset of types as functions….
I see no function in { var
, if
, case*
, throw
, try
, def
, fn*
, letfn*
, do
, let*
, loop*
, recur
, quote
, new
, set!
, ns
, deftype*
, defrecord*
, .
, js*
}
…but that implies that anything that can satisfy any protocol could be referred to a function.
Unless of course all protocols are derived from IFn
in which case the former statement would be true.
The former statement being: > anything that can satisfy any protocol could be referred to a function.
So I am learning that Clojure is a teasing apart of concepts that are bundled together in other languages. Types and protocols are examples of this.
You cannot separate them. I do not know for sure yet if it is the case that they are fully separate in Clojure but I suspect that might be the case.
so if you wanted to create a github org that captured the essence of what you just described you would name it?
Hmm…I’m not sure I understand what you’re getting at but…a name that implies a set of atomic elements. Perhaps I’d name it alchemist or something.
I will look at that. Going back to your earlier question: “What is a protocol?” It is a contract for behavior.
One interesting thing I learned recently, you can have things that satisfy nil?
that are not identical to nil
. (At least in ClojureScript.)
(I think this is perhaps the JavaScript host leaking through, where you have null
and undefined
which ClojureScript puts into the same equivalence class, I suppose.)
clojure:
(identical? nil x)
=> false
(nil? x)
=> false
(identical? nil nil)
=> true
(nil? nil)
=> true
@meow: I had asked Nolen about the diff between ClojureScript and Clojure regarding (def x)
. Ideally they’d be the same, but ClojureScript doesn’t go so far as to reify the concept of someting being “unbound”, and there hasn’t been enough payoff to do anything about it.
I enjoyed it. These concepts are not at all crystal clear in my mind… worth ruminating.
explaining to other people always shows me that i don't really know something that i thought i did
i'm learning that clojure/script is not really much like common lisp, except superficially
I think I’ve read more ClojureScript than Clojure now, by a factor of 5 to 1 at least.
Hmm… I guess you could use quoted symbols in Clojure if there were no such things as keywords
too bad all of this will be lost: https://github.com/clojurians/clojurians-chat/wiki/Slackpocalypse
how do you set up nrepl to interact with already running programs like a web server or something, so you can rewrite functions and insert println
so that you don't have to restart the server? This is for development, nothing fancy like for production or something
@tmtwd: Stuart Sierra's reloaded workflow is a common starting point for working at the repl in a Clojure project: http://thinkrelevance.com/blog/2013/06/04/clojure-workflow-reloaded
no need to set up nrepl explicitly, just running lein repl will give you a repl for your app.
suitable for development
to be able to connect to a running app that you don't have the freedom to stop and restart at will, the instructions that come with nrepl: https://github.com/clojure/tools.nrepl having your app do (use '[clojure.tools.nrepl.server :only (start-server stop-server)]) (defonce server (start-server :port 7888)) will cause it to open a socket on the specified port
to which you can connect using lein repl :connect 7888 or with an app called reply: https://github.com/trptcolin/reply