Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. // main.pony
  2.  
  3. use "files"
  4. use "collections"
  5.  
  6.  
  7. actor Main
  8. let line_processor: LineProcessor
  9. let event_processor: EventProcessor tag
  10.  
  11. new create(env: Env) =>
  12. line_processor = LineProcessor(env)
  13. event_processor = EventProcessor(env)
  14. try
  15. for file_name in env.args.slice(1).values() do
  16. let path = FilePath(env.root as AmbientAuth, file_name)?
  17. match OpenFile(path)
  18. | let file: File =>
  19. for line in FileLines(file) do
  20. env.out.print(line.clone())
  21. line_processor.process_line(
  22. consume line,
  23. {(event: I64, timestamp: U64) =>
  24. event_processor.process_event(event, timestamp)}
  25. val)
  26. end
  27. else
  28. env.err.print("Error opening file '" + file_name + "'")
  29. end
  30. end
  31. end
  32. event_processor.sort()
  33.  
  34. // process_line.pony
  35.  
  36. actor LineProcessor
  37. let env: Env
  38.  
  39. new create(env': Env) =>
  40. env = env'
  41.  
  42. be process_line(line: String, fn: {(I64, U64)} val) =>
  43. let event = line.split_by(":")
  44. if event.size() == 2 then
  45. try
  46. let start_event = event(0)?
  47. let end_event = event(1)?
  48. env.out.print("start at: " + start_event)
  49. env.out.print("end at: " + end_event)
  50. fn(1, start_event.u64()?)
  51. fn(-1, end_event.u64()?)
  52. end
  53. end
  54.  
  55.  
  56. // process_event.pony
  57.  
  58.  
  59. class Event
  60. let event: I64
  61. let timestamp: U64
  62.  
  63. new create(event': I64, timestamp': U64)=>
  64. event = event'
  65. timestamp = timestamp'
  66.  
  67. fun lt(that: box->Event): Bool =>
  68. (timestamp < that.timestamp) or
  69. ((timestamp == that.timestamp) and
  70. (event < that.event))
  71.  
  72. fun le(that: box->Event): Bool =>
  73. (timestamp <= that.timestamp) or
  74. ((timestamp == that.timestamp) and
  75. (event <= that.event))
  76.  
  77. fun gt(that: box->Event): Bool =>
  78. (timestamp > that.timestamp) or
  79. ((timestamp == that.timestamp) and
  80. (event > that.event))
  81.  
  82. fun ge(that: box->Event): Bool =>
  83. (timestamp > that.timestamp) or
  84. ((timestamp == that.timestamp) and
  85. (event >= that.event))
  86.  
  87. fun eq(that: box->Event): Bool =>
  88. (timestamp == that.timestamp) and (event == that.event)
  89.  
  90. fun ne(that: box->Event): Bool =>
  91. (timestamp != that.timestamp) or (event != that.event)
  92.  
  93. fun compare(that: box->Event): (Less val| Equal val | Greater val) =>
  94. if this > that then
  95. return Greater
  96. else
  97. if this < that then
  98. return Less
  99. else
  100. return Equal
  101. end
  102. end
  103.  
  104.  
  105. actor EventProcessor
  106. let env: Env
  107. let events: List[Event] ref
  108.  
  109. new create(env': Env) =>
  110. env = env'
  111. events = List[Event]()
  112.  
  113. be process_event(event: I64, timestamp: U64) =>
  114. env.out.print(event.string() + " at " + timestamp.string())
  115. let new_event = Event(event, timestamp)
  116. events.push(new_event)
  117.  
  118. be sort() =>
  119. Sort[List[Event], Event](events)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement