Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicholasren/1b079bf7610b41abb674 to your computer and use it in GitHub Desktop.
Save nicholasren/1b079bf7610b41abb674 to your computer and use it in GitHub Desktop.
scala the language matters training
//-------------------------------------------//
// function as parameter java implementation //
//-------------------------------------------//
class UerFetcher implement Callable<User> {
private String userId;
@Autowired
private UserService userService;
publiv User call() {
return userService.get(userId);
}
}
List<String> userIds = null;// List( "1001", "1003", "1005")
List<Future<User>> users = new ArrayList<Future<User>>()
for(id : userIds) {
users.add(executor.submit(new UserFetcher(id)))
}
//-------------------------------------------//
// function as parameter in scala //
//-------------------------------------------//
val userIds = List("1001", "1002", "1003", "1004")
val userFetcher: String => Future[User] = (id: String) => future { UserService.get(id) }
val users: List[Future[User]] = userIds.map(userFetcher)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment