Top Banner
Falcon Falcon 初初初 初初初 赖赖赖 2010.5.3
64

Falcon初印象

Sep 03, 2014

Download

Technology

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Falcon初印象

Falcon Falcon 初印象初印象

赖勇浩2010.5.3

Page 2: Falcon初印象

特性• 开源• 简单• 快速• 强大

Page 3: Falcon初印象

我最讨厌 python 的 GIL

• Falcon 完全支持多线程编程• 每条线程都运行在一个单独的 VM 上

Page 4: Falcon初印象

我最讨厌 python 不支持协程• 不完全支持• Falcon 支持

Page 5: Falcon初印象

大家都讨厌 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;

Page 6: Falcon初印象

第一行代码• number = 0• 数 = 0

Page 7: Falcon初印象

stdout

• print• printl• >>• >

Page 8: Falcon初印象

if/elif/else 语句• if expression

– statements...• elif expression

– statements...• elif expression

– statements...• /* other elifs */• else

– statements...• end

Page 9: Falcon初印象

逻辑操作符• and• or • not• not 优先级最高

Page 10: Falcon初印象

三元表达式• <condition> ? <if true> [ : <if false>]

Page 11: Falcon初印象

switch 语句• switch expression

– case item [, item, .. item ]• statements...

– case item [, item, .. item ]• statements...

– /* other cases */– default

• statements...

• end

Page 12: Falcon初印象

• 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

Page 13: Falcon初印象

• switch month– case nil

• > "Undefined"– case "Jan", "Feb", "Mar"(P43)

• > "Winter"– case "Apr", "May", "Jun"

• > "Spring"– case "Jul", "Aug", "Sep"

• > "Summer"– default

• > "Autumn"• end

Page 14: Falcon初印象

甚至是……• 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

Page 15: Falcon初印象

select 语句• select variable

– case TypeSpec• ...statements...

– case TypeSpec1 , TypeSpec2 , ..., TypeSpecN• ...statements...

– default• ...statements...

• end

Page 16: Falcon初印象

• 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

Page 17: Falcon初印象

while 语句• while expression

– statements...– [break]– statements...– [continue]– statements...

• end

Page 18: Falcon初印象

loop 语句• count = 0• loop

– > ++ count• end count == 100• 哦,跟 do...while 语句一样……

Page 19: Falcon初印象

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

Page 20: Falcon初印象

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

Page 21: Falcon初印象

dot-assign

• for elem in array– .= 0 // sets all the array elements to zero...– printl(elem) // ... but prints the original items

• end

Page 22: Falcon初印象

For/in ranges

• for value in [1:10]– printl( value )

• end• for value in [ 0: 11: 2 ]

– > value• end

Page 23: Falcon初印象

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

Page 24: Falcon初印象

const

• const name = immediate_value

Page 25: Falcon初印象

enum

• enum Seasons– spring– summer– autumn– winter

• end• > Seasons.spring // 0

Page 26: Falcon初印象

• enum Seasons– spring = 1 // 1– summer // 2– midsummer = "So hot..." // "So hot..."– endsummer // 3 (string skipped)– autumn = 10.12 // 10.12– winter // 11

• end

Page 27: Falcon初印象

基本数据类型• Array• String• Dictionary• List

Page 28: Falcon初印象

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

Page 29: Falcon初印象

range

• R=[n : m] means "all items from n to m-1"

Page 30: Falcon初印象

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 字符串相似

Page 31: Falcon初印象

String replication

• sep = "-~*~-" * 12• > sep• > " "*25 + "Hello there!"• > sep

Page 32: Falcon初印象

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

Page 33: Falcon初印象

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\"])"

Page 34: Falcon初印象

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 ]

Page 35: Falcon初印象

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()

Page 36: Falcon初印象

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

Page 37: Falcon初印象

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

Page 38: Falcon初印象

Bitwise operators

• &&• ||• ^^• ~• <<• >>• &= |= ^=• <<= >>=

Page 39: Falcon初印象

• 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"

Page 40: Falcon初印象

函数• function do_something( parameter )

– printl( "Hey, this is a function saying: ", parameter )

• end

• return nil

Page 41: Falcon初印象

• function square( x )– y = x * x– return y

• end

Page 42: Falcon初印象

递归• function sum_of_first( x )

– if x > 1• return x + sum_of_first( x - 1 )

– end– return x

• end

Page 43: Falcon初印象

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

Page 44: Falcon初印象

• 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

Page 45: Falcon初印象

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 ) )

Page 46: Falcon初印象

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

Page 47: Falcon初印象

Codeblocks

• 匿名函数的语法糖• lambda 表达式• block = { [p1, p2..., pn] => expression }• // or• block = { [p1, p2..., pn] =>

– statement– ...

• }• printl( {a, b => a + b}(2,2) )

Page 48: Falcon初印象

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..." )

Page 49: Falcon初印象

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 )

Page 50: Falcon初印象

Non positional parameters

• function f( alpha, beta, gamma )– > @"alpha: $alpha"– > @"beta : $beta"– > @"gamma: $gamma"

• end• f( gamma| "a" + "b" + "c", beta|"b-value" )

Page 51: Falcon初印象

• 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!

Page 52: Falcon初印象

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

Page 53: Falcon初印象

object 就是单件模式?

Page 54: Falcon初印象

class

• class class_name[ ( param_list ) ] [ from inh1[, inh2, ..., inhN] ]– [ static block ]– [ properties declaration ]– [init block]– [method list]

• end

Page 55: Falcon初印象

• 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

Page 56: Falcon初印象

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

Page 57: Falcon初印象

• 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" )

Page 58: Falcon初印象

Private members

• _ • object privateer

– _private = 0– function getPrivate(): return self._private– function setPrivate(value): self._private =

value• end

Page 59: Falcon初印象

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

Page 60: Falcon初印象

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]

Page 61: Falcon初印象

Subscript overloading

• []• getIndex__• setIndex__

Page 62: Falcon初印象

Automatic string conversion

• toString method

Page 63: Falcon初印象

Error recovery

• try– [try statements]– [ catch [object_type] [ in error_variable] ]– [ catch statements ]

• end

• raise keyword

Page 64: Falcon初印象

Thank you!

• @laiyonghao• http://laiyonghao.com• [email protected]