Book Image

Groovy for Domain-Specific Languages, Second Edition

By : Fergal Dearle
Book Image

Groovy for Domain-Specific Languages, Second Edition

By: Fergal Dearle

Overview of this book

Table of Contents (20 chapters)
Groovy for Domain-specific Languages Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Introduction to DSLs and Groovy
Index

Groovy improvements


So far we have been using Twitter4J as a vanilla API, with a smattering of Groovy, so we have not been bringing any of the Groovier features to bear. Now that we know a little bit about the API, let's try to improve our usage by using some Groovy features. In this section, we will progressively improve our usage of the Twitter4J APIs by selectively using the features that Groovy provides. One of the most obvious features to use is closures.

A Groovier way to find friends

In the previous examples, we iterated over our friends and printed out their details. What if we were to provide a method that takes a closure to apply to each friend or follower? In this example, we add these methods to a script, along with a follow method, to follow another Twitter user. We can use the eachFollower or eachFriend methods to list our current connections:

followersList = { user ->
    TwitterFactory.singleton.getFollowersList(user,-1)
}
friendsList = { user ->
    TwitterFactory.singleton...