title: Filter an array of objects by distance Kotlin
body: <p>Im new to Kotlin and trying to convert some Swift code to Kotlin.  </p>
  
  <p>Here is my swift function.  It filters out array objects that are over a specific distance from the user. </p>
  
  <pre><code>func filterByDistance(_ events:[Event]) -&gt; [Event] {
      let filteredEvents = events.filter { event -&gt; Bool in
          if let lat = event.venue?.location?.latitude,
              let long = event.venue?.location?.longitude,
              let userLocation = UserLocation.shared.location {
              let eventLocation = CLLocation(latitude: lat, longitude: long)
              let distance = eventLocation.distance(from: userLocation)
              let convertedDistance = distance * 0.000621371
              if convertedDistance &lt;= maxDistance {
                  return true
              }
          }
          return false
      }
      return filteredEvents
  }
  </code></pre>
  
  <p>Below is what I've got so far using Kotlin</p>
  
  <pre><code>fun LatLng.toLocation() = Location(LocationManager.GPS_PROVIDER).also {
          it.latitude = latitude
          it.longitude = longitude
      }
  
  fun filterByDistance(events: Array&lt;Events&gt;): Array&lt;Events&gt; {
      val filteredEvents = events.filter&lt;Events&gt; { event -&gt;
          val lat = event.venue?.location?.latitude
          val long = event.venue?.location?.longitude
          val userLocation = LatLng(latitude, longitude)
          val eventLocation = LatLng(lat, long)
          val distance = eventLocation.toLocation().distanceTo(userLocation.toLocation())
          val convertedDistance = distance * 0.000621371
          if (convertedDistance &lt;= 500) {
              return  true
          } else {
              return false
          }
      }
      return filterEvents(events)
  }
  </code></pre>
  
  <p>Im getting an error asking me to change my return type to a Bool,  but I need to return an array of filtered events.  Can someone help me out?  </p>
  
  <p>EDIT:  Thanks to JB Nizet I was able to get this working.  I had to change the object from Array to List.  Here is the working code.</p>
  
  <pre><code>fun fetchJson() {
          val url = "URL String"
          val request = Request.Builder().url(url).build()
          val client = OkHttpClient()
          client.newCall(request).enqueue(object:Callback{
              override fun onResponse(call: Call?, response: Response?) {
                  val body = response?.body()?.string()
                  val gson = GsonBuilder().create()
                  val eventss = gson.fromJson(body, Array&lt;Events&gt;::class.java)
                  val events = eventss.toList()
                  val filteredEvents = filterByDistance(events)
                  runOnUiThread {
                      recyclerView.adapter = MainAdaptor(filteredEvents)
                  }
              }
              override fun onFailure(call: Call?, e: IOException?) {
                  println("failed")
              }
          })
      }
  
  
  
  fun LatLng.toLocation() = Location(LocationManager.GPS_PROVIDER).also {
          it.latitude = latitude
          it.longitude = longitude
      }
  
  fun filterByDistance(events: List&lt;Events&gt;): List&lt;Events&gt; {
        val filteredEvents = events.filter { event -&gt;
              val lat = event.venue?.location?.latitude
              val long = event.venue?.location?.longitude
              val userLocation = LatLng(latitude, longitude)
              val eventLocation = LatLng(lat, long)
              val distance = eventLocation.toLocation().distanceTo(userLocation.toLocation())
              val convertedDistance = distance * 0.000621371
              convertedDistance &lt;= maxDistance
          }
          return filteredEvents
    }
  </code></pre>
  
  <p>And the Class if it helps anyone:</p>
  
  <pre><code>class Events (val type: String,
                val venue: Venue,
                val time: String,
                val name: String,
                val summary: String,
                val activity: String,
                val image_link: String,
                val membership_link: String,
                val description: String
                )
  
  class Venue(val type: String,
              val name: String,
              val address: String,
              val location:Location
  )
  
  class Location(val type: String,
                 val latitude: Double,
                 val longitude: Double)
  </code></pre>