Created
December 23, 2013 08:11
-
-
Save nimolix/8093326 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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