Skip to content

Instantly share code, notes, and snippets.

@xiaoyunyang
Last active January 20, 2018 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaoyunyang/fade07ac705ecd8aae63efea58daea24 to your computer and use it in GitHub Desktop.
Save xiaoyunyang/fade07ac705ecd8aae63efea58daea24 to your computer and use it in GitHub Desktop.
Functional Programming in Scala
//Currying is when you break down a function that takes multiple arguments into a //series of functions that take part of the arguments.
//converts a function f of two arguments into a function of one argument that partially applies f.
def curry[A,B,C](f: (A,B) => C): A => (B => C) =
(a: A) => (b: B) => f(a,b)
//> curry: [A, B, C](f: (A, B) => C)A => (B => C)
//in javascript
//A loop - prints "1 2 3" to console
var res = 0
let nums = [1, 2, 3]
for(i=0; i<nums.length; i++) {
res = res + nums[i]
}
//Another loop - Performs 1+2+3 then print out sum
var res = ""
let strs = ["a", "b", "c"]
for(i=0; i< strs.length; i++) {
res = res + strs[i]
}
[1,2,3].reduce((a,b) => (a+b), 0) //> 6
["a", "b", "c"].reduce((a,b) => a+b, "") //> "abc"
const myReduce = (arr, initVal) =>
arr.reduce((a,b) => (a+b), initVal)
let arr1 = [1,2,3]
let arr2 = ["a", "b", "c"]
myReduce(arr1, 0) //> 6
myReduce(arr2, "") //>"abc"
let arr3 = [arr1, arr2]
myReduce(arr3, [])
let arr3 = [arr1, arr2]
arr3.reduce((a,b) => a.concat(b), [])
//In Scala
List(1,2,3) ++ List("a", "b", "c") //> res: List[Any] = List(1, 2, 3, a, b, c)
List(1,2,3) ++ "abc" //> res: List[AnyVal] = List(1, 2, 3, a, b, c)
"abc" ++ List(1,2,3) //res: scala.collection.immutable.IndexedSeq[AnyVal] = Vector(a, b, c, 1, 2, 3)
"abc" ++ "def" //> res: String = abcdef
"abc" + "def" //> res: String = abcdef
//In javascript
let arr1 = [""]
let arr2 = ["", ""]
let arr3 = ["", "", "a"]
let res1 = arr1.reduce((a,b) => a+b) //> ""
let res2 = arr2.reduce((a,b) => a+b) //> ""
let res3 = arr3.reduce((a,b) => a+b) //> "a"
const strLen = arr =>
arr.reduce((a,b) => a+b).length
strLen([res1]) //> 0
strLen([res1, res2]) //> 0
strLen([res1, res2, res3]) //> 1
strLen([res3, res2, res1]) //> 1
def findAve(l: List[Double]): Double = l.reduce(_ + _)/l.length
findAve(List(1,2,3))
//In javascript
let num = "7" //user inputs "7" for num
let res = num - 1 //> 6 ... good
let num = "07" //user inputs "07" for num
res = num - 1 //>6 ... still good
let num = "apple" - 1 //> NaN
res = num - 1 //> NaN ... bad
def findAve(l: List[Double]) = l.reduce(_ + _)/l.length
//> findAve: (l: List[Double])Double
//In javascript
let findAve = arr => arr.reduce((a,b) => a+b)/arr.length
findAve([1,2,3]) //> 2
findAve([1,1,3]) //>1.6666666666666667
//In Scala
def findAve(l: List[Int]) = l.reduce(_ + _)/l.length
//> findAve: (l: List[Int])Int
findAve(List(1,2,3)) //> 2
findAve(List(1,1,3)) //> 1
def findAve2(l: List[Int]): Double = l.reduce(_ + _)/l.length
findAve2(List(1,1,3)) //> 1.0
def findAve3(l: List[Double]): Double = l.reduce(_ + _)/l.length
findAve: (l: List[Double])Double
findAve(List(1,1,3)) //>1.6666666666666667
//In Java
double[] arr = new double[] {1,2,3};
double acc = 0;
for(int i=0; i<arr.length; i++) {
acc += arr[i];
}
double ave = acc/arr.length;
System.out.println("The average of " + Arrays.toString(arr) + " is "+ave);
//> The average of [1.0, 2.0, 3.0] is 2.0
//In Scala
def list = List(1.0, 2.0 ,3.0)
def findAve(l: List[Double]): Double = l.reduce(_ + _)/l.length
findAve(List(1,2,3))
println(s"The average of $list is $ave")
//> The average of List(1.0, 2.0, 3.0) is 2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment