Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Open Source Software and Culture -- Crystal

Open Source Software and Culture -- Crystal

CS5470 Open Source Software and Culture in NTHU, Taiwan.

David Kuo (Davy)

April 21, 2016
Tweet

More Decks by David Kuo (Davy)

Other Decks in Education

Transcript

  1. 3VCZ š 5IPSPVHIMZPCKFDUPSJFOUFEXJUIJOIFSJUBODF NJYJOT BOENFUBDMBTTFT š %ZOBNJDUZQJOHBOEEVDLUZQJOH š &WFSZUIJOHJTBOFYQSFTTJPO FWFOTUBUFNFOUT

    BOEFWFSZUIJOHJTFYFDVUFEJNQFSBUJWFMZ FWFO EFDMBSBUJPOT  š %ZOBNJDSFGMFDUJPOBOEBMUFSBUJPOPGPCKFDUTUPGBDJMJUBUFNFUBQSPHSBNNJOH š -FYJDBMDMPTVSFT JUFSBUPSTBOEHFOFSBUPST XJUIBVOJRVFCMPDLTZOUBY š (BSCBHFDPMMFDUJPO š $VTUPNEJTQBUDICFIBWJPS UISPVHImethod_missing BOEconst_missing  š 4USJDUCPPMFBO DPFSDJPOSVMFT FWFSZUIJOHJTUSVFFYDFQUfalse BOEnil 
  2. 3VCZ say = "I love Ruby" puts say #=> I

    love Ruby say['love'] = "*love*" puts say.upcase #=> I *LOVE* RUBY # "I *love* Ruby" in five times 5.times { puts say }
  3. (SBEVBM5ZQJOH def add1(x): return x + 1 class A(object): pass

    a = A() add1(a) IUUQXQIPNFTTPJDJOEJBOBFEVKTJFLXIBUJTHSBEVBMUZQJOH
  4. def add1(x): return x + 1 class A(object): pass a

    = A() add1(a) # TypeError: unsupported operand type(s) for +: 'A' and ‘int' # (Interpreter) (SBEVBM5ZQJOH IUUQXQIPNFTTPJDJOEJBOBFEVKTJFLXIBUJTHSBEVBMUZQJOH
  5. (SBEVBM5ZQJOH class A { int add1(int x) { return x

    + 1; } public static void main(String args[]) { A a = new A(); add1(a); } } IUUQXQIPNFTTPJDJOEJBOBFEVKTJFLXIBUJTHSBEVBMUZQJOH
  6. (SBEVBM5ZQJOH class A { int add1(int x) { return x

    + 1; } public static void main(String args[]) { A a = new A(); add1(a); } } IUUQXQIPNFTTPJDJOEJBOBFEVKTJFLXIBUJTHSBEVBMUZQJOH # add1(int) in A cannot be applied to (A) add1(a); # (Compiler)
  7. (SBEVBM5ZQJOH class A { int add1(int x) { return x

    + 1; } public static void main(String args[]) { A a = new A(); if (false) add1(a); } } IUUQXQIPNFTTPJDJOEJBOBFEVKTJFLXIBUJTHSBEVBMUZQJOH
  8. (SBEVBM5ZQJOH class A { int add1(int x) { return x

    + 1; } public static void main(String args[]) { A a = new A(); if (false) add1(a); } } IUUQXQIPNFTTPJDJOEJBOBFEVKTJFLXIBUJTHSBEVBMUZQJOH # add1(int) in A cannot be applied to (A) add1(a);
  9. (SBEVBM5ZQJOH ü %ZOBNJDUZQFDIFDLJOHNBLFTJUFBTZUPEFBMXJUITJUVBUJPOTXIFSFUIFUZQFPGBWBMVF EFQFOETPOSVOUJNFJOGPSNBUJPO ü 4UBUJDUZQJOHJNQSPWFTNPEVMBSJUZ ü 'PSFYBNQMF JOBEZOBNJDMBOHVBHF ZPVDBODBMMBMJCSBSZTVCSPVUJOFJODPSSFDUMZCVUUIFOHFUBUZQFFSSPS

    EFFQJOTJEFUIBUSPVUJOF4UBUJDDIFDLJOHDBUDIFTUIFUZQFFSSPSTVQGSPOU BUUIFQPJOUXIFSFZPVDBMMFE UIFTVCSPVUJOF X 4UBUJDUZQFDIFDLJOHNBLFTZPVUIJOLNPSFTFSJPVTMZBCPVUZPVSQSPHSBNXIJDIIFMQT UPGVSUIFSSFEVDFCVHT X 8JUIEZOBNJDUZQFDIFDLJOH ZPVEPOUIBWFUPTQFOEUJNFXSJUJOHUZQFBOOPUBUJPOT IUUQXQIPNFTTPJDJOEJBOBFEVKTJFLXIBUJTHSBEVBMUZQJOH
  10. (SBEVBM5ZQJOH def add1(x): return x + 1 class A(object): pass

    a = A() add1(a) IUUQXQIPNFTTPJDJOEJBOBFEVKTJFLXIBUJTHSBEVBMUZQJOH
  11. (SBEVBM5ZQJOH def add1(x : int): return x + 1 class

    A(object): pass a = A() add1(a) IUUQXQIPNFTTPJDJOEJBOBFEVKTJFLXIBUJTHSBEVBMUZQJOH
  12. (SBEVBM5ZQJOH def add1(x : ?): return x + 1 add1(1)

    # int ~> ? IUUQXQIPNFTTPJDJOEJBOBFEVKTJFLXIBUJTHSBEVBMUZQJOH
  13. (SBEVBM5ZQJOH def g(p : int * int): return p[0] def

    f(x, y : int): p = (x, y) g(p) # ? * int ~> int * int IUUQXQIPNFTTPJDJOEJBOBFEVKTJFLXIBUJTHSBEVBMUZQJOH
  14. (SBEVBM5ZQJOH š S ~ T UPFYQSFTTUIBUUZQFS JTDPOTJTUFOUXJUIUZQFT š 'PSBOZUZQFT XFIBWFCPUI?

    ~ T BOET ~ ? š 'PSBOZCBTJDUZQFB TVDIBTJOU XFIBWFB ~ B š "UVQMFUZQFT1 * T2 JTDPOTJUFOUXJUIBOPUIFSUVQMFUZQFS1 * S2 JGT1 ~ S1 BOET2 ~ S2 5IJTSVMFHFOFSBMJ[FTJOBTUSBJHIUGPSXBSEXBZUPUVQMFTPGBSCJUSBSZTJ[F š "GVODUJPOUZQFfun(T1, ..., Tn, R) 5˘5O BSFUIFQBSBNFUFSUZQFT 3JTUIFSFUVSOUZQF  JTDPOTJTUFOUXJUIBOPUIFSGVODUJPOUZQFfun(S1, ..., Sn, U) JGT1 ~ S1˘Tn ~ Sn BOER ~ U IUUQXQIPNFTTPJDJOEJBOBFEVKTJFLXIBUJTHSBEVBMUZQJOH
  15. (SBEVBM5ZQJOH š int ~ int š int !~ bool š

    ? ~ int š bool ~ ? š int * int ~ ? š fun(?, ?) ~ fun(int, int) š ? ~ fun(int, int) š int * int !~ ? * bool IUUQXQIPNFTTPJDJOEJBOBFEVKTJFLXIBUJTHSBEVBMUZQJOH
  16. )FMMPXPSME /* Java: Main.java */ public class Main { public

    static void main(String[] args) { System.out.println("Hello world!"); } }
  17. %ZOBNJDBMMZUZQFE def prime?(n) (2...n).all? { |i| n % i !=

    0 } end # Prime pairs that differ from six def sexy_primes(n) (9..n).map { |i| { i - 6, i } } .select { |i| i.all? { |x| prime?(x) } } end puts sexy_primes(100_000)
  18. #FODINBSL š #SBJOGVDL ˋˋ CFODIC 5JNF T .FNPSZ .C $

      $SZTUBM   (P   3VCZ   IUUQTHJUIVCDPNLPTUZBCFODINBSLT
  19. #FODINBSL š #SBJOGVDL ˋˋ NBOEFMC 5JNF T .FNPSZ .C $

      $SZTUBM   (P   1ZUIPO1ZQZ   IUUQTHJUIVCDPNLPTUZBCFODINBSLT
  20. #FODINBSL š .BUSJYNVMUJQMJDBUJPO ˋˋ .BUNVM 5JNF T .FNPSZ .C $

      $SZTUBM   (P   3VCZ   IUUQTHJUIVCDPNLPTUZBCFODINBSLT
  21. #FODINBSL š )BWMBLMPPQGJOEFSˋˋ )BWMBL 5JNF T .FNPSZ .C $SZTUBM 

     $   (P   1ZUIPO   IUUQTHJUIVCDPNLPTUZBCFODINBSLT
  22. #FODINBSL š 1BSTFCJHKTPO GJMFˋˋ +TPO 5JNF T .FNPSZ .C $

    (BTPO   $SZTUBM X4DIFNB     (P   3VCZ   IUUQTHJUIVCDPNLPTUZBCFODINBSLT
  23. #FODINBSL š 5ISFBESJOH 5JNF T .FNPSZ .C $SZTUBM  

    (P   4DBMB   3VCZ   IUUQTHJUIVCDPNLPTUZBDSZTUBMCFODINBSLTHBNF
  24. #FODINBSL š 3VOUJNFGBDUPS 5JNF OPSNBMJ[FE UP+BWB +BWB  $SZTUBM 

    /PEFKT  3VCZ  IUUQTUFGBONBSSEFEPXOMPBETDSZTUBMIUNM
  25. 5ZQFDIFDLJOH def prime?(n) (2...n).all? { |i| n % i !=

    0 } end def sexy_primes(n) (9..n).map { |i| { i - 6, i } } .select { |i| i.all? { |x| prime?(x) } } end puts sexy_primes(100_000)
  26. 5ZQFDIFDLJOH def prime?(n) (2...n).all? { |i| n % i !=

    0 } end def sexy_primes(n) (9..n).map { |i| { i - 6, i } } .select { |i| i.all? { |x| prime?(x) } } end puts sexy_primes("Oops")
  27. 5ZQFDIFDLJOH def prime?(n) (2...n).all? { |i| n % i !=

    0 } end def sexy_primes(n) (9..n).map { |i| { i - 6, i } } .select { |i| i.all? { |x| prime?(x) } } end puts sexy_primes("Oops") # Error: no overload matches `Int32#<` with type String
  28. 5ZQFDIFDLJOH def prime?(n) (2...n).all? { |i| n % i !=

    0 } end def sexy_primes(n) (9..n).map { |i| { i - 6, i } } .select { |i| i.all? { |x| prime?(x) } } end puts sexy_primes("Oops") sexy_primes # Error: no overload matches `Int32#<` with type String
  29. 5ZQFDIFDLJOH def prime?(n) (2...n).all? { |i| n % i !=

    0 } end def sexy_primes(n) (9..n).map { |i| { i - 6, i } } .select { |i| i.all? { |x| prime?(x) } } end puts sexy_primes("Oops") map sexy_primes # Error: no overload matches `Int32#<` with type String
  30. 5ZQFDIFDLJOH def prime?(n) (2...n).all? { |i| n % i !=

    0 } end def sexy_primes(n) (9..n).map { |i| { i - 6, i } } .select { |i| i.all? { |x| prime?(x) } } end puts sexy_primes("Oops") map sexy_primes # Error: no overload matches `Int32#<` with type String
  31. 5ZQFDIFDLJOH def prime?(n) (2...n).all? { |i| n % i !=

    0 } end def sexy_primes(n) (9..n).map { |i| { i - 6, i } } .select { |i| i.all? { |x| prime?(x) } } end puts sexy_primes(100_000)
  32. 5ZQFDIFDLJOH def prime?(n) (2...n).all? { |i| n % i !=

    0 } end def sexy_primes(n : Int) (9..n).map { |i| { i - 6, i } } .select { |i| i.all? { |x| prime?(x) } } end puts sexy_primes(100_000)
  33. 5ZQFDIFDLJOH def prime?(n) (2...n).all? { |i| n % i !=

    0 } end def sexy_primes(n : Int) (9..n).map { |i| { i - 6, i } } .select { |i| i.all? { |x| prime?(x) } } end puts sexy_primes("Oops")
  34. 5ZQFDIFDLJOH def prime?(n) (2...n).all? { |i| n % i !=

    0 } end def sexy_primes(n : Int) (9..n).map { |i| { i - 6, i } } .select { |i| i.all? { |x| prime?(x) } } end puts sexy_primes("Oops") # Error: no overload matches `sexy_primes` with type String
  35. )JHIMFWFM numbers = [1, 2, 3, 4] puts numbers.map {

    |x| x + 10 } #=> [11, 12, 13, 14] puts numbers[0] + numbers[-1] #=> 5 phones = { "Ann" => "0987654321", "Bob" => "0223456789" } puts phones["Bob"] #=> "0223456789"
  36. )JHIMFWFM objects = [0, "one", { 2, "three" }] objects

    << 456 puts typeof(objects) #=> Array(String | Int32 | {Int32, String}) objects << 78.9 #=> no overload matches 'Array(String | Int32 | {Int32, String})#<<' with type Float64
  37. -PXMFWFM ptr = Pointer(UInt8).malloc(20) ptr.value = 10_u8 puts ptr #=>

    Pointer(UInt8)@0x255a440 a = 1 ptr = pointerof(a) ptr.value = 2 puts a #=> 2 buffer = uninitialized UInt8[1024] # allocated on the stack
  38. -PXMFWFM /* C: math.c */ #include <stdio.h> #include <stdlib.h> /*

    export function */ EXPORT double cos(double x) { /* return cos(x) */ }
  39. 4UBUVT š 7FSTJPO š "MQIBTUBHF š 4UJMMUXFBLJOHUIFMBOHVBHFTQFDJGJDBUJPOBOETUBOEBSEMJCSBSZ š #PPUTUSBQ š

    5IFDPNQJMFSJTXSJUUFOJO$SZTUBM š 3FXSJUJOHDPNQJMFS IUUQDSZTUBMMBOHPSHUIFGVUVSFPGDSZTUBMIUNM  š %FDFOUSBMJ[FEEFQFOEFODZNBOBHFS š 4IBSE IUUQTDSZTUBMTIBSETIFSPLVBQQDPN
  40. $PNNBOEMJOFBQQMJDBUJPOT š UMDSIUUQTHJUIVCDPNQPSSBTUMDS š "TJNQMFUFSNJOBMCBTFEDMJFOUGPS5-%3 IUUQUMESQBHFTHJUIVCJP QBHFT XSJUUFOJO$SZTUBM š DSVMIUUQTHJUIVCDPNQPSSBTDSVM

    š "DVSMSFQMBDFNFOU UIBUJT JUTBDPNNBOEMJOF)551DMJFOU š TIBSETIUUQTHJUIVCDPNZTCBEEBEFOTIBSET š %FQFOEFODZNBOBHFSGPSUIF$SZTUBMMBOHVBHF /PXEJTUSJCVUFE XJUI$SZTUBMJUTFMG š DSZMPBEIUUQTHJUIVCDPN4EPHSVZPMDSZMPBE š )551CFODINBSLJOHUPPMXSJUFO JO$SZTUBM
  41. -JCSBSJFT š %BUBCBTF%SJWFST š .Z42- 1PTUHSFT 3FEJT 42-JUF *OGMVY%# .POHP%#

     š "SUBOJTIUUQTHJUIVCDPNZTCBEEBEFOBSUBOJT š $SZTUBMTNFUBQSPHSBNNJOHNBDSPTUPCVJMEB4JOBUSBMJLF%4- IUUQXXXTJOBUSBSCDPN  š DSZTUBMMJOFIUUQTHJUIVCDPNKUPNTDISPFEFSDSZTUBMMJOF š "DPMMFDUJPOPGDPOUBJOFSTBMHPSJUINT š TZTDBMMDSIUUQTHJUIVCDPNLVCPTZTDBMMDS š 3BXTZTDBMM JOUFSGBDFGPS$SZTUBM š 0UIFST š [MJCDS CTPODS TTIDS BDUJWF@SFDPSEDS PQFOTTMDS EPDLFSDS TQPUJGZDS 
  42. .PSFBOENPSF š 8FC'SBNFXPSLT š "NFUIZTU IUUQTHJUIVCDPN$PEDPSF"NFUIZTU  3BJMTJOTQJSFEXFCGSBNFXPSL š ,FNBM

    IUUQTHJUIVCDPNTEPHSVZPMLFNBM -JHIUOJOH'BTU 4VQFS4JNQMFXFCGSBNFXPSL JOTQJSFECZ4JOBUSB š &NVMBUPST š 3$16 IUUQTHJUIVCDPNEEGSFZOFSDQV 7.FNVMBUPSBOEBTTFNCMFS š OFTDS IUUQTHJUIVCDPNSPNFSPBESJBOOFTDS  "/&4FNVMBUPS š $PNQJMFST š $SJTQ IUUQTHJUIVCDPNSIZTE$SJTQ -JTQEJBMFDUJNQMFNFOUFEXJUI$SZTUBM š $SZTUBM IUUQTHJUIVCDPNDSZTUBMMBOHDSZTUBM $SZTUBMDPNQJMFT$SZTUBM
  43. 0GGJDJBMDPNNVOJUZ š (PPHMFHSPVQ š IUUQTHSPVQTHPPHMFDPNGPSVNGPSVNDSZTUBMMBOH š (JUUFS š IUUQTHJUUFSJNDSZTUBMMBOHDSZTUBM š

    *3$DIBOOFM š DSZTUBMMBOH !JSDGSFFOPEFOFU š 4UBDL0WFSGMPX š IUUQTUBDLPWFSGMPXDPNRVFTUJPOTUBHHFEDSZTUBMMBOH