immutable | Immutable collections for Go | Map library
kandi X-RAY | immutable Summary
kandi X-RAY | immutable Summary
Immutable collections for Go
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of immutable
immutable Key Features
immutable Examples and Code Snippets
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
final int width;
final int height;
final int octaveCount;
final float persistence;
final long seed;
final String ch
public static int getTotalNumberOfLinesUsingScanner(String fileName) {
int lines = 0;
try (Scanner scanner = new Scanner(new FileReader(fileName))) {
while (scanner.hasNextLine()) {
scanner.nextLine();
Community Discussions
Trending Discussions on immutable
QUESTION
Is it possible to two reduce objects into one by summing their properties like so for any generic object
...ANSWER
Answered 2021-Jun-04 at 20:04A functional approach would be (but probably not clean)
QUESTION
I know integers are immutable so the computed values do not modify the original integers. Therefore the in-place operations should do the same as the simple operations, 1. compute the value and 2. reassign the value back to the variable. But why are the in-place operations slower than the simple ones?
...ANSWER
Answered 2021-Jun-13 at 11:37There may be a problem in that experiment: a single for-loop with 100 iterations and only containing an assignment statement like a=a+1
or a+=1
normally will not take that long to run (more than a second).
Compare those results using timeit
to the following direct execution of the same for-loop:
QUESTION
Question ahead:
why does in Java the call coll.contains(null)
fail for ImmutableCollections?
I know, that immutable collections cannot contain null-elements, and I do not want to discuss whether that's good or bad.
But when I write a Function, that takes a (general, not explicit immutable) Collection, it fails upon checking for nulls. Why does the implementation not return false (which is actually the 'correct' answer)?
And how can I properly check for nulls in a Collection
in general?
Edit:
with some discussions (thanks to the commenters!) I realized, that I mixed up two things: ImmutableCollection from the guava library, and the List returned by java.util.List.of, being some class from ImmutableCollections. However, both classes throw an NPE on .contains(null)
.
My problem was with the List.of
result, but technically the same would happen with guaves implementation.
ANSWER
Answered 2021-Feb-05 at 16:20why does in Java the call coll.contains(null) fail for ImmutableCollections?
Because the design team (the ones who have created guava) decided that, for their collections, null is unwanted, and therefore any interaction between their collections and a null
check, even in this case, should just throw to highlight to the programmer, at the earliest possible opportunity, that there is a mismatch. Even where the established behaviour (as per the existing implementations in the core runtime itself, such as ArrayList and friends, as well as the javadoc), rather explicitly go the other way and say that a non-sequitur check (is this pear part of this list of apples?) strongly suggests that the right move is to just return false
and not throw.
In other words, guava messed up. But now that they have done so, going back is potentially backwards compatibility breaking. It really isn't very - you are replacing an exception thrown with a false
return value; presumably code could be out there that relies on the NPE (catching it and doing something different from what the code would do had contains(null)
returned false instead of throwing) - but that's a rare case, and guava breaks backwards compatibility all the time.
And how can I properly check for nulls in a Collection in general?
By calling .contains(null)
, just as you are. The fact that guava doesn't do it right doesn't change the answer. You might as well ask 'how do I add elements to a list', and counter the answer of "well, you call list.add(item)
to do that" with: Well, I have this implementation of the List interface that plays Rick Astley over the speaker instead of adding to the list, so, I reject your answer.
That's.. how java and interfaces work: You can have implementations of them, and the only guardianship that they do what the interface dictates they must, is that the author understands there is a contract that needs to be followed.
Now, normally a library so badly written they break contract for no good reason*, isn't popular. But guava IS popular. Very popular. That gets at a simple truth: No library is perfect. Guava's API design is generally quite good (in my opinion, vastly superior to e.g. Apache commons libraries), and the team actively spends a lot of time debating proper API design, in the sense that the code that one would write using guava is nice (as defined by: Easy to understand, has few surprises, easy to maintain, easy to test, and probably easy to mutate to deal with changing requirements - the only useful definition for nebulous terms like 'nice' or 'elegant' code - it's code that does those things, anything else is pointless aesthetic drivel). In other words, they are actively trying, and they usually get it right.
Just, not in this case. Work around it: return item != null && coll.contains(item);
will get the job done.
There is one major argument in favour of guava's choice: They 'contract break' is an implicit break - one would expect that .contains(null)
works, and always returns false, but it's not explicitly stated in the javadoc that one must do this. Contrast to e.g. IdentityHashMap
, which uses identity equivalence (a==b
) and not value equality (a.equals(b)
) in its .containsKey
etc implementations, which explicitly goes against the javadoc contract as stated in the j.u.Map
interface. IHM has an excellent reason for it, and highlights the discrepancy, plus explains the reason, in the javadoc. Guava isn't nearly as clear about their bizarre null behaviour, but, here's a crucial thing about null in java:
Its meaning is nebulous. Sometimes it means 'empty', which is bad design: You should never write if (x == null || x.isEmpty())
- that implies some API is badly coded. If null is semantically equivalent to some value (such as ""
or List.of()
), then you should just return ""
or List.of()
, and not null. However, in such a design, list.contains(null) == false
) would make sense.
But sometimes null means not found
, irrelevant
, not applicable
, or unknown
(for example, if map.get(k)
returns null, that's what it means: Not found. Not 'I found an empty value for you'). This matches with what NULL means in e.g. SQL. In all those cases, .contains(null)
should be returning neither true nor false. If I hand you a bag of marbles and ask you if there is a marble in there that is grue, and you have no idea what grue
means, you shouldn't answer either yes
or no
to my query: Either answer is a meaningless guess. You should tell me that the question cannot be answered. Which is best represented in java by throwing, which is precisely what guava does. This also matches with what NULL does in SQL. In SQL, v IN (x)
returns one of 3 values, not 2 values: It can resolve to true
, false
, or null
. v IN (NULL)
would resolve to NULL and not false
. It is answering a question that can't be answered with the NULL value, which is to be read as: Don't know.
In other words, guava made a call on what null
implies which evidently does not match with your definitions, as you expect .contains(null)
to return false. I think your viewpoint is more idiomatic, but the point is, guava's viewpoint is different but also consistent, and the javadoc merely insinuates, but does not explicitly demand, that .contains(null)
returns false.
That's not useful whatsoever in fixing your code, but hopefully it gives you a mental model, and answers your question of "why does it work like this?".
QUESTION
I am attempting to use the WPGraphQL plugin for WordPress with PeachPie. I've built it out using a Visual Studio 2019 solution with two projects based on the ASP.NET Core Empty template with the Target Framework set to .NET 5.0.
I have successfully set up an initial project that utilizes the default Nuget package, PeachPied.WordPress.AspNetCore. It runs correctly, and renders the WordPress page as expected. The project file for this first one looks like this:
...ANSWER
Answered 2021-Jun-11 at 18:27It's a big in peachpie compiler itself.
https://github.com/peachpiecompiler/peachpie/issues/957
You van either delete the failing code from the php script or wait for the next release of peachpie.
QUESTION
I need to define a method that serializes objects for storage, but the semantics of it require that it can only work with objects whose fields keys are of string type (because they must match the column's names, so they cannot be numbers).
Using a Record
type won't work for this serializer input, because I am supplying it objects that have specific fields defined (like interface Foo {foo: number, bar: string}
and these are not assignable to Record
).
Same thing why we cannot do safely assign a List
to a List
if List
is mutable, but we can if they are immutable .
So I need to either specify that a generic type parameter's keyof T
is a subset of string
or have a ReadOnlyRecord
that is covariant with any interface that has string keys.
Suggestions?
...ANSWER
Answered 2021-Jun-11 at 13:25You do not need to take variance into account here. The reason why Record
fails is simply due to the utility requiring its type parameter to have an index signature. For the same reason, the method cannot expect to get a type assignable to { [x:string]: unknown }
as its only parameter.
In any case, I do not think you will be able to do that without generic type parameters. Then you simply need to check if a string-only keyed type like { [P in Extract : any }
is assignable to the passed-in type (the other way around will obviously always pass as your interfaces are subtypes of the former).
Note that X extends T ? T : never
in the parameter is needed for the compiler to be able to infer T
from usage while still maintaining the constraint. The only caveat is that you will not be able to catch symbol
properties (but will be if their type is unique symbol
):
QUESTION
From api call I get class ItemsResponse(val items: List) : PaginationResponse()
where abstract class PaginationResponse(var pagination: Pagination = Pagination())
. The thing is, PageableResponse and all responses in general shouldn't be mutable. The only place I'm making use of this mutability is when mocking response in Unit tests, something like:
ANSWER
Answered 2021-Jun-09 at 20:03Pass an argument through your sub-class constructor.
QUESTION
I don't really understand why but the dist folder is always empty and I get no errors
It says that everything was emitted, but in fact there is nothing in the destination folder?
...ANSWER
Answered 2021-Jun-09 at 12:42path: path.resolve(__dirname, 'dist')
QUESTION
Looking at the postgres function array_position(anyarray, anyelement [, int])
My problem is similar, but I'm looking for the position of the first value in an array that is greater than an element. I'm running this on small arrays, but really large tables.
This works:
...ANSWER
Answered 2021-Jun-09 at 06:19I don't think that there is a more efficient solution than yours, except if you write a dedicated C function for that.
Storing large arrays is often a good recipe for bad performance.
QUESTION
I was running the older 2.16.0 version of ChartMuseum Helm Chart. I am trying to update it to use newer 3.1.0. When I try to upgrade using helm upgrade -n , the upgradation fails with the following error:
...ANSWER
Answered 2021-Jun-08 at 22:50That's not from Helm, that's a Kubernetes error. This chart does not support clean upgrades or your values are not matching what you had before. If you can take the downtime, delete the offending deployment and let Helm recreate it. Otherwise you have to look up the right dance of orphan deletes and whatnot.
QUESTION
I am having lookups.tf file with the below content
...ANSWER
Answered 2021-Jun-08 at 13:26You have to use a different syntax when locals
block is defined. This should be added instead of what you currently have:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install immutable
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page