Skip to content

Instantly share code, notes, and snippets.

@ryoco
Created August 8, 2012 13:32
Show Gist options
  • Save ryoco/3295054 to your computer and use it in GitHub Desktop.
Save ryoco/3295054 to your computer and use it in GitHub Desktop.
Scala rec test
scala> def selectTailRec(items : List[Int], removeIrems : List[Int], removeItem : Int, roop: int) : (List[Int], List[Int]) = {
| if(roop > 0) { val newList = items.filter(i => i != removeItem)
| selectTailRec(newList, removeItem :: removeIrems, newList.head, roop -1)}
| else (items, removeIrems) }
warning: there were deprecation warnings; re-run with -deprecation for details
selectTailRec: (List[Int],List[Int],Int,int)(List[Int], List[Int])
scala> selectTailRec(List(1,2,3,4,5,6,7,8), Nil, 7, 3)
res2: (List[Int], List[Int]) = (List(3, 4, 5, 6, 8),List(2, 1, 7))
scala> def selectTailRec(items : List[Int], item : Int, roop : Int) : List[Int] = {
| if(roop > 0) selectTailRec(items.filter(i => i != item), items.filter(i => i != item).head, roop-1)
| else items
| }
selectTailRec: (List[Int],Int,Int)List[Int]
scala> selectTailRec(List(1,2,3,4,5,6,7), 3, 3)
res0: List[Int] = List(4, 5, 6, 7)
scala> selectTailRec(List(1,2,3,4,5,6,7), 3, 5)
res1: List[Int] = List(6, 7)
@ryoco
Copy link
Author

ryoco commented Aug 8, 2012

scala> def selectTailRec(items : List[Int], removeIrems : List[Int], removeItem : Int, roop: int) : (List[Int], List[Int]) = {
| if(roop > 0) { val newList = items.filter(i => i != removeItem)
| selectTailRec(newList, removeItem :: removeIrems, newList.head, roop -1)}
| else (items, removeIrems) }
warning: there were deprecation warnings; re-run with -deprecation for details
selectTailRec: (List[Int],List[Int],Int,int)(List[Int], List[Int])

scala> selectTailRec(List(1,2,3,4,5,6,7,8), Nil, 7, 3)
res2: (List[Int], List[Int]) = (List(3, 4, 5, 6, 8),List(2, 1, 7))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment