Search Gear #18 – Anonymous Functions and Closures

Now, I also need a function to process wildcards out of hashtags maybe creating multiple requests from a single tag.

Let’s go with anonymous functions and closures, which in flirted with on day 7, what seems like years ago now!

https://gobyexample.com/closures


func getItem(entries toml.Value) func() string {
i := -1
return func() string {
i += 1
if i < len(entries.AsArray()) {
return entries.AsArray()[i].AsString()
} else {
return ""
}
}
}

Then a function to loop ‘collect’ for each tag, user, and home.

Get Me! Parallel processing!


func (tw *twitter) GetTags() {
nextItem := getItem(tw.tags)
for {
if entry := nextItem(); entry != "" {
go tw.collect(entry)
} else {
break
}
}
}

func (tw *twitter) GetUsers() {
nextItem := getItem(tw.users)
for {
if entry := nextItem(); entry != "" {
go tw.collect(entry)
} else {
break
}
}
}

func (tw *twitter) GetHome() {
tw.collect("MyHome")
}

HUMM testing tomorrow.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s