This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-12-02
Channels
- # adventofcode (20)
- # bangalore-clj (14)
- # beginners (72)
- # cider (2)
- # clara (2)
- # cljs-dev (8)
- # clojure (36)
- # clojure-brasil (201)
- # clojure-greece (29)
- # clojure-nl (1)
- # clojure-poland (1)
- # clojure-russia (2)
- # clojure-spec (5)
- # clojure-uk (4)
- # clojurescript (41)
- # cursive (1)
- # datomic (1)
- # emacs (6)
- # fulcro (80)
- # graphql (1)
- # klipse (2)
- # leiningen (5)
- # lumo (15)
- # off-topic (1)
- # om (3)
- # om-next (3)
- # re-frame (19)
- # reagent (7)
- # test-check (1)
- # uncomplicate (2)
- # yada (8)
that's not dereference, that's unquote-splicing
~@ and @ are unrelated
@wander4096 you might find this helpful https://clojure.org/guides/weird_characters
it is a reader macro - we don't use the term "atom" for that
but yes, it 's a single reader macro with two characters
hmm - maybe ~@
should come before @
in that weird characters guide, so that if you are reading top to bottom you don't get misled...
@wander4096 YOu can use read-string
to see what the reader does with such forms ^
thanks, though it displays the result with which I still know little about the underneath
Does anyone recall why does clojure.test
's is
kind of fail to compare to false
in cases like here?
user=> (is (= 1 2) false)
FAIL in () (form-init78938769099331712.clj:1)
expected: 1
actual: 2
diff: - 1
+ 2
false
@derpocious >lein test | grep Failed thanks!
I wonder what nil-punning means( it’s only nil to me, no pun ), thus I reach http://www.lispcast.com/nil-punning#fn4
it says “first has nothing to return if the seq is empty, and so it returns nil. Because nil is a seq, first works on nil, and returns nil. 6. rest works on nil as well, because nil is a seq.”
right, they are incorrect - nil is sequable but not a seq
Clojure 1.9.0-RC1
+user=> (seqable? nil)
true
+user=> (seq nil)
nil
+user=> (filter even? nil)
()
btw seq is a relatively limited type - many (most?) things we make seq out of are not seqs
+user=> (seq? [])
false
use ` - it's the same as markdown
I think a lot of this will work - probably not all of it https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#code-and-syntax-highlighting
=> (vector? [])
true
aha, is there type graph of clj?( I’m not sure if clj should have a ‘type graph’ )there’s also https://github.com/Chouser/clojure-classes/blob/master/graph-w-legend.svg (little out of date, but still mostly right)
and https://github.com/stuartsierra/class-diagram is pretty handy
hmm... I think I know what you mean - you can look at things via supers
- the way Clojure defines data types doesn't use nested inheritance or concrete inheritance - there's a number of Interfaces and Protocols (100% abstract) and a given data type implements some number of them
so it's very flat, not much of a graph
+user=> (pprint (supers (type [])))
#{clojure.lang.IPersistentCollection clojure.lang.IKVReduce
java.lang.Runnable java.lang.Comparable clojure.lang.IHashEq
clojure.lang.IPersistentVector clojure.lang.IMeta java.util.List
clojure.lang.Seqable clojure.lang.IObj clojure.lang.Indexed
clojure.lang.AFn java.util.Collection clojure.lang.APersistentVector
clojure.lang.ILookup clojure.lang.Sequential clojure.lang.Counted
clojure.lang.IReduce java.util.RandomAccess
clojure.lang.IPersistentStack java.lang.Iterable
clojure.lang.Associative java.io.Serializable
clojure.lang.IEditableCollection clojure.lang.IFn
clojure.lang.Reversible clojure.lang.IReduceInit java.lang.Object
java.util.concurrent.Callable}
nil
+user=> (pprint (supers (type ())))
#{clojure.lang.IPersistentCollection clojure.lang.IHashEq
clojure.lang.IMeta java.util.List clojure.lang.Seqable
clojure.lang.IObj java.util.Collection clojure.lang.Sequential
clojure.lang.Counted clojure.lang.IPersistentList
clojure.lang.IPersistentStack java.lang.Iterable clojure.lang.ISeq
java.io.Serializable java.lang.Object clojure.lang.Obj}
nil
+user=> (pprint (supers (type {})))
#{clojure.lang.IPersistentCollection clojure.lang.IKVReduce
java.lang.Runnable clojure.lang.IHashEq clojure.lang.IMeta
clojure.lang.Seqable clojure.lang.IObj clojure.lang.AFn
clojure.lang.ILookup java.util.Map clojure.lang.IPersistentMap
clojure.lang.Counted clojure.lang.APersistentMap java.lang.Iterable
clojure.lang.Associative java.io.Serializable
clojure.lang.IEditableCollection clojure.lang.IMapIterable
clojure.lang.MapEquivalence clojure.lang.IFn java.lang.Object
java.util.concurrent.Callable}
nil
+user=> (pprint (supers (type #{})))
#{clojure.lang.IPersistentCollection java.lang.Runnable
clojure.lang.IHashEq clojure.lang.IMeta clojure.lang.Seqable
clojure.lang.IObj clojure.lang.AFn java.util.Collection java.util.Set
clojure.lang.Counted java.lang.Iterable java.io.Serializable
clojure.lang.IEditableCollection clojure.lang.IFn
clojure.lang.APersistentSet java.lang.Object
java.util.concurrent.Callable clojure.lang.IPersistentSet}
nil
the significance of each of the interfaces / protocols is that they each define a small number of methods, and the clojure built ins mainly use those methods
@wander4096 you can look at the source to see what those do, but reflection can help too in the repl
+user=> (->> clojure.lang.Seqable (clojure.reflect/reflect) :members (map :name))
(seq)
+user=> (->> clojure.lang.IPersistentCollection (clojure.reflect/reflect) :members (map :name))
(count cons empty equiv)
so that shows the names of the methods each of those interfaces define
eg. clojure.core/conj is a function that calls the static method clojure.lang.Rt/cons which iirc calls the cons methodon IPersistentCollection
aha - it uses the cons method, unless you give it nil, which it special cases to make a list (looping back to thestart of the convo) https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/RT.java#L667
@wander4096 as an aside, things like this are why I get pedantic when people use the terms function and method interchangeably - it makes talking about how this stuff works harder than it needs to be
then again, I think I am well out of "beginners" territory now - oops sorry for going off topic
so on a less detailed / implementation level, and a more abstract introductory level: the way I think of them, clojure's types don't form a graph, they each have a subset of the clojure interfaces they implement (maybe it would be useful to make a graph with edges for each shared interface between the types though? not sure)
I think this is a good way to think about them: interfaces are sharded traits where concrete classes implement the necessary traits. A fun thing to contemplate is whether the ideal number of methods per trait should be 1.
for graphs, see https://github.com/Chouser/clojure-classes/blob/master/graph-w-legend.svg and https://github.com/stuartsierra/class-diagram
and I have one in ch 2 of Clojure Applied that is tailored specifically to the collection apis and their mapping to the polymorphic collection functions in core
Hey folks,
how to create anonymous function which returns it's parameter unchanged?
(#(%) "a")
throws ClassCastException java.lang.String cannot be cast to clojure.lang.IFn
unfortunately.
@ghsgd2 the function identity
does this, but we can look at why that happens too
+user=> '#(%)
(fn* [p1__231#] (p1__231#))
#() expands to an anonymous function (I used ' to quote it above so that we would see the reader expansion instead of just the function that is generated)
if we rename the weird variable, we get (fn [x] (x))
if it didn't expand that way, #(println %) would break - maybe it would need to be
#((println %))` which is weird
+user=> '#(println %)
(fn* [p1__234#] (println p1__234#))
('#(%) 1)
ClassCastException clojure.lang.PersistentList cannot be cast to clojure.lang.IFn
looks like the only way is to use (fn [x] (x))
or identity
.
Thank you @noisesmith!@ghsgd2 to be clear, what ' does, is it prevents evaluation
so it returns a list, instead of compiling that list
so '(fn [x] (f x))
isn't a function, it's a list that would make a function if we evaluated it
>so it returns a list, instead of compiling that list
I know, it's just not clear where to put quote to avoid treating x
or %
as a function instead of return value.
oh,
((fn [x] (x)) 1)
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn
now it works! 🙂
> ((fn [x] x) 1)
1
@noisesmith thanks a lot.
@ghsgd2 right, () indicates you want to call something, except when unevaluated it gives you a list, but to use a value instead of calling or making a list containing its name you can use the item without the parens
And #() inserts parens so that things like #(f %) work as expected
how can I make an http call in ring and return the result to the client? when I do this
(defn get-orders [req]
(let [{:keys [sig uri]} (api-signature :get-orders)]
(client/get uri
{:async? true}
(fn [response] {:body "something"})
(fn [exception] {:body "error"}))))
it gives me this error
No implementation of method: :render of protocol: #'compojure.response/Renderable found for class: org.apache.http.impl.nio.client.FutureWrapper
@bravilogy I answered that in #clojure -- feel free to follow-up in either channel (but not both).
Hi @linux.soares! I live in the San Francisco Bay Area and we have a couple of Clojure meetups every month, and many companies here are using Clojure in production...
Meetups? https://www.meetup.com/The-Bay-Area-Clojure-User-Group/ Has both Palo Alto and SF meetings.
...my company has had Clojure in production since 2011 and we have about 75,000 lines of Clojure code now (about 20,000 is tests).
Where are you based? Six hours time difference... so Brazil?
Thanks @seancorfield. I live in Brazil, São Paulo. In my company we are using Python in 90% of systems. There is a meetup here, but only from Scala, Python that I know… I’m thinking of starting a Clojure meetup around here. I wanted to know how the adoption is outside Brazil.