LCD in Ceylon
This file contains 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
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)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :)