I have tried and I failed. Which cases that "ranges" used for?
var list = listOf(1,2,3,4,5)
It's worked well. But following code didn't work as I wanted. Why?
var list = listOf(1 .. 5)
Runnable program is : https://pl.kotl.in/c5256MZEC.
That syntax creates a list of ranges (List<IntRange>) with a single range in it. You can convert a range (or any Iterable) to a List with toList():
(1..5).toList()
You can also iterate through it without having to convert it to a list
var list2 = 1..5
list2.forEach {
println(it)
}
Another option (same as #Tenfour04 response but more readable) is to use the rangeTo operator and convert it to a list with the same extension function toList:
val list = 1.rangeTo(5).toList()
Related
adding to a list that is contained in livedata and adding elements to that list
val resourcesLiveData by lazy { MutableLiveData<List<File>>() }
I thought this should work as my LiveData is a list of files and I just want to add elements to it. But the value of live data is always an empty list. The res is the different file resources I am trying to add
resourceLiveData.value?.toMutableList()?.add(res)
So I tried it more expicity using this version but the list is still empty
val listOfRes = resourceLiveData.value ?: emptyList()
listOfRes.toMutableList().add(res)
resourceLiveData.value = listOfRes.toList()
Can anyone see if I am doing something wrong.
Just want to add to the list that is contained in the value
Agree to #KeyserSoze answer, if you have to use only List then you can do below
resourceLiveData.value = resourceLiveData.value?.toMutableList()?.apply { add(res) }?: emptyList()
You are creating a new object by calling toMutableList() instead of updating the original.
Change your LiveData type to MutableList:
val resourcesLiveData by lazy { MutableLiveData<MutableList<File>>() }
Then, update the value accordingly:
resourceLiveData.value?.add(res)
I create copy of one MutableList. When I update element value of copy MutableList But Original List element value also changed. When I use map It working fine but It is like a iteration of whole list, Is any way to do achieve without iteration ? how to copy elements of the MutableList.
val array: MutableList<UserData> = ArrayList()
val userData = UserData("DataOne")
array.add(userData)
val arrayCopy = ImmutableList.copyOf(array)// not working
//val arrayCopy = array.toMutableList()// not working
// val arrayCopy = array.map { it.copy() }.toMutableList()//working
Log.i("----> array ", array[0].name)//print DataOne
Log.i("----> arrayCopy ", arrayCopy[0].name)//print DataOne
arrayCopy[0].name = "DataTwo"
Log.d("----> array ", array[0].name)//print DataTwo
Log.d("----> arrayCopy", arrayCopy[0].name) //print DataTwo
ImmutableList.copyOf does copy the list. The problem is that you want to copy elements of the list as well. Of course you have to iterate over the list to do that, but so does copyOf and I don't see why you expect it's possible to avoid. A slightly better version of map is
array.mapTo(mutableListOf()) { it.copy() }
because it iterates only once.
Sorry but there wouldn't be any other way cause to convert one element you will have to read/copy it once, for n number of element you'll have to iterate n times to get the proper copy.
The only other way I can think of is to create the desired immutable/mutable list in the first place and not copying it all at once later.
Hope this helps
I'm new to Javascript and the google-api and still struggling with casting and basic functionality.
I'm trying to copy a column of strings harvested from a sheet to another array for manipulation and running into various errors. So far I have tried the following ( that have not generated straight up syntax errors )...
// Character VALIDATION function
function validateCharacter(characterName) {
var sheetName = ("!" + characterName);
var characterSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
// characterData array will hold the values from the characters sheet
var dataRange=characterSheet.getDataRange();
var characterData = dataRange.getValues();
var Physicals;
Physicals.copyofrange(characterData,[14][0],[31][0]);
console.log(Physicals);
}
and
// Character VALIDATION function
function validateCharacter(characterName) {
var sheetName = ("!" + characterName);
var characterSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
// characterData array will hold the values from the characters sheet
var dataRange=characterSheet.getDataRange();
var characterData = dataRange.getValues();
var Physicals;
for (i=0;i<17;i++){
Physicals[i]=characterData[i+13][0];
}
console.log(Physicals);
}
I know that Java handles strings as pointers, but I'm not clear if Javascript does and I thought of the for loop first but I'm getting
TypeError: Cannot set property "0.0" of undefined to "Agile". (line 40).
when I do that. I'm assuming I'm missing something... dimensional? about the declaration of the array to be copied to but I can't figure out what. I've looked over various solutions involving Java declarations for arrays but none of those seem to apply or be allowed in the google-spreadsheets-api Javascript. It seemed to not even acknowledge the String declaration when I tried String[]=new String etc. Frankly I don't care how it's implemented just so it works. Efficiency isn't a big concern on this project. Thank you.
OK, figured it out. After seeing all the Java things that Javascript does not support, I found some documentation specifically on Javascript string handling and figured out I have to cast Physicals as an empty array then "push" the values into it in the loop rather than assigning them with = statements, as such.
// Character VALIDATION function
function validateCharacter(characterName) {
var sheetName = ("!" + characterName);
var characterSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
// characterData array will hold the values from the characters sheet
var dataRange=characterSheet.getDataRange();
var characterData = dataRange.getValues();
var Physicals=[];
for (i=0;i<17;i++){
Physicals.push(characterData[i+13][0]);
}
return(Physicals);
}
Knowing the distinction between Java, Javascript, and Guava actually helped a great deal since they seem to have wildly different function handling despite similar names and common ancestry.
I wonder if it possible to write a user-defined store function for PIG that iterates twice over the data / input tuples.
I read here http://pig.apache.org/docs/r0.7.0/udf.html#Store+Functions how to write your own store function, e.g. by implementing your own "getNext()" method.
For my use case, however, it is necessary to see every tuple twice in the "getNext()" method, so I wonder whether there is a way to that, for example by reseting the reader somehow or by overwriting some other method...
Additional information: I am looking for a way to iterate from tuple 1 to tuple n and then again from 1 to n.
Does anyone has an idea how to do something like that?
Thanks!
Sebastian
This is from top of my head, but you could try something like this:
imports here ...;
class MyStorage extends PigStorage {
private int counter = 0;
private Tuple cachedTuple = null;
public Tuple getNext(){
if (this.counter++ % 2 == 0) {
this.cachedTuple = super.getNext();
}
return this.cachedTuple;
}
}
I have the following document in Mongodb:
{ "index_column" : "site_id", "mapped_column":"site_name"}
I'm using the mongo-scala-driver from my scala code to query the server. How do I get the mapped_column to a scala String variable? I tried with the following code:
val result = mongocollection.find(equal("index_column", data)).first()
It returns a org.mongodb.scala.ObservableImplicits$BoxedObservable#207c9b87
Is it possible to convert the Observable to an Iterator? I just need the mapped_column value in a scala String variable.
Scala driver is completely asynchronous, so you can subscribe for the result. The simplest way is to use foreach:
val result = mongocollection.find(equal("index_column", data)).first()
result.foreach { value => ... }
Another option is to convert Observable to Future:
Await.result(result.toFuture(), 3 seconds)
I resolved this issue by switching the driver to mongodb casbah and it can be done with the following code:
val result = mongocollection.find(MongoDBObject("index_column"-> "site_id")).one()
println(result.get("mapped_column"))