Learn functional programming basics in Java and Scala by implementing Twitter for galaxy far far away
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
project
src
.gitignore
README.md
build.sbt

README.md

Galactic Twitter

This project serves as a practical example of strongly-typed functional approach to creating simple DB-backed asynchronous service. There are two implementations: one in Java and one in Scala. You can use them to compare functional features in both languages.

The project is a foundation for my talk about functional programming: "Pragmatist's Guide to Functional Geekery". Slides can be found on Speaker Deck.

Background

The application simulates some of Twitter for a Star Wars based domain.

Followers functionality

Every Citizen may have 0 or more followers.

Tweet Wall functionality

Every Citizen is able to view Tweets on their Tweet Wall.

Tweet censorship functionality

Depending on who rules the galaxy, the tweets may or may not need to be censored and manipulated.

Running the applications

There are two two types of applications: console apps and HTTP server apps. All applications are implemented in both Java and Scala with exactly the same set of functionalities.

  • sbt "run-main com.michalplachta.galactic.java.http.ServerApp" [ServerApp.java] - HTTP server with /followers/{citizenName} and /tweets/{citizenName} endpoints (Java version)
  • sbt "run-main com.michalplachta.galactic.java.console.GalacticTwitterApp" [GalacticTwitterApp.java] - console-based Galactic Twitter (Java version)
  • sbt "run-main com.michalplachta.galactic.java.console.GalacticFollowersApp" [GalacticFollowersApp.java] - simplified console-based Galactic Twitter (displaying only followers) (Java version)
  • sbt "run-main com.michalplachta.galactic.http.ServerApp" [ServerApp.scala] - HTTP server with /followers/{citizenName} and /tweets/{citizenName} endpoints (Scala version)
  • sbt "run-main com.michalplachta.galactic.console.GalacticTwitterApp" [GalacticTwitterApp.scala] - console-based Galactic Twitter (Scala version)
  • sbt "run-main com.michalplachta.galactic.console.GalacticFollowersApp" [GalacticFollowersApp.scala] - simplified console-based Galactic Twitter (displaying only followers) (Scala version)

Showcased problems and approaches

Problem #1: Zero as "no value yet"

See comments in both FollowersService.java and FollowersService.scala files.

Problem #2: Not handling failures

See comments in both FollowersService.java and FollowersServiceService.scala files.

Problem #3: Cryptic return types

See comments in both FollowersService.java and FollowersServiceService.scala files.

Problem #4: Handling lists of Algebraic Data Types

See comments in both Followers.java and Followers.scala files.

Problem #5: Convoluted logic using IFs and vars

See comments in both TweetCensorship.java and TweetCensorship.scala files.

Problem #6: Writing tests by providing examples

See comments in FollowersTest.java.

Problem #7: Pure values tangled with JSON annotations

See the problem in RemoteData.java and potential solution using typeclasses in RemoteData.scala and RemoteDataJsonSupport.scala.