Skip to content

Instantly share code, notes, and snippets.

View nikhilpatil's full-sized avatar

Nikhil nikhilpatil

  • @lavego.de
  • Munich, Germany
View GitHub Profile
@nikhilpatil
nikhilpatil / .bashsrc
Created June 5, 2019 07:33
Bash config to set JAVA_HOME in MacOS
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
launchctl setenv JAVA_HOME `/usr/libexec/java_home -v 1.8`
@nikhilpatil
nikhilpatil / SavingsInterestCalculator.scala
Last active January 31, 2017 05:41
A bank wants to introduce a new bank account with variable rates of interest depending on how much money is in the account
trait Account {
def interest: Int
}
object Account {
val slabs = List((1000, 1), (5000, 2), (Int.MaxValue, 3))
def apply(amount: Int): Account = new Account {
override def interest = {
def curry[A,B,C](f: (A, B) => C): A => (B => C) = {
(a:A) => (b:B) => f(a, b)
}
def uncurry[A,B,C](f: A => B => C): (A, B) => C = {
(a:A, b:B) => f(a)(b)
}
def compose[A,B,C](f: B => C, g: A => B): A => C = {
(a:A) => f(g(a))
@nikhilpatil
nikhilpatil / MyOption.scala
Created January 28, 2017 04:58
MyOption an isomorphic representation of scala’s native `Option`. Can you implement `MyOption`‘s unimplemented api so that `MyOption` behave similar to `Option`. You can only use `cata` method and the factory methods which are `MyOption.some` and `MyOption.none`
object MyOption {
def none[A] = new MyOption[A] {
def cata[X](s: A => X, n: => X) = n
override def toString = "none"
}
def some[A](a: A) = new MyOption[A] {
def cata[X](s: A => X, n: => X) = {
s(a)
}