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
No, paths is about the classpath
You can use io/resource to read from the classpath though
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?
for example what does (map identity seq)
do? does it do what I want? Is there a better way?
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?
that will not cover the case of not a sequential object (map identity true) will fail
I do know that the given object is a sequence, that's guaranteed.
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.
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]] ()]
i.e, I don't know until the end whether it was odd or even length
then map will do what you want
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.
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))
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.
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.
perhaps I can give partition a special padding element like :found-odd-element
I don't understand the step
argument of partition
(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.
(partition 2 2 () (range 11))
--> ((0 1) (2 3) (4 5) (6 7) (8 9) (10))
the problem is that to provide pad
I am forced to provide step
đ
but why not just use partition-all?
yes partition-all
is a better way of saying (partition n n () ...). thanks.
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?
if so, I'm probably re-implementing partition-all myself, as I've already done. right?
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
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?
if I have a list of pairs, I can add them by
(concat (map f seq-of-pairs) maybe-extra)
this will give me the list to pass to the recursive call (ie. call to recur)
maybe-extra is either the empty list or a singleton list
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.
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
@jimka.issy you may be interested in this presentation by Guy Steele https://vimeo.com/6624203
yes I considered that. that means it has to make the pair decision at every iteration.
(mapcat #(if (= 2 (count %)) (f %) %) (partition-all âŚ))
Nice video, I'll take a look. it looks indeed similar to what I'm doing.
yes, but If I'm going to count every pair, isn't it better to just count the list before I start?
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
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 elegantYou 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.
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 preserve quotes in my template. I tried clostache and selmer and both replace quote by ". They both seem to be meant for html:
(render "class \"{{class-name}}\" " {:class-name "foo"})
I expect a templating solution returning 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