Created
July 16, 2012 02:42
-
-
Save tototoshi/3120089 to your computer and use it in GitHub Desktop.
trouble with jgit & scala.collection.JavaConversions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import scala.collection.JavaConversions._ | |
scala> val git = new Git(new FileRepositoryBuilder().findGitDir(new java.io.File("/path/to/repos")).readEnvironment().build()) | |
git: org.eclipse.jgit.api.Git = org.eclipse.jgit.api.Git@18ed36fb | |
scala> git.log.addPath("foo").call.head | |
res49: org.eclipse.jgit.revwalk.RevCommit = commit c4554d9f46de49e449242a2aa75d39874dc99498 1342332998 ----sp | |
scala> git.log.addPath("foo").call.headOption | |
res50: Option[org.eclipse.jgit.revwalk.RevCommit] = Some(commit d3107ef998b25d70f86f1a5d530c31c02507d59e 1342012813 ----sp) | |
scala> git.log.addPath("foo").call.toList.head | |
res51: org.eclipse.jgit.revwalk.RevCommit = commit c4554d9f46de49e449242a2aa75d39874dc99498 1342332998 ----sp | |
scala> git.log.addPath("foo").call.toList.headOption | |
res52: Option[org.eclipse.jgit.revwalk.RevCommit] = Some(commit c4554d9f46de49e449242a2aa75d39874dc99498 1342332998 ----sp) | |
scala> git.log.addPath("foo").call.toList | |
res53: List[org.eclipse.jgit.revwalk.RevCommit] = List(commit c4554d9f46de49e449242a2aa75d39874dc99498 1342332998 ----sp, commit d3107ef998b25d70f86f1a5d530c31c02507d59e 1342012813 ----sp) | |
// headOption fetches second element?? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/scala/scala/blob/v2.9.2/src/library/scala/collection/JavaConversions.scala#L401
https://github.com/scala/scala/blob/v2.9.2/src/library/scala/collection/JavaConversions.scala#L587
JIterableWrapper
がつくられるhttps://github.com/scala/scala/blob/v2.9.2/src/library/scala/collection/TraversableLike.scala#L422
isEmpty
が呼ばれるhttps://github.com/scala/scala/blob/v2.9.2/src/library/scala/collection/IterableLike.scala#L81-82
https://github.com/scala/scala/blob/v2.9.2/src/library/scala/collection/IterableLike.scala#L63
isEmpty
のために新しいiteratorがつくられる(そのiteratorが先頭の要素保持しちゃう)https://github.com/scala/scala/blob/v2.9.2/src/library/scala/collection/TraversableLike.scala#L402-416
head
呼ぶhttps://github.com/scala/scala/blob/v2.9.2/src/library/scala/collection/IterableLike.scala#L72
head
の中で呼ばれるforeach
も新しいIterator生成するが、isEmpty
のときに生成した Iterator が先頭の要素を保持というか消費してて、二回目にここで生成したiteratorは、2番目の要素が先頭になってる