Skip to content

Instantly share code, notes, and snippets.

View DhavalDalal's full-sized avatar
🇮🇳
https://twitter.com/softwareartisan

Dhaval Dalal DhavalDalal

🇮🇳
https://twitter.com/softwareartisan
View GitHub Profile

Two-Factor Authentication using Future[T] (Scala)

  • Convert imperative scala code to functional-style using Future
  • Refer to Authentication.scala for this refactoring.
  • You are not allowed to change the signature of loginChores, feel free to do whatever you want with other functions.

Geographic Services (Scala)

Refer to GeographicService.scala. The code fetches Weather and nearby places information (both http calls to an end-point) and merges the 2 end-points responses to produce meaningful information as JSON string.

  • Part 1
    • Use Future’s flatMap to get the aggregate result.
    • Re-write the same using for-comprehension.
    • In each case strive to use minimal variables.
    • Measure time taken in each case and compare the results. You may use the time function provided therein.
  • Part 2
@DhavalDalal
DhavalDalal / Attention.md
Last active April 15, 2019 03:28
Echo TCP Server (Scala)

Echo TCP Server (Scala)

  • Echo TCP Server echos all that is sent to it, except for QUIT,
    • QUIT causes the server to close the connection with the client.
  • You can either run multiple telnet sessions (acting as clients) or run the ECHO TCP Client.
    • Run Server using > ./run.sh Server.scala
    • Run Client using > ./run.sh Client.scala
    • Run Telnet session using > telnet localhost 8080
    • Note your observations.
  • Can we make the Echo Server serve all the clients at the non-sequentially? In other words can we make Echo Server Concurrent?
@DhavalDalal
DhavalDalal / README.md
Last active April 15, 2019 03:29
Speed3 (Scala)

Speed3 (Scala)

  • Speed, distance and time are modelled using the following types:
    Distance, DistanceUnit, Time, TimeUnit and Speed
    
  • An end-point receives distance, distanceUnit, time and timeUnit as Strings and returns the calculated speed as JSON String.
    def speed(distance: String, distanceUnit: String, time: String, timeUnit: String): String = ???
    
@DhavalDalal
DhavalDalal / Attention.md
Last active April 18, 2019 13:14
Refactor to Try (Scala)

Two-Factor Authentication using Try[T] (Scala)

  • Convert imperative scala code to functional-style using Try
  • Refer to Authentication.scala for this refactoring.
  • You are not allowed to change the signature of loginChores, feel free to do whatever you want with other functions.
@DhavalDalal
DhavalDalal / Attention.md
Last active April 15, 2019 03:29
Try this or try that

Try this or try that (Scala)

  • authenticate attempts to login the user into the Application. It does so as defined below:
    • It first attempts to log the user using the application user name and password.
    • In case appLogin fails, it attempts to log the user using user's Gmail Id and password.
    • In case gmailLogin fails, it attempts to log the user using user's Facebook Id and password.
    • In case fbLogin fails, it gives up and does not authenticate the user.
  • Compare notes on your solutions after solving Part 1 and Part 2

Problem Part 1

@DhavalDalal
DhavalDalal / Attention.md
Last active April 15, 2019 03:30
Free 50 (Scala)

Free 50 (Scala)

  • Incoming Requests to various URLs of a web-service are modelled as:
    • Visits - A tuple containing Client IP and number of visits made from that IP.
    import java.net.InetAddress
    type Visits = (InetAddress, Int)
    
    • Request - An ADT containing the web-service URL with a list of clients that made the visits.
@DhavalDalal
DhavalDalal / README.md
Last active April 15, 2019 03:30
Speed2 (Scala)

Speed2 (Scala)

  • Speed, distance and time are modelled using the following types:
    Distance, DistanceUnit, Time, TimeUnit and Speed
    
  • An end-point receives distance, distanceUnit, time and timeUnit as Strings and returns the calculated speed as JSON String.
    def speed(distance: String, distanceUnit: String, time: String, timeUnit: String): String = ???
    
@DhavalDalal
DhavalDalal / README.md
Last active April 15, 2019 03:31
Speed

Speed (Scala)

  • Relationship between speed, distance and time is given as:
    speed = distance / time
    
  • An end-point receives distance and time as Strings and needs to return the calculated speed as JSON String.
    • For Success - speedInKmsPerHrs("100", "1.5") returns { 'speed' : '66.66666666666667 Kms/Hrs' }
    • For Failure - speedInKmsPerHrs("100", "1.s") returns { 'error' : 'Cannot Calculate' }
  • Provide implementation for the end-point def speedInKmsPerHrs(distanceInKms: String, timeInHrs: String)
@DhavalDalal
DhavalDalal / Attention.md
Last active April 15, 2019 03:32
Events Repository (Scala)

Event Repository (Scala)

  • What is the problem with the implementation of findById?

  • Provide implementation for findAnyByIds - If an event with an Id exists, return the event corresponding to that Id, else don’t return anything. In short, it returns events for Ids that exist.

    println(EventRepository.findAnyByIds(1,3,5,7)) 
    // List(Event(1,Some(Diwali),2018-11-12), Event(3,None,2019-03-20))
    
  • Provide implementation for findAllByIds - If an Id doesn’t exist, the result should be None else it should be Some with a list of all the events.