Saturday, January 10, 2015

Scala Tutorial

What is Scala ?
  • Functional Programming (treats computation as the evaluation of mathematical functions and avoids state and mutable data)
  • Pure OO
  • Supports Java Library / Framework Ecosystem

Why Scala ?

  • It cuts down on boilerplate, so programmers can concentrate on the logic of their problems.
    To get data from url : Source.fromURL(url, "UTF-8").mkString
  • It protects existing investments by running on the Java Virtual Machine and interoperating seamlessly with Java.

Functional Programming

apply : Function is an Object with an apply method
Object Hello {
def apply(s:String) = "Hello" + world}
println (Hello("world"))
named : normal way of creating functions
def sum(x:Int,y:Int):Int =
{
  x+y
}
literal : Functions can be assigned to variable
val add_one = (x:Int) => x+1

add_one(3) // 4
// Turning existing functions into literals

val p = print _  // _ is something like we dont actually whatsoever value is
p("Hi")
// if you want to restrict your params 

val add_one:(Int) => Int = (x) => x + 1  // defines that it takes int and returns Int 
Partial functions : not all parameters info is provided
val add_one = sum(1,_:Int)
add_one(5) // gives 6
vall addition = (sum _).curried // applies arguments in a sequence
addition(5)(1)
Higher-order functions : functions that take functions as parameter
// twice(f, 7) = f(f(7)) = (7 + 3) + 3.
def f(x):
    return x + 3
 def twice(function, x):
    return function(function(x))
 print(twice(f, 7))
Pattern Matching :
def matchTest(x: Int): String = x match {
    case 1 => "one"
    case 2 => "two"
    case _ => "many"
  }

whats the big deal ? . we can do the same with switch in java 

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
Match on Class Instances
var play = sport("cricket")

play match{
  case sport("football") => println("im playing football")
  case sport("cricket") => println("im playing cricket")
  case sport(_) => println("im unsure")
}
Match on Regular Expressions
// val vs var

val cricket ="^c.*"
val football = "^f.*"

val sport = "cricket"

sport match{
 case cricket : pl("playing cricket")
 case football : pl("playing football")
 case _ : pl ("xxx")
}
Scala Closures


Object Oriented Programming

Object : object in scala is like the static portion of class in java.
Generics :
class Stack[T]
{
private val list = new List [T]
def pop : T ={ }
def peek : T = { }
}
Covariant +T   (Accepts any type that is sub class of T)
Contravariant -T (Accepts any type that is super class of T)

Apple Pay in Swift

Apple Pay lets you use iPhone 6 and Apple Watch to pay in stores and apps in an easy, secure, and private way."

Below method would be quick start for using this functionality in iOS8+ devices

/*  
Method to make apple pay
*/
func applePaymentProcess(){

if(PKPaymentAuthorizationViewController.canMakePayments()){

let pkRequest = PKPaymentRequest();
pkRequest.countryCode = "US"
pkRequest.currencyCode = "USD"
pkRequest.supportedNetworks = [PKPaymentNetworkAmex,PKPaymentNetworkMasterCard,PKPaymentNetworkVisa]
pkRequest.merchantCapabilities = PKMerchantCapability.CapabilityEMV
// pkRequest.merchantIdentifier =
let item1 = PKPaymentSummaryItem.init(label:"motoX", amount:34.5)
let item2 = PKPaymentSummaryItem.init(label:"motoG", amount:30.5)
pkRequest.paymentSummaryItems = [item1,item2]
var paymentPane = PKPaymentAuthorizationViewController.init(paymentRequest:pkRequest)
}
}

Git Hub Project : https://github.com/prashanthmadi/Apply-Pay-Sample-App---Swift