Extension function .each on Collection<T>

My first extension function and my first function literal:

https://gist.github.com/2019572

``

package litfunc

import java.util.ArrayList
import java.util.Collection

fun main(args : Array<String>) : Unit {
  val l = ArrayList<String>()
  l.add(“one”)
  l.add(“two”)
  l.add(“buckle my shoe”)
  l.each { it ->
  println(it)
  }
  /*
  Output is:
  one
  two
  buckle my shoe
  */
}

fun <T> Collection<T>.each(fn : (T) -> Unit) {
  for( item in this) {
  fn(item)
  }
}


Suggestions on how I can improve this welcome.

Mark

Looks good - I guess supporting variance should be added too (taking "in T").

BTW here’s the current extension functions available on Collection<T> - we’ve got a method we called foreach (rather than each) as it seemed a little more descriptive.

Please rename it "forEach", let's stop carrying this broken legacy around :-)


Cédric

Good point :)

jstrachan wrote:

Looks good - I guess supporting variance should be added too (taking “in T”).

BTW here’s the current extension functions available on Collection<T> - we’ve got a method we called foreach (rather than each) as it seemed a little more descriptive.


Nice.  I did not know these extensions existed.

Done (you might need to hit refresh in your browser) to see the updated extension methods on Collection<T>

Awesome, thanks James!

I can try to work on the "in T" annotation, but I'm so new at this stuff that someone will have to check my work.  It may not be worth me even attempting for that reason.

But… if using the “in T” form is the right thing to do, then maybe get this into the build before people potentially start using unsupported use-site forms.  Or something like that.

Agreed. BTW we love contributions if you fancy a challenge! There's a pending issue to double check all the extension functions are using the right in/out types. http://youtrack.jetbrains.com/issue/KT-1409

To do this we really need more test cases that use the extension functions with collections of different kinds of objects (e.g. collections of People and Managers) to see where we’ve missed some in/out type parameters.
e.g. like these tests:  https://github.com/JetBrains/kotlin/blob/master/libraries/testlib/test/CollectionTest.kt

Doing this will help us all learn about Kotlin, generics & variance too - and help firm up some missing areas in the standard library.

If you fancy helping, please fork kotlin on github, try add new tests to spot missing in/out type parameters & when you hit an issue, try figuring out a fix and then sending us a pull request? Feel free to ask here if things are not clear!

This should probably go in a new thread, but I'll start here:

Is there any documentation for contributors on how to setup your development environment?  I performed the two Ant steps indicated in README.md, then opened the Kotlin top level project (as-directory, hoping it would do the right thing).  The kotlin runtime was found in lib/kotlin-runtime.jar, and the IDE plugin added it.

Attached is a screenshot of an open file, with red all over it.



kotide.png (165 KB)

I had the exact same problem with one more symptom: the top of the editor was showing the yellow flash "The Kotlin runtime is not installed, click here to fix this" but clicking the link produced absolutely no result.

I ended up creating a brand new project and things seem to work there.

Still no luck.

I’m using EAP 11.1 116.32.

I clean the existing project files thusly, from the top level directory:

``

flist=$(find . -name *.iws -o -name *.ipr -o -name *.iml -o -name .idea)
for i in $flist; do
  rm -rf $i
done

Then

ant -f update_dependencies.xml
ant -f build.xml

Then I create a new project from existing sources, and point at the top level directory.  The IDE finds a bunch of source trees and libs, then opens the workspace.  I open a .kt file, such as /TemplateCoreTest.kt, but after a few seconds of indexing, I still get red all over the editor.



tct.png (197 KB)

The IDEA project is already in the repository, so, you can use it instead of creation of the new one. Additionally, Kotlin plugin should be deleted before working with Kotlin project in IDEA.

So its a little fiddly working on the kotlin project; as its kinda 2 projects; the compiler & IDEA plugin - then a bunch of libraries written in kotlin.

I’ve just updated the ReadMe.md to try be a bit more clear; typically you either want to:

  • work on the compiler/IDE plugin - in which case you work on the root project in an IDEA with no Kotlin plugin installed
  • work on the kotlin libraries - in which case you either use the above to run a  new IDEA with the Kotlin plugin - or install the Kotlin plugin and open the libraries project

I hope that helps! Am sure we could improve these instructions further though :)