Office Hours — Today, June 18

Saturday, June 15

Jun 18
8:55 AM
Mark M.
has entered the room
Mark M.
turned on guest access
9:25 AM
ndrocchietto_ivano
has entered the room
ndrocchietto_ivano
Hello
Mark M.
hello, Ivano!
ndrocchietto_ivano
Mark
Mark M.
how can I help you today?
ndrocchietto_ivano
:( still juniorish
I initially had an array that need to compare to another one
View paste
private fun notInExistingList(behaviorRelay: BehaviorRelay<List<Client>>, client: Client) =
            
            !behaviorRelay.value.contains(client)
then since I gotta do a POST request and some endpoint response is not clear, I have to compare the id of one list with the one of another one to find the match in my Adapter
the problem is that this method should return a boolean
instead if I write the following snippet I get back a List
View paste
private fun notInExistingList(behaviorRelay: BehaviorRelay<List<Client>>, client: Client) =
            
            !behaviorRelay.value.contains(client)
9:30 AM
ndrocchietto_ivano
sorry wrong
View paste
  viewModel.selectedClients.value.forEach {

                    if (it.id == client.id) markClientAtPosition(true)

                }
how can I made a control between two id arrays, having back a true
instead of a List?
Mark M.
selectedClients.value is an Array<Client>?
ndrocchietto_ivano
is a BehaviourRelay a library from JW
so is a subject
but yes with value gives back a List<Client>
`value`
Mark M.
you have been saying "array", but you have been coding List -- in Kotlin, Array and List are different things
ndrocchietto_ivano
I am sorry I mean a List
Mark M.
OK
so, you want true if both List<Client> have the same length and all members have the same value for id
?
ndrocchietto_ivano
Absolutely
cannot work this out, nor find on SO some keyword
because is more a data structure skill which I lack still
Mark M.
hold on
ndrocchietto_ivano
sure thing
9:35 AM
ndrocchietto_ivano
thank you for your interest, and sorry for my abusing curiosity
9:35 AM
Mark M.
does order matter? List is ordered, so do the IDs have to appear in the same order?
ndrocchietto_ivano
no
9:40 AM
Mark M.
ah, too bad, because equals() should work if order was important: https://kotlinlang.org/api/latest/jvm/stdlib/ko...
ndrocchietto_ivano
View paste
order is not important because I have to use a forEach that goes to iterate trough, and the onBindViewHolder is going to order for me, because client is an `            val client = itemsList[position]
`
well order is good as well
if they get sorted is not a problem guess
Mark M.
actually, equals() would only work if Client had an equals() that compared id values
ndrocchietto_ivano
mmh
Mark M.
View paste
something like this should be close:

fun clientListsEqual(list1: List<Client>, list2: List<Client>) =
  list1.size == list2.size && list1.map { it.id }.containsAll { list2.map { it.id } }
ndrocchietto_ivano
I am going to look into
Mark M.
View paste
sorry, that should be:

fun clientListsEqual(list1: List<Client>, list2: List<Client>) =
  list1.size == list2.size && list1.map { it.id }.containsAll(list2.map { it.id })
View paste
data class Client(val id: String)

fun clientListsEqual(list1: List<Client>, list2: List<Client>) =
  list1.size == list2.size && list1.map { it.id }.containsAll(list2.map { it.id })

println(clientListsEqual(listOf(Client("1"), Client("X"), Client("foo")), listOf(Client("1"), Client("X"), Client("foo"))))
println(clientListsEqual(listOf(Client("1adfasdfasd"), Client("X"), Client("foo")), listOf(Client("1"), Client("X"), Client("foo"))))

prints:

true
false
ndrocchietto_ivano
thank you very much
I am gonna implemtn
implement
9:50 AM
Mark M.
actually, equals() on the mapped lists is simpler
View paste
data class Client(val id: String)

fun clientListsEqual(list1: List<Client>, list2: List<Client>) =
  list1.map { it.id }.equals(list2.map { it.id })

println(clientListsEqual(listOf(Client("1"), Client("X"), Client("foo")), listOf(Client("1"), Client("X"), Client("foo"))))
println(clientListsEqual(listOf(Client("1adfasdfasd"), Client("X"), Client("foo")), listOf(Client("1"), Client("X"), Client("foo"))))
println(clientListsEqual(listOf(Client("1"), Client("X"), Client("foo")), listOf(Client("1"), Client("X"), Client("foo"), Client("foo"))))
View paste
prints:
true
false
false
9:55 AM
ndrocchietto_ivano
thank you Mark
Looks as a winner
quick win
I can improve these skills studying algorithms and data structure?
Mark M.
um, possibly
ndrocchietto_ivano
thank you, have a pleasant morning
Mark M.
you're welcome!
10:00 AM
Mark M.
that's a wrap for today's chat
the next chat is tomorrow at 4pm US Eastern
this transcript will be posted to https://commonsware.com/office-hours/ shortly
ndrocchietto_ivano
has left the room
Mark M.
turned off guest access

Saturday, June 15

 

Office Hours

People in this transcript

  • Mark Murphy
  • ndrocchietto_ivano