Created
February 21, 2013 21:37
-
-
Save sam/5008489 to your computer and use it in GitHub Desktop.
Trying to get a basic Nested Set structure defined using not terrible code.
This file contains hidden or 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
| package models | |
| case class Channel(id:Int, title:String, hidden:Boolean, left:Int, children:Seq[Channel]) { | |
| def right = 2 | |
| def ++(children:Seq[Channel]) = { | |
| copy(children = children.scanLeft(this) { (previous, channel) => | |
| channel.copy(left = previous.right + 1) | |
| }.tail) | |
| } | |
| } | |
| object Channel { | |
| object root extends Channel(0, "root", true, 1, Nil) | |
| } | |
| // Channel.root ++ Seq(Channel(1, "child1", false, 0, Nil), Channel(2, "child2", false, 27, Nil), Channel(3, "child3", false, 10, Nil)) | |
| // Almost there, but the Channel++ needs to pass in the left of the first argument, and the right for every other. | |
| // So a scanLeft that had *two* accumulators? One of them a List to map to, and the other a cumulative variable to express? | |
| // Maybe I'm forced to use a foldLeft[Tuple2] instead? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment