Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 6

[ 123 ]

}
friendsList = { user ->
TwitterFactory.singleton.getFriendsList(user,-1)
}
cachedFriendsList = friendsList.memoize()

// Method to apply a closure to each friend
def eachFriend(Closure c) {
def friends = friendsList('groovydsl')
friends.each {
c.call(it.screenName)
}
}

// Method to apply a closure to each follower
def eachFollower(Closure c) {
def followers = followersList('groovydsl')
followers.each { follower ->
c.call(follower.screenName)
}
}

// Method to follow another twitter user
void follow(user) {
TwitterFactory.singleton.createFriendship(user )
}

With these methods defined, we can start writing some powerful Groovy code to act
on our friends and followers. How about printing the screen names of all the users
that we are following:


println "I'm Following"
eachFriend {
println it
}

Or those who are following us:


println "Following me"
eachFollower {
println it
}
Free download pdf