Last active
December 10, 2018 12:07
-
-
Save syedatifakhtar/46407faaf57f2515b876528271320fcf to your computer and use it in GitHub Desktop.
Functional Programming and Domain Modelling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.util.Try | |
println("Hello World") | |
//Functions as first class citizen | |
def someFunction = {a: Int=> a + 10} | |
val zz = someFunction | |
//Immutability | |
//val a = 10 | |
//a= a + 20 | |
//Composability | |
def squareFun = { x: Int => x * x } | |
val x = squareFun | |
def sumFunction(f: Int => Int, a: Int, b: Int): Int = { | |
(a to b).map(f).sum | |
} | |
sumFunction(squareFun, 1, 10) | |
def sumFunctionCurried(f: Int => Int)(a: Int, b: Int): Int = { | |
(a to b).map(f).sum | |
} | |
val sumSquaredFunction = sumFunctionCurried(squareFun)_ | |
val int = sumSquaredFunction(1,10) | |
//Even Higher | |
def combineFunction(combineOp: (Int,Int)=>Int,initialValue: Int)(f: Int=>Int)(a: Int, b: Int) = { | |
var result = initialValue // Oh noes?! | |
for(number<- a to b) { | |
result = combineOp(f(number),result) | |
} | |
result | |
} | |
def add = {(a: Int,b: Int)=> a + b} | |
def multiply = {(a: Int,b: Int)=> a*b} | |
def newSumFunction = combineFunction(add,0)_ | |
def cubes= {x: Int => x * x * x} | |
newSumFunction(cubes)(1,3) | |
def prodFunction = combineFunction(multiply,1)_ | |
prodFunction(x=>x)(1,4) | |
//val reserveProducts, blockPayment, isProductAvailable,hasReceivedPayment = { _: Any => true } | |
// | |
// | |
//trait Product {} | |
// | |
//case class Order(items: List[Product]) | |
// | |
//case class Gadget(name: String) extends Product | |
// | |
//case class HouseHoldItem(name: String) extends Product | |
// | |
//trait PaymentType | |
// | |
//case class CashPayment(amount: Int) extends PaymentType | |
// | |
//case class CardPayment(amount: Int) extends PaymentType | |
// | |
// | |
//case class OrderSummary(products: List[Product], paymentType: PaymentType) | |
// | |
////def processPurchase: List[Product] => PaymentType => Try[OrderSummary] = ??? | |
// | |
// | |
// | |
// | |
//val orderSummary = processPurchase(List(Gadget("Mobile")))(CashPayment(500)) | |
// | |
//def processPurchase: List[Product] => PaymentType => Try[OrderSummary] = { | |
// products => { | |
// paymentType => { | |
// Try { | |
// require(isProductAvailable(products)) | |
// reserveProducts(products) | |
// blockPayment(paymentType) | |
// hasReceivedPayment(paymentType) | |
// OrderSummary(products, paymentType) | |
// } | |
// } | |
// } | |
// | |
//} | |
// | |
// | |
// | |
// | |
////Higher Order | |
//def combineFunction(combineOp: (Int,Int)=>Int,startValue: Int)(f: Int=>Int)(a: Int, b: Int) = { | |
// var result = startValue // Oh noes?! | |
// for(number<- a to b) { | |
// result = combineOp(f(number),result) | |
// } | |
// result | |
//} | |
// | |
//def add = {(a: Int,b: Int)=> a + b} | |
//def multiply = {(a: Int,b: Int)=> a*b} | |
// | |
//def newSumFunction = combineFunction(add,0)_ | |
// | |
//newSumFunction(squareFun)(1,3) | |
// | |
//def prodFunction = combineFunction(multiply,1)_ | |
//prodFunction(x=>x)(1,4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment