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

Falcon intro. techparty.org

Falcon intro. techparty.org

100503-gz-reftory

Zoom.Quiet

May 18, 2010
Tweet

More Decks by Zoom.Quiet

Other Decks in Technology

Transcript

  1. 大家都讨厌 python 的速度 • Falcon 要快得多 • Raw VM loop

    speed : 1023 – Python : 442|340 – Lua : 1247 • Raw VM loop speed – int a = 0; – for( int i = 0; i < 100000000; i++ ) a = a + 1;
  2. if/elif/else 语句 • if expression – statements... • elif expression

    – statements... • elif expression – statements... • /* other elifs */ • else – statements... • end
  3. switch 语句 • switch expression – case item [, item,

    .. item ] • statements... – case item [, item, .. item ] • statements... – /* other cases */ – default • statements... • end
  4. • print( "Enter your age: >" ) • age =

    int( input() ) • switch age – case 1 to 5 • printl( "You are too young to program. Maybe." ) – case 6, 7, 8 • printl( "You may already be a great Falcon programmer." ) – case 9, 10 • printl( "You are ready to become a programmer." ) – default • printl( "What are you waiting for? Start programming NOW" ) • end
  5. • switch month – case nil • > "Undefined" –

    case "Jan", "Feb", "Mar"(P43) • > "Winter" – case "Apr", "May", "Jun" • > "Spring" – case "Jul", "Aug", "Sep" • > "Summer" – default • > "Autumn" • end
  6. 甚至是…… • switch func – case print • printl( "It

    was a print !!!" ) – case printl • printl( "It was a printl !!!" ) – case MyObject • printl( "The value of func is the same of MyObject" ) • end
  7. select 语句 • select variable – case TypeSpec • ...statements...

    – case TypeSpec1 , TypeSpec2 , ..., TypeSpecN • ...statements... – default • ...statements... • end
  8. • select param – case IntegerType, NumericType • return "number"

    – case StringType: return "string" – case Test2: return "class test2" – case Test: return "class test" – case Instance: return "instance obj" – default : return "something else" • end
  9. while 语句 • while expression – statements... – [break] –

    statements... – [continue] – statements... • end
  10. loop 语句 • count = 0 • loop – >

    ++ count • end count == 100 • 哦,跟 do...while 语句一样……
  11. for/in 循环 • 嗯……其实很多语言都有……它的只是太强大了…… • for variable[,variable...] in collection –

    ...statements... – [break | continue | continue dropping] – ...statements... – forfirst • ... first time only statements ... – end – formiddle • ... statements executed between element processing ... – end – forlast • ... last time only statements ... – end • end
  12. continue dropping • array = [ 1, 2, 3, 4,

    5 ] • for elem in array – if elem % 2 == 1 • continue dropping – end – printl( "We accepted the even number: ", elem ) • end
  13. dot-assign • for elem in array – .= 0 //

    sets all the array elements to zero... – printl(elem) // ... but prints the original items • end
  14. For/in ranges • for value in [1:10] – printl( value

    ) • end • for value in [ 0: 11: 2 ] – > value • end
  15. For/to loops • for variable = lowerbound to upperbound [,

    step] – // for/to body, same as for/in • end • for i = 1 to 10 – forfirst: >> "Starting: " – >> i – formiddle: >> ", " – forlast: > "." • end • for i = 2 to 10, 2 – > i • end
  16. enum • enum Seasons – spring – summer – autumn

    – winter • end • > Seasons.spring // 0
  17. • enum Seasons – spring = 1 // 1 –

    summer // 2 – midsummer = "So hot..." // "So hot..." – endsummer // 3 (string skipped) – autumn = 10.12 // 10.12 – winter // 11 • end
  18. Array • array = ["person", 1, 3.5, int( "123" ),

    var1 ] • array = "person", 1, 3.5, int( "123" ), var1 • a2 = .[ 1 2 3 4 'a' 'b' var1 + var2 var3 * var4 .[x y z]] • a, b, c = 1, 2, 3 • array = 1, 2, 3 • a, b, c = array
  19. String • string = "Hello world" • longString = "

    – Aye, matey, this is a very long string. – Let me tell you about my remembering – of when men were men, women were women, – and life was so great.“ • iStr = ' – 国際ストリング – 国際ストリング – ‘ • > 'Hello ''quoted'' world!' // Will print "Hello 'quoted' world" • 基础特性跟 C 字符串相似
  20. String replication • sep = "-~*~-" * 12 • >

    sep • > " "*25 + "Hello there!" • > sep
  21. String-to-number concatenation • string = "Value: " + 100 •

    > string // prints "Value: 100" • string = "Value: " % 64 • > string // prints "Value: A" • string %= 0x3B2 • > string // "Value: Aβ" • d_letter = "a" / 3 // chr( ord('a') + 3) == 'd' • a_letter = d_letter / -3 // chr( ord('d') - 3) == 'a' • > a_letter, ", ", d_letter
  22. String expansion operator • value = 1000 • printl( @

    "Value is $value" ) • array = [ 100, 200, 300 ] • printl( @ "Array is $array[0], $array[1], $array[2]" ) • printl( @ "The selected value is $(array[ value ])." ) • dict = [ "a" => 1, "b" => 2] • > @ "A is $(dict['a']), and B is $(dict[\"b\"])"
  23. Dictionary • dict = [ => ] // creates an

    empty dictionary • dict = [ "a" => 123, "b" => "onetwothree" ] • printl( dict["a"] ,":", dict["b"] ) // 123:onetwothree • a = [ 1=>'1', 2=>'2', "alpha"=>0, "beta"=>1 ]
  24. List • deque • 高效的两端操作 • l = List( "a",

    "b", "c" ) • > "Elements in list: ", l.len() • > "First element: ", l.front() • > "Last element: ", l.back() • l.pushFront( "newFront" ) • > "New first element: ", l.front() • l.push( "newBack" ) • > "New first element: ", l.back() • l.popFront() • l.pop() • > "Element count now: ", l.len()
  25. in/notin • name = input() • if "abba" in name

    – printl( "Your name contains a famous pop group name" ) • end • dict = [ "one" => 1 ] • if "one" in dict – printl( "always true" ) • end • if "abba" notin name – printl( "Your name does not contain a famous pop group name" ) • end
  26. Memory buffers • memory = MemBuf( 5, 2 ) //

    creates a table of 5 elements, each 2 bytes long. • for value in [0:5] – memory[ value ] = value * 256 • end
  27. Bitwise operators • && • || • ^^ • ~

    • << • >> • &= |= ^= • <<= >>=
  28. • value = 0x1 || 0x2 // or bits 0

    and 1 • // display binary: • > @"$(value:b)b = $(value:X)H = $value" • value = value && 0x1 // turns off bit 2 • > @"$(value:b)b = $(value:X)H = $value" • value = ~value && 0xFFFF // Shows first 2 bytes of reversed value • > @"$(value:b)b = $(value:X)H = $value" • value = value ^^ 0x3 // turns off bit 2 and on bit 1 • > @"$(value:b)b = $(value:X)H = $value"
  29. 函数 • function do_something( parameter ) – printl( "Hey, this

    is a function saying: ", parameter ) • end • return nil
  30. • function square( x ) – y = x *

    x – return y • end
  31. 递归 • function sum_of_first( x ) – if x >

    1 • return x + sum_of_first( x - 1 ) – end – return x • end
  32. Local and global variable names • sqr = 1.41 •

    function square( x ) – printl( "sqr was: ", sqr ) – sqr = x * x – return sqr • end • number = square( 8 ) * sqr • function square_in_z( x ) – global z – z = x * x • end • z = 0 • square_in_z( 8 ) • printl( z ) // 64
  33. • function say_something() – static • data = [ "have",

    "a", "nice", "day" ] • current = 0 – end – if current == len( data ) • return – end – element = data[current] – current += 1 – return element • end
  34. Anonymous and nested functions • innerfunc • var = innerfunc

    ( [param1, param2, ..., paramN] ) – [static block] – [statements] • end • square = innerfunc ( a ) – return a * a • end • printl( "Square of 10: ", square( 10 ) )
  35. Function closure • function keyword • function makeMultiplier( operand )

    – multiplier = function( value ) • return value * operand – end – return multiplier • end • m2 = makeMultiplier( 2 ) // ... by 2 • > m2( 100 ) // will be 200 • m4 = makeMultiplier( 4 ) // ... by 4 • > m4( 100 ) // will be 400
  36. Codeblocks • 匿名函数的语法糖 • lambda 表达式 • block = {

    [p1, p2..., pn] => expression } • // or • block = { [p1, p2..., pn] => – statement – ... • } • printl( {a, b => a + b}(2,2) )
  37. Callable arrays • array 的第一个元素是函数,即为 callable array • printl( "Hello

    world" ) • [printl]( "Hello world" ) • [printl, "Hello"]( " world" ) • [printl, "Hello", " ", "world"]() • i = 0 • icall = .[printl $i ": "] • for i in [0:10]: icall( "Looping..." )
  38. Accessing the calling context • fself keyword • caller keyword

    • function recurse( val ) – if val <= 0: return 1 – > recurse.caller(), ":", val // or fself.caller() – return recurse( val-1 ) + val • end • recurse( 5 )
  39. Non positional parameters • function f( alpha, beta, gamma )

    – > @"alpha: $alpha" – > @"beta : $beta" – > @"gamma: $gamma" • end • f( gamma| "a" + "b" + "c", beta|"b-value" )
  40. • future bindings • future_beta = beta|"b-value" • future_gamma =

    lbind( "gamma", "a" + "b" + "c" ) • f( future_gamma, future_beta ) • The lbind function can create late and future bindings; • f( non_existing|"value" ) // raises an error!
  41. object • object object_name [ from class1, class2 ... classN]

    – property_1 = expression – property_2 = expression – ... – property_N = expression – [init block] – function method_1( [parameter_list] ) • [method_body] – end – ... – function method_N( [parameter_list] ) • [method_body] – end • end
  42. class • class class_name[ ( param_list ) ] [ from

    inh1[, inh2, ..., inhN] ] – [ static block ] – [ properties declaration ] – [init block] – [method list] • end
  43. • class mailbox( max_msg ) – capacity = max_msg *

    10 – name = nil – messages = [] – init • printl( "Box now ready for ", self.capacity, " messages." ) – end – function slot_left() • return self.max_msg - len( self.messages ) – end • end
  44. Property accessors • __set_propname/__get_propname • class Series( values ) –

    values = values – function __get_mean() • sum = 0 • for i in self.values: sum += i • return sum / self.values.len() – end • end • s = Series( [14,53,18,8] ) • > "The mean is: ", s.mean // 23.25
  45. • class parent1( p ) • end • class parent2(

    p ) • end • class child(p1, p2) from parent1( p1 ), parent2( p2 ) – init • > "Initializing child with ", p1, " and ", p2 – end • end • instance = child( "First", "Second" )
  46. Private members • _ • object privateer – _private =

    0 – function getPrivate(): return self._private – function setPrivate(value): self._private = value • end
  47. Operator overloading • class OverPlus( initValue ) – numval =

    initValue – function add__( operand ) • return OverPlus( self.numval + operand ) – end • end • op = OverPlus( 10 ) • nop = op + 10 • > nop.numval //: 20
  48. Comparison overloading • Comparison operators, namely <, >, <=, >=,

    == and != all refer to the same overloaded method: compare. • 没有 __ 结尾 • class CmpOver( val ) – number = val – function compare( oper ) • if oper provides number – return self.number - oper.number • elif oper.typeId() == NumericType or oper.typeId() == IntegerType – return self.number - oper • end • return nil – end • end • ten = CmpOver( 10 ) • > "Is ten > 5? ", ten > 5 • > "Is ten != 3? ", ten != 3 • > "Is ten <= 10? ", ten <= 10 • > "Is ten > an array? ", ten > [1,2,3]
  49. Error recovery • try – [try statements] – [ catch

    [object_type] [ in error_variable] ] – [ catch statements ] • end • raise keyword