beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
yiorgos 2021-03-18T11:49:35.070500Z

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

yiorgos 2021-03-18T11:50:29.071600Z

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

alexmiller 2021-03-18T12:17:09.072200Z

No, paths is about the classpath

alexmiller 2021-03-18T12:17:56.073100Z

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

👍 1
Jim Newton 2021-03-18T12:53:41.074200Z

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 Newton 2021-03-18T12:54:35.074800Z

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

thosmos 2021-03-29T19:11:28.253800Z

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.

dharrigan 2021-03-18T12:58:11.075Z

would lazy-seq help here?

2021-03-18T13:18:21.075100Z

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

Jim Newton 2021-03-18T13:32:23.075300Z

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

Jim Newton 2021-03-18T13:34:23.077300Z

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.

2021-03-18T13:36:01.079400Z

(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 Newton 2021-03-18T13:36:47.080300Z

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 Newton 2021-03-18T13:37:17.080800Z

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

2021-03-18T13:37:27.081100Z

then map will do what you want

2021-03-18T13:37:28.081300Z

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

2021-03-18T13:37:48.081800Z

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

2021-03-18T13:38:43.082100Z

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

2021-03-18T13:39:12.082500Z

ah, sorry, I misread your example return values.

Jim Newton 2021-03-18T13:41:17.084500Z

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.

2021-03-18T13:41:27.084800Z

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?

2021-03-18T13:43:26.086600Z

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 Newton 2021-03-18T13:43:32.087Z

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.

2021-03-18T13:43:50.087500Z

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

Jim Newton 2021-03-18T13:44:10.087800Z

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

2021-03-18T13:44:55.088400Z

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

Jim Newton 2021-03-18T13:45:04.088700Z

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

Jim Newton 2021-03-18T13:46:59.089Z

I don't understand the step argument of partition

Jim Newton 2021-03-18T13:48:22.089500Z

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

2021-03-18T13:48:27.089700Z

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

2021-03-18T13:48:35.089900Z

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

2021-03-18T13:49:05.090600Z

step defaults to n if not provided

2021-03-18T13:49:07.090800Z

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

2021-03-18T13:49:31.091600Z

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

Jim Newton 2021-03-18T13:49:33.091700Z

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

Jim Newton 2021-03-18T13:50:01.092100Z

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

2021-03-18T13:50:39.092400Z

but why not just use partition-all?

Jim Newton 2021-03-18T13:51:38.093300Z

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

Jim Newton 2021-03-18T13:52:13.094200Z

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?

2021-03-18T13:53:59.095500Z

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

Jim Newton 2021-03-18T13:54:02.095600Z

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

Jim Newton 2021-03-18T13:58:17.099200Z

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 Newton 2021-03-18T13:59:35.101Z

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

2021-03-18T13:59:36.101100Z

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

Jim Newton 2021-03-18T14:00:47.102Z

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

Jim Newton 2021-03-18T14:01:22.103100Z

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

Jim Newton 2021-03-18T14:02:24.103700Z

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

Jim Newton 2021-03-18T14:03:01.104800Z

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

Jim Newton 2021-03-18T14:04:10.106400Z

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.

2021-03-18T14:04:23.106800Z

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

ghadi 2021-03-18T14:04:43.107200Z

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

1
Jim Newton 2021-03-18T14:05:01.107600Z

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

2021-03-18T14:05:02.107800Z

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

Jim Newton 2021-03-18T14:05:49.108400Z

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

Jim Newton 2021-03-18T14:06:46.108600Z

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

Jim Newton 2021-03-18T14:07:28.108800Z

although, what you suggest is not bad at all

2021-03-18T14:08:40.109Z

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

2021-03-18T14:09:03.109200Z

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

Jim Newton 2021-03-18T14:10:03.109400Z

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 Newton 2021-03-18T14:11:49.109600Z

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.

popeye 2021-03-18T19:05:54.111600Z

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

popeye 2021-03-18T19:06:25.112200Z

output of /query shpuld pass to /fetch-rsult

popeye 2021-03-18T19:07:44.112800Z

in other words , output of one endpoint to another

seancorfield 2021-03-18T19:12:31.113400Z

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

seancorfield 2021-03-18T19:13:19.114400Z

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?

caumond 2021-03-18T21:53:28.117400Z

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 &quot. They both seem to be meant for html:

(render "class \"{{class-name}}\" " {:class-name "foo"})
I expect a templating solution returning class \"foo\"

seancorfield 2021-03-18T22:49:07.118200Z

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

seancorfield 2021-03-18T22:49:45.119Z

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

caumond 2021-03-18T22:56:46.119900Z

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