Skip to content

Instantly share code, notes, and snippets.

@nimolix
Created December 23, 2013 08:11
Show Gist options
  • Save nimolix/8093326 to your computer and use it in GitHub Desktop.
Save nimolix/8093326 to your computer and use it in GitHub Desktop.
package main
type Task struct {
action func()
depends_on []*Task
}
func (t *Task) Run() {
if t.depends_on != nil {
for _, st := range t.depends_on {
st.action()
}
}
t.action()
}
type ActionT struct {
action func()
}
type DependencyT struct {
d []*Task
}
func Action(fn func()) ActionT { return ActionT{fn} }
func DependsOn(t ...*Task) DependencyT { return DependencyT{t} }
func task(a ...interface{}) *Task {
result := &Task{}
for _, x := range a {
switch v := x.(type) {
case ActionT:
result.action = v.action
case DependencyT:
result.depends_on = v.d
}
}
return result
}
//------------------------------
func main() {
var hello = task(
Action(func() { println("Hello") }))
var world = task(
Action(func() { println("World!") }),
DependsOn(hello))
var bye = task(
Action(func() { println("Bye!") }),
DependsOn(hello, world))
bye.Run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment