Surprise, surprise. At least the stack trace wasn't 1,000,000 lines longjava AbsentFibonacciDemo
1000000
Exception in thread "main" java.lang.StackOverflowError
at java.base/java.lang.Long.hashCode(Long.java:1404)
java AbsentFibonacciDemo
2000
Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1138)
at AbsentFibonacciDemo.fib(AbsentFibonacciDemo.java:25)
at AbsentFibonacciDemo.lambda$fib$0(AbsentFibonacciDemo.java:25)
at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1137)
at AbsentFibonacciDemo.fib(AbsentFibonacciDemo.java:25)
at AbsentFibonacciDemo.lambda$fib$0(AbsentFibonacciDemo.java:25)
at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1137)
at AbsentFibonacciDemo.fib(AbsentFibonacciDemo.java:25)
Which appears to mean you can't calculate big Fibonacci numbers like that. Only a key one larger than the preceding keys in the Map. So today I went to look in the documentation (link above) and it seems the computeIfAbsent method is intended for computing a single value to be put into the Map; since the method I was using would appear to be attempting to put multiple values into the Map, that isn't allowed and any attempt would appear to require a concurrent modification exception be thrown.java AbsentFibonacciDemo
0
fib(0) = 0
1
fib(1) = 1
2
fib(2) = 1
3
fib(3) = 2
4
fib(4) = 3
5
fib(5) = 5
6
fib(6) = 8
7
fib(7) = 13
8
fib(8) = 21
9
fib(9) = 34
10
fib(10) = 55
11
fib(11) = 89
-1
Isn't that in line with the conventions of functional programming? Altering the Map anywhere else would constitute a side effect and side effects are to be avoided.That API link (computeIfPresent) wrote:The remapping function should not modify this map during computation. . . . Non-concurrent implementations should override this method and, on a best-effort basis, throw a ConcurrentModificationException if it is detected
I spent the morning putting in a comma and the afternoon removing it.
-- Gustave Flaubert, French realist novelist (1821-1880)
There are three kinds of actuaries: those who can count, and those who can't.
There are three kinds of actuaries: those who can count, and those who can't.
There are three kinds of actuaries: those who can count, and those who can't.
I spent the morning putting in a comma and the afternoon removing it.
-- Gustave Flaubert, French realist novelist (1821-1880)
There are three kinds of actuaries: those who can count, and those who can't.
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
|