Skip to content

Instantly share code, notes, and snippets.

@chochos
Created March 22, 2012 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chochos/2156739 to your computer and use it in GitHub Desktop.
Save chochos/2156739 to your computer and use it in GitHub Desktop.
LCD in Ceylon
class Digito(String partes) {
shared String parte(Integer idx) {
if (exists p=partes[idx]) {
if (p==`_`) {
return " _ ";
} else if (p==` `) {
return " ";
} else {
return "| ";
}
}
return "???";
}
shared Boolean vacio(Integer idx) {
if (exists p=partes[idx]) {
return p==` `;
}
return true;
}
}
Sequence<Digito> digitos = {
Digito("_|| ||_"), //0
Digito(" | | "), //1
Digito("_ |_| _"), //2
Digito("_ |_ |_"), //3
Digito(" ||_ | "), //4
Digito("_| _ |_"), //5
Digito("_| _||_"), //6
Digito("_ | | "), //7
Digito("_||_||_"), //8
Digito("_||_ |_") //9
};
String dig2string(Integer x) {
value digb=SequenceBuilder<Digito>();
for (c in x.string) {
if (exists d=digitos[c.integer-48]) {
digb.append(d);
}
}
value digs=digb.sequence;
StringBuilder sb = StringBuilder();
void pl(Integer idx, void line(Integer idx, Digito d), Boolean lastNewline) {
for (d in digs) {
line(idx, d);
}
if (lastNewline) { sb.append("\n"); }
}
void horiz(Integer idx, Digito d) {
sb.append(d.parte(idx)).append(d.vacio(idx) then " " else " ");
}
void vert(Integer idx, Digito d) {
sb.append(d.parte(idx)).append(d.parte(idx+1));
}
pl(0, horiz, true);
pl(1, vert, true);
pl(3, horiz, true);
pl(4, vert, true);
pl(6, horiz, false);
return sb.string;
}
void run() {
print(dig2string(12345));
print(dig2string(67890));
}
@oscarryz
Copy link

Ahh que bonito...este si es tratar a las funciones como ciudadanos de primer nivel, dandoles su propio tipo de dato y no hacerlos pasar por callables :)

  void pl(Integer idx, void line(Integer idx, Digito d), /* <-- este */ Boolean lastNewline)

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