Python to LiveScript
@hychen
for PyCon APAC 2014
Star	 to	 learn	 
LiveScript	 	 
after	 join	 a	 
g0v.tw,	 a	 open	 
civic	 hacking	 
community.
劣即
是夯JavaScript everywhere
LiveScript
• concise syntax.
• say bye to bad parts of JavaScript.
• many improvements for OOP.
• many features of FP.
• h...
JS
LS
use -> to replace anonymous function
LS
LS
use bound call to remove `that = this`
LS
LS
@ = this
LS
LS
func! === func()
LS
LS
‘foo’ === foo
LS
LS
use back call
LS
LS
use bound back call
JS
LS
Package Manager
you can use any JavaScript modules in LiveScript.
NPM
PyPi
Import
Import os
!
require! os
PY
LS
PY
LS
from os import path
!
path = require os .path
REPL
user@host: python
Python 2.7.6 (default, Nov 17 2013, 16:03:54)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)...
Inspect attributes of an
object
Object.keys
dir
>>> dir(path)
['__all__', '__builtins__', '__doc__', '__file__', '__name__'...
Help
PY
LS X
pydoc
Object Model
Please check the slide “Python New-Style Objec
v.s. JavaScript prototype, COSCUP 2012”
The basic object model...
List construction
[1, 2, [3, 4], [5,6]]PY & LS
1 spaceLS
List Property Access
[1, 2, [3,4]][0]PY & LS
LS
LS
[1, 2, [3,4]].0
[1, 2, [3,4]].2.0
Dict construction
PY & LS
LS
quotes of the keywords are optional
use object to simulate Python dict in LiveScript.
Dict construction(cont.)
PY & LS
LS
another better represent way
use object to simulate Python dict in LiveScript.
Dict Property Access
{‘a’:1, ‘b’}[‘a’]PY & LS
LS {‘a’:1, ‘b’}.a
unpacking function
arguments
LS
PY
unpacking function keyword
argument
LS
PY
packing function arguments
PY
LS
packing function keyword
arguments
PY
LS
destructing assignment
PY & LS
LS
destructing assignment
(cont.)
LS
Python does not support.
destructing function
arguments
LS
Python does not support.
List Comprehension
PY
LS
result: [‘a1', 'a2', 'b1', 'b2']
Dict Comprehension
PY
LS
result: {‘a': 2, 'b': 4}
File Reading
⼤大家
!
好
!
我是
!
[Name]
file: memo.txt
PY
File Reading(cont.)
callback 2
callback 1
LS
Backcall
callback1
callback2
LS
Backcall(cont.)
LS
LS
Class
PY
Class(cont.)
LS
… means arguments of current function
Python Super
• super(type, obj) -> bound super object; requires
isinstance(obj, type)
LiveScript Super
• is a reference to the appropriate function.
• If you want to call it with all arguments, use super
…
• ...
LiveScript Mixing
You can extend only 1 parent but can implement many interface.
Many more…
LS Curried Function
Many more…
LS
Conclusion
• change mind to code with callbacks.
• functional style programming with prelude.ls
• use class rarely after u...
Questions?
Python To LiveScript @PythonCon APAC 2014
Python To LiveScript @PythonCon APAC 2014
Upcoming SlideShare
Loading in …5
×

Python To LiveScript @PythonCon APAC 2014

1,254 views

Published on

introduction of LiveScript for Python programmer.

Published in: Technology, Sports

Python To LiveScript @PythonCon APAC 2014

  1. 1. Python to LiveScript @hychen for PyCon APAC 2014
  2. 2. Star to learn LiveScript after join a g0v.tw, a open civic hacking community.
  3. 3. 劣即 是夯JavaScript everywhere
  4. 4. LiveScript • concise syntax. • say bye to bad parts of JavaScript. • many improvements for OOP. • many features of FP. • http://livescript.net/ • go there and try compiling
  5. 5. JS LS use -> to replace anonymous function
  6. 6. LS LS use bound call to remove `that = this`
  7. 7. LS LS @ = this
  8. 8. LS LS func! === func()
  9. 9. LS LS ‘foo’ === foo
  10. 10. LS LS use back call
  11. 11. LS LS use bound back call
  12. 12. JS LS
  13. 13. Package Manager you can use any JavaScript modules in LiveScript. NPM PyPi
  14. 14. Import Import os ! require! os PY LS PY LS from os import path ! path = require os .path
  15. 15. REPL user@host: python Python 2.7.6 (default, Nov 17 2013, 16:03:54) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> user@host: lsc LiveScript 1.2.0 - use 'lsc --help' for more information ls> Python LiveScript
  16. 16. Inspect attributes of an object Object.keys dir >>> dir(path) ['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_joinrealpath', '_unicode', '_varprog', 'abspath', 'altsep', 'basename', 'commonprefix', 'curdir', 'defpath', 'devnull', 'dirname', 'exists', 'expanduser', 'expandvars', 'extsep', 'genericpath', 'getatime', 'getctime', 'getmtime', 'getsize', 'isabs', 'isdir', 'isfile', 'islink', ‘ismount', …. ls> Object.keys path [ 'resolve', 'normalize', 'join','relative','sep','delimiter', 'dirname', 'basename','extname','exists', 'existsSync', '_makeLong' ]
  17. 17. Help PY LS X pydoc
  18. 18. Object Model Please check the slide “Python New-Style Objec v.s. JavaScript prototype, COSCUP 2012” The basic object model of LiveScript should be the same as JavaScript.
  19. 19. List construction [1, 2, [3, 4], [5,6]]PY & LS 1 spaceLS
  20. 20. List Property Access [1, 2, [3,4]][0]PY & LS LS LS [1, 2, [3,4]].0 [1, 2, [3,4]].2.0
  21. 21. Dict construction PY & LS LS quotes of the keywords are optional use object to simulate Python dict in LiveScript.
  22. 22. Dict construction(cont.) PY & LS LS another better represent way use object to simulate Python dict in LiveScript.
  23. 23. Dict Property Access {‘a’:1, ‘b’}[‘a’]PY & LS LS {‘a’:1, ‘b’}.a
  24. 24. unpacking function arguments LS PY
  25. 25. unpacking function keyword argument LS PY
  26. 26. packing function arguments PY LS
  27. 27. packing function keyword arguments PY LS
  28. 28. destructing assignment PY & LS LS
  29. 29. destructing assignment (cont.) LS Python does not support.
  30. 30. destructing function arguments LS Python does not support.
  31. 31. List Comprehension PY LS result: [‘a1', 'a2', 'b1', 'b2']
  32. 32. Dict Comprehension PY LS result: {‘a': 2, 'b': 4}
  33. 33. File Reading ⼤大家 ! 好 ! 我是 ! [Name] file: memo.txt PY
  34. 34. File Reading(cont.) callback 2 callback 1 LS
  35. 35. Backcall callback1 callback2 LS
  36. 36. Backcall(cont.) LS LS
  37. 37. Class PY
  38. 38. Class(cont.) LS … means arguments of current function
  39. 39. Python Super • super(type, obj) -> bound super object; requires isinstance(obj, type)
  40. 40. LiveScript Super • is a reference to the appropriate function. • If you want to call it with all arguments, use super … • … implies calling it with the arguments of the current function.
  41. 41. LiveScript Mixing You can extend only 1 parent but can implement many interface.
  42. 42. Many more… LS Curried Function
  43. 43. Many more… LS
  44. 44. Conclusion • change mind to code with callbacks. • functional style programming with prelude.ls • use class rarely after use LiveScript. • LiveScript does not have debugger now, testing is highly important! • You may need to check compiled JavaScript even your LiveScript code are correctly rarely.
  45. 45. Questions?

×