Fork me on GitHub
#beginners
<
2021-03-18
>
yiorgos11:03:35

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

yiorgos11:03:29

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?

delaguardo13:03:21

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.

delaguardo13:03:27

then map will do what you want

thosmos19:03:28

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.

dharrigan12:03:11

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.

andy.fingerhut13:03:01

(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

andy.fingerhut13:03:28

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

andy.fingerhut13:03:48

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

andy.fingerhut13:03:43

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))

andy.fingerhut13:03:12

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.

andy.fingerhut13:03:27

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?

andy.fingerhut13:03:26

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.

andy.fingerhut13:03:50

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

andy.fingerhut13:03:55

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))

delaguardo13:03:27

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

delaguardo13:03:35

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

delaguardo13:03:05

step defaults to n if not provided

andy.fingerhut13:03:07

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

andy.fingerhut13:03:31

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 😞

delaguardo13:03:39

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?

delaguardo13:03:59

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

delaguardo13:03:36

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)

delaguardo14:03:23

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.

delaguardo14:03:02

(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

delaguardo14:03:40

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

delaguardo14:03:03

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.

ghadi14:03:43

@jimka.issy you may be interested in this presentation by Guy Steele https://vimeo.com/6624203

3
Jim Newton14:03:49

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

popeye19:03:54

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---")
                 )

popeye19:03:25

output of /query shpuld pass to /fetch-rsult

popeye19:03:44

in other words , output of one endpoint to another

seancorfield19:03:31

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

seancorfield19:03:19

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?

caumond21:03:28

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\"

seancorfield22:03:07

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

seancorfield22:03:45

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

caumond22:03:46

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