This page is not created by, affiliated with, or supported by Slack Technologies, Inc.

## 2019-03-27

## Channels

- # aleph (5)
- # announcements (17)
- # beginners (200)
- # cider (25)
- # cljdoc (4)
- # cljsrn (3)
- # clojure (90)
- # clojure-europe (3)
- # clojure-finland (5)
- # clojure-france (1)
- # clojure-houston (1)
- # clojure-italy (8)
- # clojure-nl (15)
- # clojure-spec (24)
- # clojure-uk (20)
- # clojurescript (199)
- # core-async (2)
- # cursive (45)
- # data-science (14)
- # datomic (33)
- # duct (13)
- # fulcro (4)
- # graphql (3)
- # kaocha (9)
- # leiningen (24)
- # nrepl (16)
- # off-topic (105)
- # pathom (15)
- # pedestal (28)
- # re-frame (1)
- # reagent (14)
- # shadow-cljs (28)
- # spacemacs (8)
- # tools-deps (8)
- # vim (4)

Hi, so I feel like I’m going crazy. I’m relatively new to Clojure (been using it on and off for a month or so) and I’m going through problems on 4Clojure. I’m on question 95 (http://www.4clojure.com/problem/95) and I’ve been wrestling with it for a few hours now and have absolutely no idea why it’s not passing the unit tests. I can’t see how it’s logically different from an answer I found online (the answer-key version is far more elegant, that’s for sure). Can somebody lend me a second pair of eyes? What am I doing wrong? My version:

```
(fn tree? [tree]
(if (= tree nil)
true)
(if (seq? tree)
(if (= (count tree) 3)
(if (and (tree? (second tree)) (tree? (last tree)))
true
false)
false)
false))
```

The answer key (https://gist.github.com/SegFaultAX/3607101 )’s version:
```
(fn tree? [coll]
(cond
(or (seq? coll) (vector? coll))
(and (= 3 (count coll)) (tree? (nth coll 1)) (tree? (nth coll 2)))
(nil? coll) true
:else false))
```

Here's a properly indented version of your solution. Might help:

```
(fn tree? [tree]
(if (= tree nil)
true)
(if (seq? tree)
(if (= (count tree) 3)
(if (and (tree? (second tree)) (tree? (last tree)))
true
false)
false)
false))
```

It's a named anonymous function; the name is scoped to the function body. You can use them for recursion, and they also can make stacktraces a little bit easier to read.

I have to be honest. Its difficult to line up the parans... can you use a cond ?

@ I don’t see what’s wrong with it. Do `if`

statement *have* to have two branches or something?

```
(fn tree? [tree]
(an-expression)
(another-expression)
(and-another-expression) <-- this will be returned
)
```

```
(fn tree? [tree]
(if (= tree nil)
true) <--- this will never be returned
(if (seq? tree)
.... <snip>
```

This is merely an initial suggestion. There are many other ways to clean up this code

I just tried that question and noticed that `seq?`

doesn't work for vectors, only lists, whereas `sequential?`

works for both.

```
(fn istree [atree]
(or
(nil? atree)
(and (sequential? atree) (= 3 (count atree))
(not (nil? (first atree)))
(istree (second atree))
(istree (nth atree 2))
)
))
```

Is anyone using vim with emacs? Evil mode. On OSX. Wiii. I got it to work. Just had to restart emacs.

Is there a limit to lazy sequences? I am trying to solve Project Euler #357, which basically requires generating primes up to 100 million. The general `prime?`

filter I'd cooked up (more or less memoized trial division) is too slow for this particular problem, so I borrowed a lazy-sequence solution from someone on SO:

```
(def lazy-primes
;; Adapted from
```
(letfn [(no-smaller-prime-factors? [x]
(not-any? (fn [p] (divisible? x p))
(take-while #(<= % (Math/sqrt x)) lazy-primes)))]
(lazy-seq (filter no-smaller-prime-factors? (drop 2 (range))))))

There is nothing wrong with my algorithm to solve the problem (as I've tested it on smaller limits), but I kept getting the wrong answer. Finally, after banging my head against the wall tons of times, I finally realized that the lazy sequence craps out somewhere between 1 and 10 million such that `count`

ing the items in the sequence above some `n`

does not yield a higher count.
Lo and behold,
`(nth lazy-primes 800000)`

gives me
```
Execution error (IndexOutOfBoundsException) at scratch.euler2/eval48451 (form-init5213815999557406566.clj:1).
null
```

Can anyone shed some light on this? And how I might work around it?I noticed that the error is an Index out of bounds, which I guess is coming from the nth. (last (take 800000 lazy-primes)) worked for me, don't know why nth is limited though

@nathantech2005 re: long limit—I didn't think of that! I thought "oh, I'm just finding square roots, there's no need to coerce to bigint (or place to do so)." Interestingly, I was able to get around the Out of Bounds error by changing `Math/sqrt x`

to `tower/expt x 1/2`

, but finding the 800,000th prime seems like it will take all afternoon.
re: function crashing—I'm just running it in the REPL. No crash at 700,000 or below.

I cannot get a value higher than 8325781, which is far less than the 800,000th prime.

Hmm.. index error is not a stack error though. I dont think its a 64 long max problem

```
user=> (defn divisible? [i p] (zero? (rem i p)))
#'user/divisible?
user=> (def lazy-primes
#_=> ;; Adapted from
```
#_=> (letfn [(no-smaller-prime-factors? [x]
#_=> (not-any? (fn [p] (divisible? x p))
#_=> (take-while #(<= % (Math/sqrt x)) lazy-primes)))]
#_=> (lazy-seq (filter no-smaller-prime-factors? (drop 2 (range))))))
#'user/lazy-primes
user=> (time (nth lazy-primes 800000))
"Elapsed time: 29022.974863 msecs"
12195263

Weird, I just tried to "refresh" the function by converting it back to `Math/sqrt x`

(bad idea) and then back to `tower/expt x 1/2`

again. It seems to get stuck at the first value I find? i.e., after clearing the cached sequence from memory, this is what I get:

```
(last (take 100000 lazy-primes))
=> 364031
(last (take 300000 lazy-primes))
=> 364031
(last (take 600000 lazy-primes))
=> 364031
```

I have no idea. I'm working in Atom via Proto-REPL. Must be some memory issue because I'm now getting out of bounds errors even on `nth lazy-primes 40000`

I guess?

I think you are getting the same number, because a lazy seq caches the first result

But I cannot find a max allowed on the sequence data type... I'd assume it depends on ram, memory

Because the default JVM can eat a ton of memory... depends on underlieing data structures and complexity big O

Ah, I restarted Atom and the REPL and I'm getting results again. (Except with `nth`

, as you say, which is still throwing the Index OOB error.) It is already becoming too slow at `(last (take 200000 lazy-primes))`

though. Worse yet, I need all of the primes up to `n`

(100 million), not just the `nth`

, and I have no idea how many primes that is.
So frustrating because everyone says this problem is so easy! Conceptually it isn't very hard but I just can't get my computer to do it. I might end up just rewriting this one in Python just to get it over with. Thanks for your help guys!

That makes sense. But even if `nth`

was O(1), it wouldn't really help much in this case since I need them all 😕

It depends... if you call nth each time in a loop, that is linear times linear = exponential

It's missing because the first result from the lazy-seq is cached, right? Because now `(time (count (doall (take 400000 lazy-primes))))`

is giving me the same count for 400k, 1M, 10M, etc...

So basically I should just restart the REPL, run the code for the problem **without** testing intermediate values, and let the program run all afternoon until it gets the answer?

No, not testing for intermediate values in the program. Just testing in the REPL (but results are still cached)

Problem is when I try to test 800,000 right off the bat, I have no idea how long it is going to take (and the real count I need is, well, I have no idea). Seems like this is one I should just let run overnight

Id modify it into a batch... estimate time for each 10,000 #s, resets memory resources, see where it crashes

Cool, thanks for your help. I don't have a C++ background so I am pretty clueless about memory allocation and all the really low-level stuff. Although I do get the impression that Clojure is not the best fit for this problem anyway (given the ginormous range of numbers involved). There aren't many, but I had to raise the white flag on a couple others and switch over to Python.

I can't repro your IndexOutOfBoundsException -- I can get `(nth lazy-primes 1000000)`

just fine (but it takes a long time to get there)

But as it stands your `lazy-primes`

will eventually eat up all available memory because you're holding on to the entire sequence there.

It must have been a memory issue on my machine, because the upper bound that would trigger the exception was not consistent.

But Euler 357 doesn't require you calculate all those primes. It just requires that you have a fast way of determining whether any given number *is* prime or not.

And it also wants you to produce the list of divisors for any given number. Which is a related computation (primes have no divisors other than themselves or 1).

Well, I have another non-lazy-seq function that I use for most problems. It is technically a primality checker and I use it, memoized, with `filter?`

on `range`

to generate a sequence, which (for its simplicity) is surprisingly fast up to a decent number (a million or so?). But it was too slow for this problem.
The reason I wanted to calculate all the primes is because the initial set of candidates will be all numbers that are 1 less than a prime. (And the valid numbers are a subset of those.) So I thought it would be faster to do it that way than to test every even number.
The rest of the problem (divisors and all) is manageable; I'm getting correct results as long as I can generate primes correctly.

Fair point. I strongly suspect the "best" answer is something mathematically clever rather than anything brute force (which tends to be the case with a lot of these Euler problems... which is why I gave up on Project Euler... and *I'm a mathematician by training!*).

Ya... example... there is a formula to find nth fibonacci... called Binet Formula

Certainly given unlimited memory you can calculate and cache them all (which is probably why they set the limit to be 100,000,000 to try to prevent brute force solutions?).

There's a lot of filtering you can do since for `d + n/d`

the divisors are going to be symmetric (5 + 30/5 is the same as 6 + 30/6, and the same is true for all the smaller/larger divisors).

Yeah, absolutely. So I looked at this problem up, down and sideways but still couldn't think of any fast, intuitive way to check the validity of a given number. Of course, I don't have a math background at all; just getting into "real math" for the first time since high school.

Everything is divisible by 1. 289 is not divisible by 2..9 tho' but it isn't prime.

I love how it's "tune in next week to learn about the 5% of numbers for which this doesn't work!"...

sicp contains a primality check that works for all but a very small number of primes (chapter 2 i think)

check probabilistic methods: http://sarabander.github.io/sicp/html/1_002e2.xhtml#g_t1_002e2_002e6

That's been fun, but I'm out for the night. See y'all tomorrow. I'll leave my REPL running that to see what I get...

> Numbers that fool the Fermat test are called Carmichael numbers, and little is known about them other than that they are extremely rare. There are 255 Carmichael numbers below 100,000,000. The smallest few are 561, 1105, 1729, 2465, 2821, and 6601. In testing primality of very large numbers chosen at random, the chance of stumbling upon a value that fools the Fermat test is less than the chance that cosmic radiation will cause the computer to make an error in carrying out a “correct” algorithm. Considering an algorithm to be inadequate for the first reason but not for the second illustrates the difference between mathematics and engineering.

Hmm... If that's the case, I wonder if something not too elaborate like using Java interop for `isProbablePrime`

would be sufficient to make the problem tractable. Will have to try after lunch

If you use a clock, base 12. You will see the pattern... I found a video on it

Omg... nevermind. 289 would go in the power. Meaning a 289th degree polynomial

https://math.stackexchange.com/questions/913287/relationship-between-primes-and-fibonacci-sequence

There are pretty good reasons why factoring very large numbers is computationally difficult, although I hear from a friend who has been looking seriously into the issue that quantum computers are likely to be able to start doing a few problems faster than today's computers in 5 years or so. I am not 100% certain whether factoring large numbers is one of them, but I think it is.

@dpsutton Thanks for suggesting a probabilistic method. I used `isProbablePrime`

to create the initial list of candidates, and then used my trial division primality checker to confirm the validity of the candidates. My machine managed to get the correct answer after 13.5 minutes.

```
(defn problem-357 [lim]
"Find the sum of all positive integers n not exceeding 100 000 000
such that for every divisor d of n, d+n/d is prime."
(let [primes (->> (range (inc lim))
(filter #(.isProbablePrime (BigInteger/valueOf %) 5)))
candidates (map dec primes)
valid? (fn [n]
(loop [d 2]
(cond
(> d (tower/sqrt n)) true
(or (not (h/divisible? n d))
(h/prime? (+ d (/ n d)))) (recur (inc d))
:else false)))]
(reduce + (filter valid? candidates))))
(time (problem-357 100000000))
;; Elapsed time: 808512.398784 msecs
```

Finally, I can forget about this stupid problem! Thanks for your help everyone.Have you tried `lein run &`

to see if it will run in the background that way, assuming that the command shell you are using, e.g. bash, uses the syntax ` &`

at the end of the command to start the process in the background?

it will not start to listen for ports unless I run `fg`

later - weird because if I run simple bash script this way it will run in background.

I'd recommend asking in the *#leiningen* channel -- someone there may know more about why that is, or if there is a way to make it work as you are hoping.

Hi, Is there a way to do a`trigger fn if atom value changes`? similar how reagent atom works - trigger rerender once atom state changes? but in my case call that fn

@lepistane you probably looking for https://clojuredocs.org/clojure.core/add-watch

registers a function that gets called every time the passed in agent/atom/var/ref changes

less spam for people like this.
Basically i have an atom that holds information about task. `running`

true/false.
There could be situation where i have task running in the background meaning `:running true`

.
but i've turned off it's next execution.
and new task is being submitted.
(button mashing)
instead of new task running while old one is running i would like if it triggered after (once task is done it puts `:running false`

in atom)

sorry, it's hard to notice thread replies... Feels like you would have one internal atom per task, and something around that can check in general if tasks are running. I'm sorry, but I still quite don't understand the issue clearly

I'm working on this problem: http://www.4clojure.com/problem/118
And I am attempting to solve with `lazy-seq`

as this seems to make sense. However, I can't figure out why this is not working.

```
(defn my-map
[f s]
(lazy-seq (cons (f (first s)) (my-map f (rest s)))))
```

It's the first time I've tried to use `lazy-seq`

. When I run the following code I get a nullpointerexception.
```
clojure-test.core=> (my-map inc [1 2 3])
NullPointerException clojure.lang.Numbers.ops (Numbers.java:1013)
```

What happens when s becomes empty? You haven't defined a base case for your recursive call, so eventually s will be empty. Then `(inc nil)`

gives your null pointer

Hmm i was wondering about this, and if that is the case where would the check go? I see in this example they use `rest`

similarly with no check. https://clojuredocs.org/clojure.core/lazy-seq#example-542692d3c026201cdc326ff1

That `sieve`

returns an infinite sequence. With lazy sequences you can do that (ex. `(range)`

tries to return every number from 0 on) - note that they call `take 20`

when they use it, because otherwise it would try to give them every prime number

quick question guys I’m using component and I have some Services ( http clients ) which need some configuration ( api-tokens / auth … ) is it ok to create a component for that Http Clients ?

it is a matter of taste. when using component I like for any services used to be made a first class value as their own component

so for example if I was using the github api, instead of creating a http client component I would make a github api component

If you create a list of `futures`

and then dereference them right after creation, is that considered pointless? Couldn't you have just ran the functions sequentially?

`result [(future (ten-min-func)) (future (five-min-func)) (future (twenty-min-func)))]`

and then `(map deref result)`

they're still running in the background, and you're still waiting for all of them to complete

do you might not be waiting at the point of the code that runs `(map deref result)`

. Whoever realizes that seq will do the waiting

you also have to careful when creating futures, if you create them in a lazy seq they of course won't run until the seq is realized

so for example using for or map to create a seq of futures, then maping deref, then forcing that seq is bad

that's the scenario that hiredman was mentioning. `map`

is lazy, so the futures (first map) won't be launched until the second map happens

like in your original Q https://clojurians.slack.com/archives/C053AK3F9/p1553711929647700

@mario.cordova.862 for `(let [futures (map ...)] (doall (map deref futures)))`

the futures aren't **created** until the ones before them return

you want to create all of them in one go, or at least explicitly control how many are in flight instead of relying on lazy-seq internals for the pipelining