35 ms·
I'd like to see the Scala code too. I've played with both and in the end, both codes are pretty complex, just complex in different ways. It's a complex problem
by laureny 13y ago
I'd like to see the Scala code too. I've played with both and in the end, both codes are pretty complex, just complex in different ways.
It's a complex problem with a lot of edge cases and tricky error cases that need to be handled properly, which is where the elegance of the language or the programming paradigm usually goes down the drain.
I'd like to see a fully functional snippet of code that gracefully handles errors in both languages.
- noelwelsh 13y agofor { a <- doSomething1() b <- domSomething2() } yield { foo(a, b) } Use a Future + \/ monad.
- hythloday 13y agoOK. Something like: val fNotifications: Future[List[Notification]] = // get notifications from the database val fPosts: Future[List[Post]] = // get posts from a different database // If getting notifications fail, log the error and pretend there are no notifications val safeNotifications = fNotifications rescue { t: Throwable => log.error(t); Future(Nil) } // If getting posts fails, log the error and try retrieving stale posts from the cached post server val safePosts = fPosts rescue { t: Throwable => log.error(t) // If the cached post server fails too, just pretend there are no posts cachedPostsDatabase.getForCurrentUser rescue { t: Throwable => log.error(t); Future(Nil) } } val facebookPage: Future[Page] = for { notifications: List[Notification] <- safeNotifications posts: List[Post] <- safePosts } yield { // Code here executes when both/all outer futures are ready renderFBPage(notifications, posts) } // We can return the future or call more rescues on it or combine it with more futures is a thumbnail sketch of how you'd implement the facebook timeline in Scala if you were doing so. There are even better abstractions that I've started using (functional reactive programming specifically) but none of them come with the standard library, so I thought that was a bit unfair.