Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.pony
- use "files"
- use "collections"
- actor Main
- let line_processor: LineProcessor
- let event_processor: EventProcessor tag
- new create(env: Env) =>
- line_processor = LineProcessor(env)
- event_processor = EventProcessor(env)
- try
- for file_name in env.args.slice(1).values() do
- let path = FilePath(env.root as AmbientAuth, file_name)?
- match OpenFile(path)
- | let file: File =>
- for line in FileLines(file) do
- env.out.print(line.clone())
- line_processor.process_line(
- consume line,
- {(event: I64, timestamp: U64) =>
- event_processor.process_event(event, timestamp)}
- val)
- end
- else
- env.err.print("Error opening file '" + file_name + "'")
- end
- end
- end
- event_processor.sort()
- // process_line.pony
- actor LineProcessor
- let env: Env
- new create(env': Env) =>
- env = env'
- be process_line(line: String, fn: {(I64, U64)} val) =>
- let event = line.split_by(":")
- if event.size() == 2 then
- try
- let start_event = event(0)?
- let end_event = event(1)?
- env.out.print("start at: " + start_event)
- env.out.print("end at: " + end_event)
- fn(1, start_event.u64()?)
- fn(-1, end_event.u64()?)
- end
- end
- // process_event.pony
- class Event
- let event: I64
- let timestamp: U64
- new create(event': I64, timestamp': U64)=>
- event = event'
- timestamp = timestamp'
- fun lt(that: box->Event): Bool =>
- (timestamp < that.timestamp) or
- ((timestamp == that.timestamp) and
- (event < that.event))
- fun le(that: box->Event): Bool =>
- (timestamp <= that.timestamp) or
- ((timestamp == that.timestamp) and
- (event <= that.event))
- fun gt(that: box->Event): Bool =>
- (timestamp > that.timestamp) or
- ((timestamp == that.timestamp) and
- (event > that.event))
- fun ge(that: box->Event): Bool =>
- (timestamp > that.timestamp) or
- ((timestamp == that.timestamp) and
- (event >= that.event))
- fun eq(that: box->Event): Bool =>
- (timestamp == that.timestamp) and (event == that.event)
- fun ne(that: box->Event): Bool =>
- (timestamp != that.timestamp) or (event != that.event)
- fun compare(that: box->Event): (Less val| Equal val | Greater val) =>
- if this > that then
- return Greater
- else
- if this < that then
- return Less
- else
- return Equal
- end
- end
- actor EventProcessor
- let env: Env
- let events: List[Event] ref
- new create(env': Env) =>
- env = env'
- events = List[Event]()
- be process_event(event: I64, timestamp: U64) =>
- env.out.print(event.string() + " at " + timestamp.string())
- let new_event = Event(event, timestamp)
- events.push(new_event)
- be sort() =>
- Sort[List[Event], Event](events)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement