dependency sorting in ruby with tsort

16
need some rails power? www.tinci.fr @happynoff Dependency sorting in Ruby with TSort

Upload: simon-courtois

Post on 17-Jul-2015

153 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

Dependency sorting in Ruby with TSort

Page 2: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

Simon Courtois

www.tinci.fr

happynoff

simonc

Page 3: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

Breakfast

Make coffee

Cook Buy eggs

Buy stuff

Buy coffee

Page 4: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

Topological Sort

Page 5: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

TSORT

Page 6: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

require 'tsort'It's in the Ruby's Standard Library!

Page 7: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

Task: name, dependenciesBreakfast, [cook, make coffee]

Page 8: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

todo = TaskList.new todo.add 'breakfast', ['make coffee', 'cook'] todo.add 'cook', ['buy stuff'] todo.add 'make coffee', ['buy stuff'] todo.add 'buy stuff', ['buy coffee', 'buy eggs'] todo.add 'buy coffee' todo.add 'buy eggs'

Page 9: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

class TaskList include TSort end

Page 10: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

class TaskList include TSort

def initialize @tasks = {} end end

Page 11: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

class TaskList include TSort

def initialize @tasks = {} end

def add(name, dependencies = []) @tasks[name] = dependencies end end

Page 12: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

class TaskList # ...

def tsort_each_node(&block) @tasks.each_key(&block) end end

Page 13: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

class TaskList # ...

def tsort_each_child(node, &block) @tasks[node].each(&block) end end

Page 14: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

todo = TaskList.new todo.add 'breakfast', ['make coffee', 'cook'] todo.add 'cook', ['buy stuff'] todo.add 'make coffee', ['buy stuff'] todo.add 'buy stuff', ['buy coffee', 'buy eggs'] todo.add 'buy coffee' todo.add 'buy eggs'

puts todo.tsort # >> buy coffee # >> buy eggs # >> buy stuff # >> make coffee # >> cook # >> breakfast

Page 15: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

Questions?

Page 16: Dependency sorting in Ruby with TSort

need some rails power? www.tinci.fr@happynoff

Thanks!