Fork me on GitHub

Hi, is concat on lists in Clojure constant time? And is there a better function then concat if I don't need laziness?


you can use the cat transducer or mapcat or for to concatenate as part of a data transform


you can use into to concatenate some seqable onto the end of a vector


concat can't be constant time as the inputs can be lazy and it creates a new list, so it can't do better than O(n)


(unless I'm thinking about this wrong...)


if you need fast concatenation, check out finger trees


come to think of it, concat returns instantly and does almost 0 work regardless of input size, it's just that it delays some calculations to be done later, once you actually access elements in the collection


NB - don't mix eager code that combines collections with repeated nested calls to cat


Thanks! I just read this article by Stuart. I need to insert some elements in the middle of a list (I'm merging two lists where some nodes may be added, removed or modified). My idea was to split list at the node where I need to insert, use conj and concat and I somehow can't find the good way to solve it.


(let [input [0 1 2 3 4 5]
      position 2
      [pre post] (split-at position input)]
  (concat pre [:inserted] post))

=> (0 1 :inserted 2 3 4 5)


something like that?


yes, but I need to be mindful of performance since lists may be quite big and have children that needs to be merged too. That's why I was looking towards linked lists


lazy-seqs are linked lists, they just aren't mutable


if you mean doubly-linked, that's what finger-trees offer (linked above) iirc


of course you can use a mutable java doubly linked list and dump it into a clojure collection on return


if nobody saw you mutate, you're pure :D


is list structure is linked list too?


concat reuses the structure of all its inputs, I should have been more clear about that


very little copying is done by clojure collection ops, you don't need to copy as much when the data is immutable


Thanks, that's very helpful. I'll give it a try and see how it works. Unless, I'm missing something I should be fine with just linked lists. No need for finger trees.


internally, concat creates a generator that walks the first input, and then starts walking the next input when (if) it hits the end


the "concat bomb" that the blog mentions is a danger when you call (concat (concat (concat ....))) and all the nested wrapping generators blow up the stack


Yeah, I'll have some recursion, but it shouldn't be too deep. I don't think I come anywhere close to limit.


How do you regex on a paren? I tried double escaping it from some googling but that's not matching either.


(ins)user=> (re-seq #"\(" "((((()))))")
("(" "(" "(" "(" "(")


regex literals use different escaping rules than regular clojure strings, and they use bare "()" for grouping


lol, oops. I had a typo in another section of the regex. Yeah, I think it's time to call it a night. Thank you!


that is, "\(" isn't a valid string, but #"\(" is the regex you get when you re-compile "\\("

Mark-James M.01:06:15

How do I create a binding for a vector? I've tried using (def var-name (value1 value 2)) but that doesn't seem to work


Use [ .. ] for a vector.


( .. ) is a list, which will be treated as a function call here (so it will attempt to call value1 passing value2 as its argument)

Mark-James M.01:06:59

I initially tried that but it didn't seem to work either

(def suits
  [1 2 3 4 5 6 7 8 9 10 Jack Queen])


What about that last code example did not seem to work for you?


Oh, Jack and Queen are symbols that will be evaluated, unless they are quoted to prevent their evaluation


It's not going to know what Jack and Queen are.


So you could quote them, like ‘Jack


Or quote the whole vector like ‘[ ... ]


You would want "Jack" or "Queen" or probably :Jack and :Queen I think


Or use keywords like :Jack instead of a symbol, because keywords evaluate to themselves

Mark-James M.03:06:17

Is there a reason why I don't need to specify vector here:

(def rank
  [1 2 3 4 5 6 7 8 9 10 :Jack :Queen :King :Ace])
but I do here:
(def suits (vector [:Diamonds :Clubs :Spades :Hearts]))
If I try
(def suits [:Diamonds :Clubs :Spades :Hearts])
They type is listed as clojure.lang.Var$Unbound


i'm not able to recreate what you're seeing

~/p/clojure ❯❯❯ clj
Clojure 1.10.1
user=> (def suits [:Diamonds :Clubs :Spades :hearts])
user=> suits
[:Diamonds :Clubs :Spades :hearts]

Mark-James M.03:06:15

It seems I needed to reload the REPL for some reason


what is your repl environment like? how are you evaling forms?

Mark-James M.03:06:10

I'm using cursive with nREPL


ok. do you know the keybinding to send top level form to the repl and send form before cursor to repl?

Mark-James M.03:06:00

I just found it, alt-shift-p


yeah use those keybindings to evaluate forms as you go

Mark-James M.03:06:27

Previously I had been reloading the repl each time I modified a function

😲 6

this should be far nicer for you

👍 3

cmd-\ sends your cursor to the repl if you want to type and esc will put your cursor back in the code buffer. also, use (comment ) forms to type in the repl and eval them

Mark-James M.03:06:38

What would be the equivalent to mapping using a for loop? For example, I want to take one vector, and then for every item in that vector, map it to an item from another vector. I've found zipmap which seems to create maps from vectors


can you give me some example inputs and desired output?

Mark-James M.03:06:41

(def rank
  [1 2 3 4 5 6 7 8 9 10 :Jack :Queen :King :Ace])

(def suits
  [:Diamonds :Clubs :Spades :Hearts])
Desired output (map):
1:Diamonds 2:Diamonds...


(into {}
      (for [rank [1 2 3 4 5 6 7 8 9 :jack :queen :king :ace]
            suit [:diamonds :clubs :spades :hearts]]
        [rank suit]))
the for here does a list comprehension over rank and suit and makes a vector or tuple of rank and suit. into {} "pours" this collection of tuples into the map, which interprets them as key/value pairs and does the obvious thing


this only preserves one suit for each rank


Ha. Whoops.

Mark-James M.03:06:46

And I put this statement into a function?


and in cursive, put your cursor on into and for and hit ^-j (control j) to see their doc strings. and you should hopefully have the clojure docs downloaded as well. the documentation story in cursive is very nice

👍 3
Mark-James M.03:06:34

Ah, this is super helpful


depends on what you want. this is making a map of the cartesian product. you could make a function called that which you use to create a map of a deck of cards. or just def the deck of cards. what ever makes the most sense

Mark-James M.03:06:43

Just ran into a bug

Mark-James M.03:06:29

Managed to fix it by enabling EAP updates

parens 3

I have two responses at two different bidi routes:

{:status 200, :headers {"Content-Type" "application/edn", "Access-Control-Allow-Headers" "Content-Type", "Access-Control-Allow-Origin" "", "Access-Control-Request-Method" "GET, OPTIONS", "Access-Control-Allow-Credentials" true}, :body "{:duration-wise-cost 0.5}"}
{:status 200, :headers {"Content-Type" "application/edn", "Access-Control-Allow-Headers" "Content-Type", "Access-Control-Allow-Origin" "", "Access-Control-Request-Method" "GET, OPTIONS", "Access-Control-Allow-Credentials" true}, :body "{:paid true}"}
In my browser the first one doesn't give an error but the second one gives a 406 not acceptable. Why would that be?


Seems like the error is happening in the client side and is related to re-frame:

GET ?..... 406 (Not Acceptable) @ xhrio.js:631
eval @ xhrio.cljs:32
ajax$protocols$_js_ajax_request @ protocols.cljc:6
ajax$simple$raw_ajax_request @ simple.cljc:64
ajax$simple$ajax_request @ simple.cljc:67
day8$re_frame$http_fx$http_effect @ http_fx.cljs:88
re_frame$fx$do_fx_after @ fx.cljc:76
re_frame$interceptor$invoke_interceptor_fn @ interceptor.cljc:71
re_frame$interceptor$invoke_interceptors @ interceptor.cljc:109
re_frame$interceptor$execute @ interceptor.cljc:204
re_frame$events$handle @ events.cljc:65
eval @ router.cljc:179
eval @ router.cljc:198
eval @ router.cljc:146
eval @ router.cljc:169
G__67540 @ router.cljc:187
channel.port1.onmessage @ nexttick.js:218


A 406 would suggest the endpoint is not happy with Accept or similar headers value

Michael J Dorian11:06:33

Hello! Can anyone provide information on how to store data in a browser session from http-kit routes (routing with compojure)? I have a working api going and I'd like to add some really minimal authentication. Thanks!

Michael J Dorian11:06:05

The only reference to sessions I can find in the docs is an issue where they aren't working quite the way someone expects:

Michael J Dorian11:06:58

Oh nice, it probably will let me try it 😁


I remembered it being a Ring thing and did some quick googling for "ring session middleware"

Michael J Dorian11:06:01

Yeah I figured if it wasn't documented with http-hit it must somehow be obvious, and I guess the obviousness vector was that I should look for the ring docs!

Daniel Tan12:06:13

http kit websockets are a different story though

Daniel Tan12:06:26

not documented well enough, but the source code is documented

Lucy Wang13:06:34

is there any out of the box function/lib that could print a map in column wise manner, i.e. format {:k1 1 :k2 2} to something like this?

| :k1 | 1 |
| :k2 | 2 |

Lucy Wang13:06:09

would be very handy to look at the content at one glance


pprint/print-table gives rows to each key in a vector of maps

(ins)user=> (clojure.pprint/print-table [{:a 0 :b 1} {:a 12 :b 42}])

| :a | :b |
|  0 |  1 |
| 12 | 42 |

👍 3

but what you want can easily be done as a one-liner with doseq

Lucy Wang13:06:32

thx, I knew that, but want something for a single map object ...

Lucy Wang13:06:08

and what's more it's headers on the top line, which is different from what I want

Lucy Wang13:06:36

e.g. when the screen is small it'll wrap around, which is not good


oh you want right-alignment, that makes it harder


short answer: nothing built in

Alex Miller (Clojure team)13:06:24

I always assume you can do anything with cl-format


that's true, but cl-format is a turing complete programming language

Lucy Wang13:06:12

user=> (t/table (meta #'doc))
| key       | value                                                         |
| :macro    | true                                                          |
| :ns       | clojure.repl                                                  |
| :name     | doc                                                           |
| :arglists | ([name])                                                      |
| :added    | 1.0                                                           |
| :doc      | Prints documentation for a var or special form given its name |
| :line     | 120                                                           |
| :file     | clojure/repl.clj                                              |


user=> (clojure.pprint/print-table (map (fn [[k v]] {'key k 'value v}) (meta #'doc))))

|       key |                                                                                              value |
|    :added |                                                                                                1.0 |
|       :ns |                                                                                       clojure.repl |
|     :name |                                                                                                doc |
|     :file |                                                                                   clojure/repl.clj |
|   :column |                                                                                                  1 |
|     :line |                                                                                                132 |
|    :macro |                                                                                               true |
| :arglists |                                                                                           ([name]) |
|      :doc | Prints documentation for a var or special form given its name,
   or for a spec if given a keyword |


that lib is smarter for sure (see what happened with the newline in the string)

Lucy Wang13:06:23

yeah, but the lib doesn't support cljs


you could pre-process the v in the lambda to make it better behaved (give a max length, trim newlines...)

Lucy Wang13:06:49

cl-format is new to me, I'll take a look, thx guys

Lucy Wang13:06:50

(println (cl-format nil "~{~<~%~1,20:;~A~> ~}" word-wrap)) ... that's really mind-blowing ..

Lucy Wang13:06:15

I guess I will not touch it 😂

Mark-James M.13:06:15

Is there a reason why this is only generating a map from the :spades keyword in suit, and not for all the suits?

(def card-deck
  (into {}
        (for [rank [:Ace 1 2 3 4 5 6 7 8 9 10 :Jack :Queen :King]
             suit [:diamonds :clubs :spades :spades]]
          [rank suit])))


i was too casual when helping you. a map can only hold one value for any particular key. so when rank is used as a key, there can only be one suit available. :jack can be in the map once. and therefore only one suit can be stored in this way


@noisesmith pointed this out in a thread


right, you don't want a map at all, probably a vector or set


i'm not sure what representation you want but perhaps a set would work. in which case you can turn the {} into #{}


or just the raw output of for


also I think one of your :spades should be :hearts


wow i get no gold stars on that example 🙂

⭐ 3
Mark-James M.14:06:20

At least you tried, thats what counts the most :D

Mark-James M.13:06:03

Ah, I hadn't seen his reply

Milan Munzar15:06:44

Hey, 🙂 Do you guys know why this import fails core is not defined ? I am not sure what I am doing wrong. Thank you!


if you require :as core, then is wrong, you need core/foo


also, as someone doing maint. work on a project with multiple core.clj files, there's never a reason to name anything core

👍 6
Milan Munzar15:06:10

Thank you 🙂 That did it 😄

Jim Strieter18:06:08

I've been thinking of (defn someFunc [...] (reify (SomeProtocol ...))) as implementing a protocol. But is that the right way to think about reify?

Alex Miller (Clojure team)18:06:07

no, and it's not really good to do that

Alex Miller (Clojure team)18:06:35

SomeProtocol here is an interface generated as a side effect of the implementation of protocols

Alex Miller (Clojure team)18:06:30

relying on tapping into that is kind of going through the window instead of the door a bit (and not portable to cljs afaik)

Alex Miller (Clojure team)18:06:36

depending on exactly what you're doing, extending protocols via metadata (assuming you control it and can allow that) is probably better for this usage

Alex Miller (Clojure team)18:06:50

or actually making a defrecord/deftype that extends it

Jim Strieter20:06:06

Can you help me understand reify? Is thee any simple or plain language way to think about it?


I am not sure if there is some other context to the question I am missing, but SomeProtocol may refer to the protocol or may refer to the interface generated by defining the protocol, and it is tricky for beginners to tell which it is referring to in a given context. reify explicitly supports both protocols and interfaces


the verbs for protocols and interfaces are different, you can implement an interface, and you can satisfy a protocol


and you can satisfy a protocol by implementing the interface defining the protocol generated


which is how reify's protocol support works, given a name that refers to a protocol instead of an interface, it looks up the interface behind the protocol

Jim Strieter12:06:30

What does reify do?


Reify makes something abstract concrete. It makes an anonymous object which can implement some interfaces and satisfy some protocols


but it is always preferable to use the protocol over the interface directly.


as a beginner that is tricky to do because the same name can refer to either, so a rule of thumb is never use import for a name created by defprotocol, and never use a name with a "/" in it somewhere when referring to the protocol outside of the namespace where it was defined


(because import will import the java interface, and using '/' in a name ensures it goes through namespace resolution which will get you the protocol instance instead of java class resolution which will get you the interface)


the ambiguity of the name referring to the protocol or the interface can arise with using defrecord or deftype as well


What about the example above indicated it was using the generated interface rather than the protocol?


reify can't do protocols per se can it?


it definitely can


oh I thought it just did the interface


the doc string says so and the code explicitly handles them


reify, defrecord, and deftype all handle "inline" protocol satisfaction in the same way


when given a protocol, they lookup the interface behind the protocol, and implement that interface, which in turns causes them to satisfy the protocol


nothing about the above example indicated confusion of protocols and intefaces(but nothing in the example rules it out either), but @alexmiller's response doesn't make sense to me unless you assume the example shows that confusion


which is why I started with "I am not sure if there is some other context to the question I am missing"

Alex Miller (Clojure team)19:06:06

sorry, I've clearly lost my mind :) I recant!

Alex Miller (Clojure team)19:06:30

I read it as the interface


Hey guys, noob question incoming. My editing environment is the spacemacs clojure layer (Cider, etc), and am trying to write a basic REST API using http-kit/ring/compojure. When I try to send my code to the CIDER repl, it keeps coming back with the error Could not locate org/httpkit/server__init.class, org/httpkit/server.clj or org/httpkit/server.cljc on classpath. However, lein classpath shows the dependencies just fine, and my code works when I hit lein run. Are there steps I need to take to get Cider to recognize the right classpath?


(And I've run cider-ns-refresh - it also fails).


Did you start your REPL before adding httpkit as a dependency? If so, just restart your REPL now that you've updated project.clj and connect back in from Spacemacs and you should be good to go.


There are ways to add dependencies to a running REPL but they're not really "beginner" level friendly 😐


Well that did it and fixed a few hours of frustration. Thank you so much! Minor follow-up question, the linter is still giving me a couple of errors, e.g. unresolved symbol defroutes. I'm not sure why since when I run this in the cider repl it definitely works. Any idea what's up?


Is that clj-kondo?


I've no idea how it would be set up in Spacemacs (I haven't used Emacs for years) but there's a #clj-kondo channel where they should be able to help. It may just be a matter of how you're requiring the namespace defroutes is in...


Ah, yeah [compojure.core :refer :all] -- that's not very good practice. It's better to use an alias (`:as`) or explicitly list the referred-in symbols.


Got it. Thank you so much for all the help!


[compojure.core :refer [defroutes GET POST]] is better and will satisfy the linter... well, once you are using POST in your code.


Perfect - I'm on my way. Appreciate it!