Skip to content

Instantly share code, notes, and snippets.

@soc
Last active December 18, 2015 05:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soc/5737097 to your computer and use it in GitHub Desktop.
Save soc/5737097 to your computer and use it in GitHub Desktop.
final case class ::[B](private var hd: B, private[scala] var tl: List[B]) extends List[B]
override def head: B = hd
override def tail: List[B] = tl
override def isEmpty: Boolean = false
private def readObject(in: ObjectInputStream): Unit =
val firstObject = in.readObject()
hd = firstObject.asInstanceOf[B]
assert(hd != ListSerializeEnd)
var current: ::[B] = this
while true
do in.readObject match
case ListSerializeEnd =>
current.tl = Nil
return
case a =>
val list: ::[B] = new ::(a.asInstanceOf[B], Nil)
current.tl = list
current = list
private def writeObject(out: ObjectOutputStream): Unit =
var xs: List[B] = this
while !xs.isEmpty
do out.writeObject(xs.head); xs = xs.tail
out.writeObject(ListSerializeEnd)
override def slice(from: Int, until: Int): List[A] =
val lo = scala.math.max(from, 0)
if until <= lo || isEmpty
then Nil
else this drop lo take (until - lo)
@folone
Copy link

folone commented Jul 3, 2013

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