Skip to content

Instantly share code, notes, and snippets.

@chochos chochos/lcd.ceylon
Created Mar 22, 2012

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Mar 22, 2012

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
You can’t perform that action at this time.