-
Notifications
You must be signed in to change notification settings - Fork 25
My Logger with currying #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
JavierCane
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
En este caso estaríamos haciendo uso de la encapsulación en funciones para delegar cómo tratar cada uno de los niveles de log.
No obstante, lo que se pide en el ejercicio es hacer esta misma encapsulación gracias al currying. Es decir, invocar parcialmente a la función definida aquí como log.
Algo tal que:
def log(level: String)(message: String): Unit = println(s"[$level] $message")
def debug(message: String): Unit = log(level = "DEBUG")(message)
debug("👋👋👋") // output: "[DEBUG] 👋👋👋"|
|
||
| def my_log(level: String, message: String): Unit = log("MISOLUCION")(level, message) | ||
|
|
||
| def debug = (message: String) => my_log("[DEBUG]", message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Posible alternativa a la hora de declarar esta misma función:
def debug(message: String): Unit = my_log("[DEBUG]", message)|
|
||
| def log(prefix: String)(level: String, value: String): Unit = println("[" + prefix + "]" + level + value) | ||
|
|
||
| def my_log(level: String, message: String): Unit = log("MISOLUCION")(level, message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
la convención a la hora de definir funciones y valores en Scala es camelCase
| @@ -0,0 +1,15 @@ | |||
| //Logger | |||
|
|
|||
| def log(prefix: String)(level: String, value: String): Unit = println("[" + prefix + "]" + level + value) | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gracias a especificar el "interpolador" s antes de las comillas podemos añadir variables a la hora de formar strings:
antes:
"[" + prefix + "]" + level + valuedespués:
s"[$prefix] $level $value"| @@ -0,0 +1,15 @@ | |||
| //Logger | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
te agradeceríamos que añadieras tu solución dentro de una nueva subcarpeta src/main/tv/codely/scala_intro_examples/lesson_06_anon_fn_currying/exercise_solutions/ y con nombre juan_miralles_logger.sc. De esta forma otros usuarios también pueden añadir sus propuestas, y podemos mergear la PR :)
Gracias!
Fixes based on PR comments
|
Changes done |
|
Thanks! :) |
Me faltaría transformarlo en una clase pero todavía no tengo claros algunos conceptos. 📚