Fork me on GitHub

has the :extra-paths key in a deps.edn alias any impact to io/file path?


for example if I want to find a file with io/file can I use a shorter path to the file using the extra-paths in the alias

Alex Miller (Clojure team)12:03:09

No, paths is about the classpath

Alex Miller (Clojure team)12:03:56

You can use io/resource to read from the classpath though

👍 3
Jim Newton12:03:41

I have a sequence which may be a list or maybe not and may be very long. Is there a way to lazily convert it to a list? I.e. to wrap it with something for which I can easily call first and rest on it?

Jim Newton12:03:35

for example what does (map identity seq) do? does it do what I want? Is there a better way?


that will not cover the case of not a sequential object (map identity true) will fail

Jim Newton13:03:23

I do know that the given object is a sequence, that's guaranteed.


then map will do what you want


you might also just do (seq coll) which will change the interface to a seq, or will return nil if it’s not a collection. I think it’s faster than running map over the whole thing.


would lazy-seq help here?

Jim Newton13:03:23

WRT lazy-seq I need to do first/`rest` iteration over it. Actually I need to do first/`second`/`(comp rest rest)` iteration over it using loop/`recur` So would much rather it be a list an preferably a lazy list, rather than just a lazy seq.


(map identity seq) will return a lazy sequence -- it won't be of type PersistentList, but not sure that is what you are asking for by returning a list.

Jim Newton13:03:47

If I take a step back. Someone might be able to suggest a better alternative. Given a sequence of arbitrary (possibly very long) length, I want to partition the list into consecutive pairs, and possibly a left-over element in case the sequence has odd length. E.g., [1 10 2 20 3 30 4 40 5] -> [[[1 10] [2 20] [3 30] [4 40]] (5)] or [1 10 2 20 3 30 4 40] -> [[[1 10] [2 20] [3 30] [4 40]] ()]

Jim Newton13:03:17

i.e, I don't know until the end whether it was odd or even length


There are built-in functions partition and partition-all, one of which I believe can do this with the proper arguments.


but it will return a sequence, not a vector as implied by the square brackets in your example return values.


user=> (partition-all 2 [1 10 2 20 3 30 4 40 5])
((1 10) (2 20) (3 30) (4 40) (5))
user=> (partition-all 2 [1 10 2 20 3 30 4 40])
((1 10) (2 20) (3 30) (4 40))


ah, sorry, I misread your example return values.

Jim Newton13:03:17

so if I use partition how can I know whether the final element has the same length as everything else? Do I need to traverse down that and examine it? I've arranged for my algorithm to directly call reduce on the sequence of pairs.


You are hoping to return a sequential thing with two elements, where the second thing is lazily evaluated, so that it will suddenly traverse the entire input collection to get the last few elements (or an empty sequence) at that point in time?


user=> (partition 2 [1 10 2 20 3 30 4 40])
((1 10) (2 20) (3 30) (4 40))
user=> (partition 2 [1 10 2 20 3 30 4 40 5])
((1 10) (2 20) (3 30) (4 40))

Jim Newton13:03:32

no not exactly, I want to get as many pairs as possible and then maybe one more. In my current implementation I just always return a vector of length 2, the first element is a seq of pairs, and the second element is either an empty list or a singleton list.


If you don't need to know that last 'odd one out' element at all, partition can return the first sequence you describe.

Jim Newton13:03:10

yes but I need to know if there was a left-over element or not


It seems likely that copying and modifying the implementation of partition might enable that.

Jim Newton13:03:04

perhaps I can give partition a special padding element like :found-odd-element

Jim Newton13:03:59

I don't understand the step argument of partition

Jim Newton13:03:22

(partition 2 2 '(:odd-element) (range 11))
 -->.  ((0 1) (2 3) (4 5) (6 7) (8 9) (10 :odd-element))


;; uses the step to select the starting point for each partition


(partition 4 6 (range 20))
;;=> ((0 1 2 3) (6 7 8 9) (12 13 14 15))


step defaults to n if not provided


For your use case, I don't think you need the variant of partition that uses step


but yeah, it lets you repeat elements in the return sequences, or skip over some entirely.

Jim Newton13:03:33

(partition 2 2 () (range 11))
--> ((0 1) (2 3) (4 5) (6 7) (8 9) (10))

Jim Newton13:03:01

the problem is that to provide pad I am forced to provide step 😞


but why not just use partition-all?

Jim Newton13:03:38

yes partition-all is a better way of saying (partition n n () ...). thanks.

Jim Newton13:03:13

however, the question remains: how do I know whether the final element is complete or not? Do I have to traverse (again) over the list and detect it?


yes, but that will force realization of lazy sequence. Maybe you can describe the algorithm you are implementing?

Jim Newton13:03:02

if so, I'm probably re-implementing partition-all myself, as I've already done. right?

Jim Newton13:03:17

What is the algorithm I'm implementing? I'm trying to compare the time and accuracy of different strategies of the reduce function, given some associative binary operator. This amounts to drawing parentheses differently in an expression such as: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 (((((((( 1 + 2 ) + 3 ) + 4 ) + 5 ) + 6 ) + 7 ) + 8 ) + 9 ) ;; reduce (((( 1 + 2 ) + ( 3 + 4 )) + (( 5 + 6 ) + ( 7 + 8 ))) + 9) ;; my-function

Jim Newton13:03:35

first I add adjacent pairs, then I have a list half as long plus maybe one extra then I call recur to apply the same algorithm to that. eventually I'll be left with exactly one element which is the answer


and do you need to know if the collection has odd amount of element before realization?

Jim Newton14:03:47

if I have a list of pairs, I can add them by

Jim Newton14:03:22

(concat (map f seq-of-pairs) maybe-extra)


you can make a function f aware of the length of given list. If it has 2 element - operate normally, if 1 - simply return it

Jim Newton14:03:01

yes I considered that. that means it has to make the pair decision at every iteration.


(mapcat #(if (= 2 (count %)) (f %) %) (partition-all …))

Jim Newton14:03:46

yes, but If I'm going to count every pair, isn't it better to just count the list before I start?

Jim Newton14:03:28

although, what you suggest is not bad at all


> isn’t it better to just count the list before I start? that will realize entire list


if you need to keep it unrealized I wouldn’t recommend doing this

Jim Newton14:03:03

however, if I can partition a list into a [seq-of-pairs maybe-singleton]. then the recursive call

(recur (concat (map f seq-of-pairs) maybe-singleton))
is very elegant

Jim Newton14:03:49

You MUST get to the end to decide whether it is odd or even. so I'm undecided whether keeping it unrealised is really important. If the unrealised seq has length 2n or 2n+1 we're going to create a realised seq of length n.

Jim Newton14:03:24

this will give me the list to pass to the recursive call (ie. call to recur)

Jim Newton14:03:01

maybe-extra is either the empty list or a singleton list

Jim Newton14:03:10

yes, that was a suggestion, count the list first. I don't really need to do that theoretically because partition has already done it internally, just not given me any easy way to detect it.


@jimka.issy you may be interested in this presentation by Guy Steele

Jim Newton14:03:49

Nice video, I'll take a look. it looks indeed similar to what I'm doing.


hello team, Are we able to call one web request in another in ring

`   (POST "/query" [query :as {headers :headers}]
               (fn-call query)

               (POST "/fetch-rsult" []
                 (println "-----Hello world---")


output of /query shpuld pass to /fetch-rsult


in other words , output of one endpoint to another


@popeyepwr That isn’t calling anything — that’s just defining the routes.


You’d need to use a library like clj-http to perform an HTTP call if that’s what you really wanted. But I suspect you just want the handler to call another handler, not actually do an HTTP request?


Hi! I'm trying templating solutions but have troubles to try to preserve quotes in my template. Don't know how to preven clostache and selmer. Both seem to be built for html and replace quote by &quot:

(render "class \"{{class-name}}\" " {:class-name "foo"})
which should return class \"foo\"


@caumond If you know the substitution is safe you can use the |safe filter in Selmer.


That will stop it escaping things. But of course, be sure that the data you’re passing in is known to be HTML safe 🙂


it is not meant to be sent to any html viewer and execution is safe anyway