Skip to content

Instantly share code, notes, and snippets.

View chrisphelps's full-sized avatar

Chris Phelps chrisphelps

View GitHub Profile
@chrisphelps
chrisphelps / ForComprehensionWithMap.scala
Created January 2, 2016 19:23
For-comprehension with map
for {
config <- configServiceResponse
user <- userServiceResponse
prefs <- getPreferencesForUser(user, config)
items <- getItemsForUser(user).map{ item => personalizeItem(item, prefs) }
} yield format(items)
@chrisphelps
chrisphelps / NestedLFTransform.java
Created January 2, 2016 18:56
Use of a helper function to hide the nested transform
public ListenableFuture<List<Item>> getUserItemsWithConfig(User user, ListenableFuture<Config> configFuture) {
// transform on configFuture and call getItems(user, config)
...
}
public List<Item> getItems(User user, Config config) { ... }
ListenableFuture<User> user = getUser(args);
final ListenableFuture<Config> config = getConfig(args);
ListenableFuture<List<Item>> items = Futures.transform(user,
for {
config <- configServiceResponse
user <- userServiceResponse
items <- getItemsForUser(user)
prefs <- getPreferencesForUser(user, config)
personalized <- personalizeItems(items, prefs)
} yield format(personalized)
@chrisphelps
chrisphelps / ListenableFutureToScalaFuture.scala
Created December 30, 2015 06:45
Conversion from guava ListenableFuture to Scala Future
implicit def toScalaFuture[T](lFuture: ListenableFuture[T]): Future[T] = {
val p = Promise[T]
Futures.addCallback(lFuture,
new FutureCallback[T] {
def onSuccess(result: T) = p.success(result)
def onFailure(t: Throwable) = p.failure(t)
})
p.future
}
@chrisphelps
chrisphelps / BasicLF.java
Created December 30, 2015 06:04
Basic example - get a user and look up items for him
ListenableFuture<User> user = getUser(args);
ListenableFuture<List<Item>> items = Futures.transform(user,
new AsyncFunction<User, List<Item>>() {
@Override
public ListenableFuture<List<Item>> apply(User user) throws Exception {
return getItemsForUser(user);
}
});
@chrisphelps
chrisphelps / keybase.md
Created November 19, 2014 16:39
keybase.md

Keybase proof

I hereby claim:

  • I am chrisphelps on github.
  • I am cjphelps (https://keybase.io/cjphelps) on keybase.
  • I have a public key whose fingerprint is A077 D641 82C2 94A3 588C 44AE 585C 6AF1 721B B82C

To claim this, I am signing this object:

@chrisphelps
chrisphelps / implicit args
Last active August 29, 2015 14:04
Implicit arguments for mocking
class Foo (name: String) {
def apply() = "Foo " + name + "results"
}
class Bar(name: String) {
def apply()(implicit f: Foo) = "Bar" + name + "results: " + f()
}
class Baz {
implicit val myfoo = new Foo("production")
@chrisphelps
chrisphelps / gist:abd303948531b14f3ae4
Created June 20, 2014 09:13
Desugar the for-comprehension
f1 flatMap(a => for (b <- f2; c <- f3(b)) yield c * a)
f1 flatMap(a => f2 flatMap(b => for (c <- f3(b)) yield c * a))
f1 flatMap(a => f2 flatMap(b => f3(b) map(c => c * a)))
@chrisphelps
chrisphelps / gist:419201cc30b3de9e56aa
Last active August 29, 2015 14:02
For comprehension over futures
val f1 = future { 5 }
val f2 = future { "str" }
val f3 = (s: String) => future { s + "vap" }
val f4 = for {
a <- f1
b <- f2
c <- f3(b)
} yield c * a