Skip to content

Instantly share code, notes, and snippets.

@hsk
Created June 13, 2010 12:08
Show Gist options
  • Save hsk/436609 to your computer and use it in GitHub Desktop.
Save hsk/436609 to your computer and use it in GitHub Desktop.
def classify(xts:List[('a,Type.T)], ini:'b, addf:('b,'a)=>'b, addi:('b,'a,Type.T)=>'b):'b = {
xts.foldLeft(ini) {
case (acc, (x, t)) => t match {
case Type.Unit() => acc
case Type.Float() => addf(acc, x)
case _ => addi(acc, x, t)
}
}
}
def separate(xts:List[(Id.T, Type.T)]):(List[Id.T],List[Id.T]) = {
classify(
xts,
(List(), List()),
((int1, float1), x) => (int1, float1 ::: List(x))),
((int1, float1), x, _) => (int1 ::: List(x), float1))
)
}
def expand(
xts:List[(Id.T, Type.T)],
ini:(Int,T),
addf:(Id.T, Int, (Id.T,Type.T))=>T, addi:(Id.T, Int, T,(Id.T,Type.T))=>T):(Int,T) = {
classify(
xts,
ini,
{case ((offset, acc), x) =>
val offset2 = align(offset);
(offset2 + 8, addf(x, offset2, acc))
},
{case ((offset, acc), x, t) => (offset + 4, addi(x, t, offset, acc))}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment