REPLs

33
REPLs Antonio Ognio Cesti <[email protected]> http://gnrfan.org (Entornos interactivos de programación)

description

Una mirada a los entornos REPL (Read-Eval-Print-Loop) o consolas interactivas de programación.

Transcript of REPLs

Page 1: REPLs

REPLs

Antonio Ognio Cesti<[email protected]>

http://gnrfan.org

(Entornos interactivos de programación)

Page 2: REPLs

REPLs / Antonio Ognio

¿Qué es un REPL?

● Read-Eval-Print Loop● Entorno interactivo de programación● Escribo código y obtengo el resultado de inmediato● Ideal para probar ideas y estrategias● Manera rápida y fácil de obtener el fragmento de programación buscado

Page 3: REPLs

REPLs / Antonio Ognio

Mas sobre los REPLs● El nombre viene de las funciones que se utilizaron en su implementación en LISP● Muchas veces se le llama intérprete sin embargo esto no es exacto porque hay lenguages como Python o C# que si compilan el código fuente a bytecode y lo ejecutan el código de forma interactiva● Actualmente se encuentran disponibles para muchos lenguajes y cada vez hay más y nuevas implementaciones

Page 4: REPLs

REPLs / Antonio Ognio

Lenguajes con REPLs disponibles● LISP (top-level)● Python / Ipython● Java (Beanshell)● Ruby (IRB)● Haskell (ghci)● PHP (phpsh)● Javascript (Rhino js)● Scala (scala)● Erlang (erl)

● Perl (Perl Console)● F# (fsi)● Clojure● C (c-repl)● Ocaml (top-level)● C# (CSREPL)● Scheme (top-level)● Otros mas...

Page 5: REPLs

REPLs / Antonio Ognio

Cualquier linea de comandos...

● Bash / C Shell / Z Shell (UNIX)● command.com (DOS)● Microsoft Powershell (.Net)● Cualquier otro “intérprete” de comandos

Page 6: REPLs

REPLs / Antonio Ognio

Los REPLs nos acompañan hace mucho tiempo...

● Orígenes en LISP en los 60's y 70's● Integración con editor Emacs (SLIME)● Popular con Logo en los 80's

Page 7: REPLs

REPLs / Antonio Ognio

La tortuga de Logo...

Page 8: REPLs

REPLs / Antonio Ognio

Commodore 64 logo

Page 9: REPLs

REPLs / Antonio Ognio

Turtle Art en OLPC

Page 10: REPLs

REPLs / Antonio Ognio

F# Interactive en Visual Studio

Page 11: REPLs

REPLs / Antonio Ognio

Ventajas de un REPL● Ideal para dar los primeros pasos● Programación exploratoria● Depuración● Aprender a usar una API● Prototipear alguna idea● Afinar una expresión y/o algoritmo● Demostraciones en vivo! :)

Page 12: REPLs

REPLs / Antonio Ognio

Desventajas de un REPL● Los errores se notan de inmediato y no hay mucha oportunidad de corregirlos● ¿Esto último es un feature o un bug?● No es muy cómodo editar muchas líneas de código● No funcionan muchas características presentes en un IDE

Page 13: REPLs

REPLs / Antonio Ognio

Algunos REPLs disponibles en Ubuntu● c-repl● ipython● irb● erlang-base (erl)● slime● rhino● haskell-mode (ghci)● bsh (Bean Shell)

Page 14: REPLs

REPLs / Antonio Ognio

... a solo un apt-get install de distancia

$ sudo apt-get install <paquete>

Page 15: REPLs

REPLs / Antonio Ognio

Ejemplo: iPython

$ sudo apt-get install ipython

Page 16: REPLs

REPLs / Antonio Ognio

Demo de iPython

Page 17: REPLs

REPLs / Antonio Ognio

Demo de iPython

Page 18: REPLs

REPLs / Antonio Ognio

Características de iPython● “pretty-printing” de resultados● Autocompletar con Tab● Auto identación● Historial de comandos● Ayuda formateada● Traceback enriquecidos

●plain●context●verbose

Page 19: REPLs

REPLs / Antonio Ognio

Demo OnlinePython REPL

http://datamech.com/devan/trypython/trypython.py

Page 20: REPLs

REPLs / Antonio Ognio

http://tryruby.hobix.com/

Page 21: REPLs

REPLs / Antonio Ognio

http://www.phpsh.org

http://developers.facebook.com/opensource.php

Page 22: REPLs

REPLs / Antonio Ognio

[gnrfan]:~/src/phpsh-0.86b$./phpsh ejemplo.php phpsh (c)2006 by Charlie Cheever and Dan Corson and Facebook, Inc.type 'h' or 'help' to see instructions & featuresCommandline: php -q ./phpsh.php example.phpphp> = 3 + 36php> = $_6php> = $_+ $_12php> $x = $_php> print $x12php> $y = $x * $xphp> print "$y=144"144=144

Page 23: REPLs

REPLs / Antonio Ognio

php> = starts_withstarts_with starts_with($s, $prefix) php> = starts_with("abc", "a")1php> d starts_withfunction starts_with($s, $prefix) { type=None file=example.php/home/.dogmatic/ccheever/src/phpsh-0.86b/example.php, lines 2-10:/** * Returns true if the first arg starts with the second arg * @param string $big_string * @param string $little_string * @return true or false * * @author ccheever */php> = 2 + \24

Page 24: REPLs

REPLs / Antonio Ognio

php> = array(array(1,2,3), array("a" => "b", "c" => "d", "e" => "f"), 'g', 'h')Array( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 )

[1] => Array ( [a] => b [c] => d [e] => f )

[2] => g [3] => h)

php> q

Page 25: REPLs

REPLs / Antonio Ognio

http://www.mono-project.com/CsharpRepl

Page 26: REPLs

REPLs / Antonio Ognio

Page 27: REPLs

REPLs / Antonio Ognio

Page 28: REPLs

REPLs / Antonio Ognio

Page 29: REPLs

REPLs / Antonio Ognio

Page 30: REPLs

REPLs / Antonio Ognio

$ ./manage.py shell

Page 31: REPLs

REPLs / Antonio Ognio

$ ./manage.py shellPython 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2Type "help", "copyright", "credits" or "license" for more information.(InteractiveConsole)>>> from django.contrib.auth.models import User>>> user = User()>>> user.email = '[email protected]'>>> dir(user)>>> user.username = 'gnrfan'>>> user.first_name = 'Antonio'>>> user.last_name = 'Ognio'>>> user.save()

$ ./manage.py shellPython 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2Type "help", "copyright", "credits" or "license" for more information.(InteractiveConsole)>>> from django.contrib.auth.models import User>>> user = User()>>> user.email = '[email protected]'>>> dir(user)>>> user.username = 'gnrfan'>>> user.first_name = 'Antonio'>>> user.last_name = 'Ognio'>>> user.save()

Page 32: REPLs

REPLs / Antonio Ognio

Conclusiones● Los REPLs son fáciles de usar● Nos facilitan aprender programación● Nos permiten descubrir nuevas APIs● Nos ayudan a plasmar ideas en código● Existen para muchos lenguajes● Es posible que un REPL para tu lenguaje favorito este a la vuelta de la esquina

Page 33: REPLs

Gracias!!!

Antonio Ognio Cesti<[email protected]>

http://gnrfan.org