Immutability

789 views

Published on

CocoaHeads Kaohsiung

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
789
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
9
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Immutability

  1. 1. Immutability Yung-Luen LanFriday, August 10, 12
  2. 2. 我是誰? • 藍永倫 @yllan • 前 CocoaHeads Taipei Organizer. • 寫過 (Open Source) Cocoa App.Friday, August 10, 12
  3. 3. Friday, August 10, 12
  4. 4. 我以為 OSS 是這樣Friday, August 10, 12
  5. 5. 實際上得到的: “堂堂⼀一個⽐比⼀一搬⼈人聰明的電腦程式設計師, 還是 Mac程式 ⼯工程師, 竟然講話 像 是 在⼆二⼿手煙密佈的骯髒網咖裡⾯面打電動玩具的 ⼩小混混講的話. 真的令⼈人不敢領教, 真的是 讓⼈人很遺憾!! 堂堂⼀一個 Mac OS 軟體設計⼯工程師, 竟 然還說:我如果⼼心地很壞, 妳問我, 我會⽼老實回答嗎? 幹XXXXX 他⾺馬的, 我真的很⽣生氣, 很⽣生氣~~~ 竟然⼀一個程式設計師的⽔水準是 這樣的.” “幹林勞奧” “我⾮非常 90%肯定 他的軟體 不安全!!! 請⼤大家 ⾃自⼰己⼩小⼼心!!! 不是 每⼀一個電腦⼯工程師 都是 正派的!!”Friday, August 10, 12
  6. 6. Friday, August 10, 12
  7. 7. 在主題 開始前Friday, August 10, 12
  8. 8. 壹Friday, August 10, 12
  9. 9. 警告Friday, August 10, 12
  10. 10. Slides 裡的 code 都是錯的Friday, August 10, 12
  11. 11. 貮Friday, August 10, 12
  12. 12. Friday, August 10, 12
  13. 13. immutable |iˈmyo͞otəәbəәl| adjective unchanging over time or unable to be changed: an immutable fact.Friday, August 10, 12
  14. 14. 無法Friday, August 10, 12 修改
  15. 15. 安全 第一Friday, August 10, 12
  16. 16. ChangeFriday, August 10, 12
  17. 17. char *p = "hello"; p[0] = c; // GOTCHA!Friday, August 10, 12
  18. 18. - (void) setUsers: (NSArray *)users { if (users != _users) { [_users release]; _users = [users retain]; } } - (NSArray *) users { return _users; }Friday, August 10, 12
  19. 19. CocoaHeads *ksBranch = [CocoaHeads new]; NSMutableArray *people = [NSMutableArray arrayWithObjects: @"mikimoto", …, nil]; [ksBranch setUsers: people]; [people addObject: @"yllan"]; // BAD!Friday, August 10, 12
  20. 20. MUTABLEFriday, August 10, 12
  21. 21. Immutable in Cocoa • NSArray • NSDictionary • NSSet • NSString •…Friday, August 10, 12
  22. 22. Mutable in Cocoa • NSMutableArray • NSMutableDictionary • NSMutableSet • NSMutableString •…Friday, August 10, 12
  23. 23. init for Immutable • 在 init 時就給定初始值。 • 到被 dealloc 前都無法修改。 • 像物件版的 const!Friday, August 10, 12
  24. 24. + (id) classnameWithData: (id)data - (id) initWithData: (id)dataFriday, August 10, 12
  25. 25. 沒有Friday, August 10, 12 用?
  26. 26. DerivationFriday, August 10, 12
  27. 27. - (id) arrayByAddingObject: Array1 Array2Friday, August 10, 12
  28. 28. // init + (id) classnameWithData: (id)data - (id) initWithData: (id)data // derive - (classname *) classnameByDoSomething…Friday, August 10, 12
  29. 29. 也要有MutableFriday, August 10, 12
  30. 30. // init + (id) classnameWithData: (id)data - (id) initWithData: (id)data // derive - (classname *) classnameByDoSomething… @interface MutableClassname : Classname // modify - (void) doSomething…Friday, August 10, 12
  31. 31. CocoaHeads *ksBranch = [CocoaHeads new]; NSMutableArray *people = [NSMutableArray arrayWithObjects: @"mikimoto", …, nil]; [ksBranch setUsers: people]; [people addObject: @"yllan"]; // BAD! NSMutableArray 也是 NSArray!Friday, August 10, 12
  32. 32. CopyFriday, August 10, 12
  33. 33. NSArray *a = [NSArray new]; NSMutableArray *mA = [NSMutableArray new]; NSArray *b = [a copy]; NSMutableArray *mB = [mA copy]; [a arrayByAddingObject: @"Hello"]; [mA addingObject: @"Hello"]; [b arrayByAddingObject: @"Hello"]; [mB addingObject: @"Hello"];Friday, August 10, 12
  34. 34. NSArray *a = [NSArray new]; NSMutableArray *mA = [NSMutableArray new]; NSArray *b = [a copy]; NSMutableArray *mB = [mA copy]; [a arrayByAddingObject: @"Hello"]; [mA addingObject: @"Hello"]; [b arrayByAddingObject: @"Hello"]; [mB addingObject: @"Hello"]; // DAMN!Friday, August 10, 12
  35. 35. copy: immutable version mutableCopy: mutable versionFriday, August 10, 12
  36. 36. @interface Classname <NSCopying, NSMutableCopying> // init + (id) classnameWithData: (id)data - (id) initWithData: (id)data // derive - (classname *) classnameByDoSomething… // copy - (id) copyWithZone: (NSZone *)zone - (id) mutableCopyWithZone: (NSZone *)zoneFriday, August 10, 12
  37. 37. 細節Friday, August 10, 12
  38. 38. 不用真的 copy!Friday, August 10, 12
  39. 39. immutable 的話 只要 retain 就好!Friday, August 10, 12
  40. 40. Heavy RotationDerivation?Friday, August 10, 12
  41. 41. - (id) arrayByAddingObject: Array1 Array2Friday, August 10, 12
  42. 42. Persistent Data Structure come to the rescue!Friday, August 10, 12
  43. 43. m 43Friday, August 10, 12
  44. 44. m n 44Friday, August 10, 12
  45. 45. m n 45Friday, August 10, 12
  46. 46. m n 46Friday, August 10, 12
  47. 47. m n 47Friday, August 10, 12
  48. 48. 優點 • 部分更新,省時間 O(lg n) • 共享結構,省空間 n + k O(lg n) with k derivation • ⾃自然擁有歷史紀錄Friday, August 10, 12
  49. 49. 缺點 • Pointer 造成 overhead • 對 CPU cache 較不友善Friday, August 10, 12
  50. 50. Mutable ↔ Immutable Mutable Immutable Convert Update Derive Immutable backed by Mutable @interface MyArray { NSMutableArray *_array; O(n) O(1) O(n) } Mutable backed by Immutable O(n) O(n) @interface MyMutableArray { O(1) } NSArray *_array; O(lg n) O(lg n) 50Friday, August 10, 12
  51. 51. [] is fast.Friday, August 10, 12
  52. 52. ⽐比你想得複雜 • http://ridiculousfish.com/blog/posts/ array.html 52Friday, August 10, 12
  53. 53. Friday, August 10, 12
  54. 54. 問題?Friday, August 10, 12

×