Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Basics of Computer Science

368 views

Published on

Presentation held at Swift.Berlin

Published in: Education
  • Be the first to comment

  • Be the first to like this

Basics of Computer Science

  1. 1. Basics&of&Computer&Science Maxim&Zaks @iceX33 Things'I'learned'on'the'job,'a3er'my'CS'degree
  2. 2. Computer)Science)is: Shoveling*Data*Around Things'I'learned'on'the'job,'a3er'my'CS'degree
  3. 3. Computer)Science)is: Consuming)Electricity Things'I'learned'on'the'job,'a3er'my'CS'degree
  4. 4. Fast%code%produces%less%Data Things'I'learned'on'the'job,'a3er'my'CS'degree
  5. 5. Things'I'learned'on'the'job,'a3er'my'CS'degree
  6. 6. How$do$we$represent$data$in$Swi/? Things'I'learned'on'the'job,'a3er'my'CS'degree
  7. 7. By#Value: Tuple,'Struct'or'Enum By#Reference: Class,&Func+on Things'I'learned'on'the'job,'a3er'my'CS'degree
  8. 8. Value&Types&are&not&sharable! Things'I'learned'on'the'job,'a3er'my'CS'degree
  9. 9. Taking'a'car'for'a'drive Things'I'learned'on'the'job,'a3er'my'CS'degree
  10. 10. Value&types&are&cheaper&to&create Things'I'learned'on'the'job,'a3er'my'CS'degree
  11. 11. typealias TPerson = (name:String, age:Int, male:Bool) +-----------------------------------------+--------------------------------------------------+ struct SPerson{ | class CPerson{ let name : String | let name : String let age : Int | let age : Int let male : Bool | let male : Bool } | | init(name : String, age : Int, male : Bool){ +-----------------------------------------+ self.name = name enum EPerson { | self.age = age case Male(name: String, age : Int) | self.male = male case Female(name : String, age : Int) | } | } func name()->String{ | switch self { | case let Male(name, _): | return name | case let Female(name, _): | return name | } | } | } + Things'I'learned'on'the'job,'a3er'my'CS'degree
  12. 12. Benchmark*in*ms • 000.00786781311035156*Tuple*10M • 000.00405311584472656*Enum*10M • 000.003814697265625*Struct*10M • 813.668251037598*Class*10M Things'I'learned'on'the'job,'a3er'my'CS'degree
  13. 13. How$do$we$represent$a$collec/on$of$ data? Things'I'learned'on'the'job,'a3er'my'CS'degree
  14. 14. Data$Structures Things'I'learned'on'the'job,'a3er'my'CS'degree
  15. 15. Reference'or'Array'Based Things'I'learned'on'the'job,'a3er'my'CS'degree
  16. 16. Performance*Characteris0cs • Add$(Prepand$/$Append$/$Insert) • Get$(First$/$Last$/$ByIndex$/$ByValue) • Find$(Biggest$/$Smallest)$ • Delete$(ByIndex$/$ByValue) • Concatenate Things'I'learned'on'the'job,'a3er'my'CS'degree
  17. 17. Swi$%Data%Structures Array,&Dic*onary,&Set,&String All#defined#as#Struct Things'I'learned'on'the'job,'a3er'my'CS'degree
  18. 18. Once%upon%a%*me,%(before%Swi4%2) I"decided"to"implement"my"own"List Things'I'learned'on'the'job,'a3er'my'CS'degree
  19. 19. +-----------------------------------+-------------------------------------------------------------+ | private protocol List{ | public struct ListNode<T> : List { var tail : List {get} | public let value : T } | private let _tail : List +-----------------------------------+ private var tail : List { | return _tail private struct EmptyList : List{ | } var tail : List { | return EmptyList() | private init(value: T, tail : List){ } | self.value = value } | self._tail = tail | } | | public subscript(var index : UInt) -> ListNode<T>?{ +-----------------------------------+ var result : List = self | while(index>0){ | result = result.tail | index-- infix operator => { | } associativity right | return result as? ListNode<T> precedence 150 | } } | | } +-----------------------------------+-------------------------------------------------------------+ public func => <T>(lhs: T, rhs: ListNode<T>?) -> ListNode<T> { if let node = rhs { return ListNode(value: lhs, tail: node) } return ListNode(value: lhs, tail: EmptyList()) } Things'I'learned'on'the'job,'a3er'my'CS'degree
  20. 20. Benchmark*in*ms • 208.853006362915+List+1K • 000.30207633972168+BoxEList+1K • 000.0660419464111328+ClassList+1K • 000.0400543212890625+EEList+1K • 000.0429153442382812+Array+1K Things'I'learned'on'the'job,'a3er'my'CS'degree
  21. 21. WTF$happened? Things'I'learned'on'the'job,'a3er'my'CS'degree
  22. 22. Let's&talk&about&Pure&Func2onal& Programming Things'I'learned'on'the'job,'a3er'my'CS'degree
  23. 23. Things'I'learned'on'the'job,'a3er'my'CS'degree
  24. 24. Purely'Func+onal'Data'Structure: No#Destruc+ve#updates Persistent((not(ephemeral) Things'I'learned'on'the'job,'a3er'my'CS'degree
  25. 25. Why$is$it$important? Things'I'learned'on'the'job,'a3er'my'CS'degree
  26. 26. Referen&al)transparency Things'I'learned'on'the'job,'a3er'my'CS'degree
  27. 27. An#expression#always#evaluates#to#the#same# result#in#any#context: 2"+"3 array.count Things'I'learned'on'the'job,'a3er'my'CS'degree
  28. 28. Purely'Func+onal'Data'Structure: No#Destruc+ve#updates Persistent((not(ephemeral) Things'I'learned'on'the'job,'a3er'my'CS'degree
  29. 29. But$our$hardware$memory$is$ destruc1ve$and$ephemeral (Physics) Things'I'learned'on'the'job,'a3er'my'CS'degree
  30. 30. Things'I'learned'on'the'job,'a3er'my'CS'degree
  31. 31. Structural(Sharing Things'I'learned'on'the'job,'a3er'my'CS'degree
  32. 32. Things'I'learned'on'the'job,'a3er'my'CS'degree
  33. 33. Swi$ Reference'vs.'Value'Type Things'I'learned'on'the'job,'a3er'my'CS'degree
  34. 34. Value&Type&are&not&sharable! So#no#structural#sharing#is#posible Things'I'learned'on'the'job,'a3er'my'CS'degree
  35. 35. Benchmark*in*ms • 208.853006362915+List+1K • 000.30207633972168+BoxEList+1K • 000.0660419464111328+ClassList+1K • 000.0400543212890625+EEList+1K • 000.0429153442382812+Array+1K Things'I'learned'on'the'job,'a3er'my'CS'degree
  36. 36. enum EEList<Element> { case End indirect case Node(Element, next: EEList<Element>) var value: Element? { switch self { case let Node(x, _): return x case End: return nil } } } extension EEList { func cons(x: Element) -> EEList { return .Node(x, next: self) } } Things'I'learned'on'the'job,'a3er'my'CS'degree
  37. 37. Why$are$Swi+$data$structures$ defined$as$structs? Things'I'learned'on'the'job,'a3er'my'CS'degree
  38. 38. Swi$%data%structure%types%are% facades%which%support No#Destruc+ve#updates Persistent((not(ephemeral) Things'I'learned'on'the'job,'a3er'my'CS'degree
  39. 39. Is#there#(me#to#talk#about# Singletons? Things'I'learned'on'the'job,'a3er'my'CS'degree
  40. 40. Thank&you! Things'I'learned'on'the'job,'a3er'my'CS'degree
  41. 41. Singleton)is)a)globaly)shared) instance Things'I'learned'on'the'job,'a3er'my'CS'degree
  42. 42. How$many$shared$instances$do$you$see$here? private var fibRow = [0, 1, 2] public func fibM(number:Int)->Int{ if number >= fibRow.count { fibRow.append(fibM(number-2)+fibM(number-1)) } return fibRow[number] } Things'I'learned'on'the'job,'a3er'my'CS'degree
  43. 43. Named&func+ons&are&shared& instances Things'I'learned'on'the'job,'a3er'my'CS'degree
  44. 44. Sharing(means(coupling Things'I'learned'on'the'job,'a3er'my'CS'degree
  45. 45. Coupling)means • Tough'to'test • And'tough'to'reuse The$problem$with$object0oriented$languages$is$they've$got$all$this$ implicit$environment$that$they$carry$around$with$them.$You$wanted$ a$banana$but$what$you$got$was$a$gorilla$holding$the$banana$and$the$ en<re$jungle. —"Joe"Armstrong Things'I'learned'on'the'job,'a3er'my'CS'degree
  46. 46. We#can#solve#the#coupling#problem# by#abstract#type#defini7ons Things'I'learned'on'the'job,'a3er'my'CS'degree
  47. 47. +----------------+ +----------------+ | | | | | SomethingA +-------> | AbstractB | | | | | +----------------+ +-------+--------+ ^ | | | +-------+--------+ | | | SomethingB | | | +----------------+ Things'I'learned'on'the'job,'a3er'my'CS'degree
  48. 48. What%about%shared%state? Isn't&share&nothing (self&sufficient).architecture.be3er? Things'I'learned'on'the'job,'a3er'my'CS'degree
  49. 49. Shared'resources: • Memory • I/O • Network Things'I'learned'on'the'job,'a3er'my'CS'degree
  50. 50. Things'I'learned'on'the'job,'a3er'my'CS'degree
  51. 51. Not$that$bigger$problem$for iOS/OSX&developers. Things'I'learned'on'the'job,'a3er'my'CS'degree
  52. 52. Apple%takes%cares%of%it: • default... • shared... • main... Things'I'learned'on'the'job,'a3er'my'CS'degree
  53. 53. Be#aware#of#Amdahl's#law The$speedup$of$a$program$using$mul2ple$processors$in$parallel$ compu2ng$is$limited$by$the$2me$needed$for$the$sequen2al$frac2on$ of$the$program. —"Wikipedia Things'I'learned'on'the'job,'a3er'my'CS'degree
  54. 54. Now$I$am$done$:) Things'I'learned'on'the'job,'a3er'my'CS'degree
  55. 55. Ques%ons? Things'I'learned'on'the'job,'a3er'my'CS'degree
  56. 56. Thank&you Things'I'learned'on'the'job,'a3er'my'CS'degree
  57. 57. Links: • PerformanceTest • Fibonacci • Fork2Join2illustra6on • Linked2List2implementa6on2with2Swi=22Enum • Pure2Func6onal2Data2Structures Things'I'learned'on'the'job,'a3er'my'CS'degree

×