Passionate about building great technology and connecting with people to create positive change. Happy to answer questions about transitioning to tech. Find me on Twitter @lounecl
intersect is an extension function of Collections provided by Kotlin's stdlib, it would work on Lists, Sets, Maps (popularly implemented as ArrayList, HashSet and HashMap)
So the types passed to it must be S and T, and it must return of type S as well. Some people may choose to explicitly declare the type when type inference comes from a diffferent file.
Like you said, Kotlin is so clean! I love it. The perfect blend of concise and safe : )
If you're interested, a great way to learn is to automatically convert a Java file to Kotlin and refer to kotlinlang.org/docs/kotlin-docs.pdf whenever you're editing the file anyway, and have fun optimizing and continuing development without much downtime or needing to start again.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Cool article! I think Reduce is especially handy in constructing more human-friendly functions.
Kotlin:
My first time reading any Kotlin code, but it's pretty clean. How would this block of code look different with
reduce
? Isintersect
a native method?intersect
is an extension function of Collections provided by Kotlin's stdlib, it would work on Lists, Sets, Maps (popularly implemented as ArrayList, HashSet and HashMap)See: kotlinlang.org/api/latest/jvm/stdl...
My code could have been clearer by writing "arrayListOf" actually
Here is the code using reduce:
The types of acc and arr don't need to be declared, because lists is of the type List, and reduce uses type parameters in its declaration:
So the types passed to it must be S and T, and it must return of type S as well. Some people may choose to explicitly declare the type when type inference comes from a diffferent file.
Like you said, Kotlin is so clean! I love it. The perfect blend of concise and safe : )
If you're interested, a great way to learn is to automatically convert a Java file to Kotlin and refer to kotlinlang.org/docs/kotlin-docs.pdf whenever you're editing the file anyway, and have fun optimizing and continuing development without much downtime or needing to start again.