First element of

Hey guys. I’ve got that code.

The first question is: If ‘chunks’ is a sequence of strings? How would you define it?

The second question is: How can I get the first element of each string [one, two, three, four]? // o, t, t, f
Should I create listOf for each and then take the first element out of them?

Thank You for your help

I don’t really understand your question here. Maybe someone else can help you with this or you could try to explain again what you are asking for. Do you want to know what chunked does? It splits a long list into a list of shorter lists. So if you call someList.chunked(3) the first 3 elements will end up in the first list, the next 3 elements in a second list and so on.

"one two three four".split(' ').map { it[0] }

A few links that might help you:

2 Likes

Hey. I am asking about the val chunks. Is this variable a sequence or list etc?

"one two three four".split(' ').map { it[0] }

If I understand well it returns the first element which is ‘o’, however, I am looking for something that will allow me to take the first letter of each word, not only the first element. Is it any more accurate?

Thanks

chunked<T> returns a List<List<T>> so it returns a list of lists.

My code returns the a list with the first letter of each word.

2 Likes

thats great. thanks!

1 Like

If you hover your mouse cursor over chunks, your IDE should tell you.

1 Like

one more question to You. if that’s a list of list, then how can I access perhaps second letter of the second word?

I don’t think you understand chunked. It does not modify the elements of the original list, it just groups them.

listOf(1, 2, 3, 4, 5).chunked(2)
// == listOf(listOf(1, 2), listOf(3, 4), listOf(5))

It looks like you are new to programming or at least new to functional programming. You should take a look at Kotlin Playground: Edit, Run, Share Kotlin Code Online and especially the part about collections Kotlin Playground: Edit, Run, Share Kotlin Code Online

If that doesn’t help, maybe try to explain what you are trying to create in a bit more detail. I could endlessly show you code examples of how to solve a specific subproblem, but unless you fully understand why any of that code works it wont help you solve your problem.

1 Like

Thanks for your help Wasabi. I’m fresh to this. I’m doing play.kotlin but many things are not explained there, some things are just used without absolute explanation. So what I’m doing is I am going through Kotlin documentation step by step plus play.kotlin.

I am not really trying to create anything. My mind throws me some questions that I would like to know the answers for. Kotlin documentation is rich but it’s easy not stay on track if you fresh.

Okay:
println(listOf[0]) // [1,2] - is that correct? That is going to be the outcome right? I suppose it doesn’t make any sense now to access just ‘2’ once we grouped them but I wonder if its possible after .chunked

1 Like

println(listOf[0]) // [1,2] isn’t a correct syntax, I imagine you’re refering to the result of .chunked in that case:

val chunks = listOf(1, 2, 3, 4, 5).chunked(2)
println(chunks[0])  // [1,2]

If you want the 2nd element of the first chunk you can do
println(chunks[0][1]) // [2]
there’s nothing special about that, you have a list of lists and you’re getting the 2nd element of the 1st list.

2 Likes