Hey! I want to get array[0].. But I want to ensur...
# announcements
m
Hey! I want to get array[0].. But I want to ensure that the array is at least size >= 1 In Kotlin, is there an easy way / shortcut to do this? [10:36 AM] Instead of doing if( array.size() > 0){ … } [10:36 AM] If not, you could probably make an extension function
s
well, depends on what you want to happen if size is 0
you can just call
.firstOrNull()
🙂 1
if you have a default to provide, supply it like this
Copy code
val foo = list.firstOrNull() ?: default
if you just want to conditionally do something with the first element if the list isn’t empty, you could do this
Copy code
list.firstOrNull()?.let {
  doSomethingWithValue(it)
}
but that’s not too different from just doing this
Copy code
if (list.isNotEmpty()) {
  doSomethingWithValue(list[0])
}
m
The problem I’m facing is I’m using a libraries interface to call .getEntryAtIndex(index) So if they don’t handle possible array index access error, I’d have to run it in a try / catch? / Throwable And just tested it, they dont’ handle for that exception.. So I need to handle it
s
that’s a pretty different question than you originally asked lol
m
Ha I know
Sorry lol
Didn’t realize what I was asking at first
s
would it be possible to give more context/a code snippet?
there’s a few different approaches that may or may not work for you
m
Copy code
lineData.dataSets.filter{ it.label == "Bolus"}?.let {
    for(index in it.indices){
        // TODO: Safe Array Index access
        val entry = it.get(index).getEntryForIndex(0)
        withinRangeOfBolus(entry, xCoordinate, yCoordinate, xRange, yRange)
    }
}
Copy code
val entry = it.get(index).getEntryForIndex(0)
I’m assuming every entry should have at least two indices…. But I’d like to be safe and handle an exception.. Or even better check before hand
But I do not see any functions in the library that allow me to check how many indices there are
s
if an entry doesn’t have at least two indices, what should happen? do you just want to not process it?
if that’s the case, you may just want to write a more specific filter
👍 1
does
withinRangeOfBolus()
just return
Unit
?
m
Well I really just need index = 0
I don’t really care about the other indices
does 
withinRangeOfBolus()
 just return 
Unit
? --> Boolean
s
let’s maybe take a step back and consider a few things - What data types are you working with here? - What does your intended result look like? - What properties of your data can you filter by?
it looks like each DataSet has a collection of Entries you want to access by index
do you care about the result of
withinRangeOfBolus()
? in your snippet you’re not assigning it to anything or returning it
m
This is what I came up
Copy code
lineData.dataSets.filter{ it.label == "Bolus"}?.let {
    for(index in it.indices){
        it.get(index).run {

            if(entryCount > 0) {
                val entry = this.getEntryForIndex(0)
                withinRangeOfBolus(entry, xCoordinate, yCoordinate, xRange, yRange)
            }
        }
    }
}
By chaining the .run{} I get access to .entryCount
s
if your goal is to filter through your datasets to find sets that contain entries within range, then something like this might be what you want:
Copy code
dataSets.filter {
  it.label == "Bolus"
      && it.entries.isNotEmpty()
      && withinRangeOfBolus(it.entries.first(), xCoordinate, yCoordinate, xRange, yRange) 
}
if you’re looking to filter for all entries that are within range, then this would be closer:
Copy code
val entryList: List<Entry> = dataSets.asSequence()
    .filter { 
      it.label == "Bolus" 
          && it.entries.isNotEmpty() 
          && withinRangeOfBolus(it.entries.first(), xCoordinate, yCoordinate, xRange, yRange
)
    }
    .map { it.entries }
    .flatten()
    .toList()
m
I’ll take a look at those filters
Thanks!
👍 1
Your filters are cool and I have a lot to learn about using them. But I’m only filtering my list of dataSets in order to perform withinRangeOfBolus(…) If it does not match my filter than DO NOT call withinRangeOfBolus(…)
Here’s some more context:
Copy code
//graphedBoluses = {ArrayList@14219}  size = 4
// 0 = {LineDataSet@14302} "DataSet, label: Bolus, entries: 2\nEntry, x: 6883.0 y: -40.0 Entry, x: 7483.0 y: -40.0 "
// 1 = {LineDataSet@14303} "DataSet, label: Bolus, entries: 3\nEntry, x: 9097.0 y: -34.0 Entry, x: 9697.0 y: -34.0 Entry, x: 10061.0 y: -34.0 "
// 2 = {LineDataSet@14304} "DataSet, label: Bolus, entries: 3\nEntry, x: 28093.0 y: -10.68 Entry, x: 28693.0 y: -10.68 Entry, x: 29196.0 y: -10.68 "
// 3 = {LineDataSet@14305} "DataSet, label: Bolus, entries: 2\nEntry, x: 85879.0 y: -27.32 Entry, x: 86479.0 y: -27.32 "


// EachLineDataSet has an mValues: ArrayList<Entry>..


//mValues = {ArrayList@14368}  size = 3
// 0 = {Entry@14375} "Entry, x: 28093.0 y: -10.68"
//  x = 28093.0
//  mData = {BolusDisplayModel@14381} "Entry, x: 28093.0 y: 8.33"
//  mIcon = null
//  y = -10.68
//  shadow$_klass_ = {Class@12944} "class com.github.mikephil.charting.data.Entry"
//  shadow$_monitor_ = 0
// 1 = {Entry@14376} "Entry, x: 28693.0 y: -10.68"
// 2 = {Entry@14377} "Entry, x: 29196.0 y: -10.68"

// 1. Filter the events by "Bolus"... This gives you the lineDataSets
// 2. Loop through the LineDataSet via index.
    // 2a. Grab the first entry... This will be the starting entry... Should check to make sure there is an Entry to be safe
    // 2b. To get the center point, we should add width & height
Sorry I can’t format better
s
maybe I’m misinterpreting you but since
&&
is short-circuiting,
withinRangeOfBolus
doesn’t get called if either conditions to its left evaluate to false
m
Aww didn’t know that… That’s awesome
👍 1
s
ah, in the future if you wanna upload a larger snippet, use the Slack snippets function via Shortcuts (lightning bolt icon) -> Create a code or text snippet
🙂 1
m
Thanks Man, I think I’m going to to stick with:
Untitled
The stuff you posted was helpful but a little beyond my paygrade… I’ll get there soon though
I wish there was a way to give you credit or Slack Points/Rep
s
lol no worries, just glad to help
m
Is there a way to friend people?
s
well Slack is really more geared towards organizations and companies where a “friend system” doesn’t really categorically make sense
m
But it’s like a better Stack Overflow for quick feedback
It reminds me of AIM + StackOverflow
s
that might be how we’re using it here, but that’s decidedly not the intended use case lol
m
Which is awesome
s
StackOverflow definitely still has its place, and I encourage you to keep using it
but here, best thing you can do really is to keep learning and help other folks when you can
m
Ha well if you want to join my channel you’re welcome
Yeah I want to start helping folks out
But I’m still relatively a beginner at Kotlin