How to name a method that does getting and removal from the collection? - naming-conventions

How to name a method that does getting and removal from the collection?
Say we have a collection Fruits. To get apples we would do something like apples = Fruits.Get("apples"). To remove - Fruits.Remove("apples"). If we want to do both in one method it would return apples and remove them form the collection. Something like apples = Fruits.Take("apples"). Any better ideas than "Take"?

Either remove() or take() are fine.
For example:
In java.util.Map, the remove() method gets and removes an object specified by a key.
In JavaSpaces, the take*() methods get and remove and object specified by a template.

pop(index) seems a reasonable misuse of stack's method to me.

Somewhat language specific but something like:
Object remove(int index)

Related

V-Model not cleansed when adding Object to array (Vue-Js)

I'm trying to add objects (simple key-value pairs) to a list.
However, the v-model is still bound to the previously added objects, so if I add "ObjectOne" vith "ValueOne", then try to add "ObjectTwo" with "ValueTwo", "ObjectOne" gets edited AND "ObjectTwo" gets added.
I am by no mean an expert in Javascript, so it might not be related to VueJS.
I can obviously make this work with a method per list .
The point is that my model has multiple lists of key value pair to be edited, so I tried making a generic method :
addToList: function(value, list){
console.log("Adding " + value + " to list "+list);
list.push(value);
value={};
},
This method works if used on "simple" lists (like an array of string), but not on "objects" list.
My guess is that as I try to clean "value" instead of "this.value", the reference still points to the same object, but since I don't know what "value" will be when called, I don't know how to do this.
Here is the fiddle with a re-creation of my issue.
My objective would be to be able to use the "addToList" function to add to any list, without having to re-write a function for each list.
Thank you for your help.
The above behaviour is because you are updating the value of same object whenever you add a new todo task.You need to set your object again to add new values as below.
addToList(value, todos){
this.todos.push(value);
this.anotherTodo={ text:'',
done:'false'}
}
Working fiddle here.
I post this as an answer, but if someone has a better way to do it, I'm all hear.
I solved the way by adding a watch on my list. When the list changes, I clean the model object that's added to it.
In my production work, I had to add a computed property, since I can't add a watch on an object's property, then a watch on said computed property :
watch:{
todos(){
this.anotherTodo={};
},
fiddle as demo

Does Kotlin's MutableList add() function always add to the end of the list?

I am adding an element into a MutableList and I want to know its index.
mutableList.add(foo)
Will the index of the most recently added element always be the last index mutableList.size - 1 like it is with ArrayList?
I don't want to use mutableList.indexOf(foo) because I believe it takes O(n). I'm not finding lot of documentation on these.
Yes, MutableList.add() always adds to the end of the list (this is a requirement for any class implementing the interface). I've filed an issue to say this explicitly in the documentation.
MutableList is merely an interface. How are you initializing the list?
Using Kotlin methods like mutableListOf() creates ArrayList objects by default.
You raise a question that has never occurred to me since from testing and experience I've found that all new elements added with the add method are placed at the end.
I believe that this is the case and won't change.
Although not mentioned in the documentation, this case holds. If it didn't then for sure it would be in the documentation. Just like Set where it says
A generic unordered collection
On JVM, MutableList is equivalent to java.util.List whose add documentation does specify
Appends the specified element to the end of this list

How to implement introspection on RealBasic?

RealBasic's Introspection is kinda of different than what I expected.
My intention is:
Create a MainObject from which other objects will inherit two, three methods, to simplify.
Method 1-> Returns to the child class itself all of its properties, types and values.
Method 2-> Would call Method1 and with the information, save the child Object.
So, for method 1 I thought about writing a generalised introspection which for each child class would easily return what I need for Method 2 to do its work.
Why do I want this? So I can have tens of objects knowing how to save, draw themselves without worrying too much about a modification here or there on the properties etc...
But using what RealBasic tutorials and reference offers doesn't work, since it requires me to have it happening outside the object etc... i.e.: I can easily, inside ObjectA, get ObjectB's properties, methods etc, but I want to get inside ObjectA, A's properties, not B's
Thanks in advance...
I've found out how... Very simple, create the MainClass and inside of it a simple method WhoAmI which could return an array, dictionary etc...
Dim thisClassTypeInfo As Introspection.TypeInfo = Introspection.GetType(Self)
Dim thisClassProperties() As Introspection.PropertyInfo = thisClassTypeInfo.GetProperties
Dim thisClassMethods() As Introspection.MethodInfo = thisClassTypeInfo.GetMethods
For Each myProperty As Introspection.PropertyInfo In thisClassProperties
// Then here use myProperty.Name, myProperty.Value(Self).StringValue
// or myProperty.Value(Self).anyotheroption and create a dictionary
// or array with the results and return it.
Next

Why do we have mutableOrderedSetValueForKey yet not OrderedSetValueForKey?

What about if we just want to look at what's there rather wanting to change stuff and then we want to access it by it's key rather than using dot notation?
For example:
I have object called CatalogData
I can do CatalogData.Images to get the pages.
However, say I have a function that return NSManagedObject and I want to pass #"Images" to that function.
So, eventually it'll get something like
[anNSManagedObjectThatisactuallyaCatalogData orderdSetforKey:#"Images"]
Well, we can't have that.
So why?
Should we use the good old objectForKey?
You can just use
[anNSManagedObjectThatisactuallyaCatalogData valueForKey:#"Images"]
which returns an NSOrderedSet if "Images" is an ordered to-many relationship.

A function that will convert input string to the actual object

I am unsure how to describe what I am looking for, so hopefully the situation will make it somewhat clear.
I have an object with a number of properties (let's say object.one, object.two, object.three). There are about 30 of these properties and they all hold a string ("Pass" or "Fail").
Right now the existing code checks whether the property has value "Pass" or "Fail" and then runs some code that prints stuff out. That is, the same snippet of code is duplicated 30 times, one for each of these properties.
The code looks something like this
If (object.one = ... )
...
End if
If (object.two = ... )
...
End if
If (object.three = ... )
...
End if
I want to use a loop to clean this mess up (each block is huge), but am not sure how to do it. I was thinking perhaps there was a way such that I might be able to construct a string like "object.one" and run some function that will tell the compiler that this is actually an object's property?
That way I could create an array containing the object's name like my array = {"object.one", "object.two", "object.three"} and then do something like, in pseudocode
For each string in my array
If (some_function(string) = ...)
...
End If
Essentially, it would take those massive blocks of duplicated code and reduce it to just one block. Is there such a some_function that I am looking for?
This is in VB.net.
I'm not sure i've understand but you can use reflection and get object properties runtime ?
With reflection you can access public object properties, use PropertyFiled.GetValue() to get one, two, etc. and build an array (i suppose that one, two, etc are object Properties, true?)
Here you can find more information: http://msdn.microsoft.com/en-us/library/system.reflection.aspx
Sorry for my bad english, i'm italian.
You seem to be describing serialization, which is the act of converting object state to a format that can be stored/transmitted and deserialization, which is the opposite.
The .NET framework has several different serializers that can work with text - either XML or JSON - the DataContractSerializer for XML and the DataContractJsonSerizlizer for JSON amongst them.