Simple tour of Scala -- monad
Different from Go which is created by IT giant Google, Scala is created by Martin Odersky in EPFL.
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
2003 - A drunken Martin Odersky sees a Reese's Peanut Butter Cup ad featuring somebody's peanut
butter getting on somebody else's chocolate and has an idea. He creates Scala, a language that
unifies constructs from both object oriented and functional languages. This pisses off both groups
and each promptly declares jihad.
Why Scala?
Scala ecosystem could provide fast data processing toolkit.
What’s Scala?
- Scala is object oriented language with concise syntax
- Scala is a functional language which is based on monad
What’s monad?
A monad is just a monoid in the category of endofunctors, what's the problem?
一个单子(Monad)说白了不过就是自函子范畴上的一个幺半群而已,有什么难以理解的。
--from <<Categories for the Working Mathematician>>
What’s monad’s meaning, in human’s language?
Take monad factor Option for example:
Suppose you need finish a function of sum two input integer:
def addInt(input):
if "a" in input and "b" in input:
a = input["a"]
b = input["b"]
try:
int(a)
try:
int(b)
return a+b
except:
print b + " is not int"
except:
print a + " is not int"
else:
print "a or b not in input parameters"
The function will have two types of outputs:
- If the inputs are two integers, then return the sum
- Else return nothing
For a simple function which may be Ok to have two types of output, if a system has a chain of hundreds function, the output types will be enormous, then the system will be impossible to test and manage.
How to fix the issue? Scala introduces the Option monad factor:
def addInt(requst:String):Option[Int]={
val inputParas = Json.parse(requst)
val a = (inputParas \ "a").asOpt[Int]
val b = (inputParas \ "b").asOpt[Int]
(a,b) match {
case (a1:Some[Int],b1:Some[Int])=>Some(a1.get +b1.get)
case _=>None
}
}
The Option factor combines the two results:
- If in good way, there will return Some[Int]
- If in bad way, there will return None
So there will have only one result type, then the system will have limited status. But, wait, the Some and None are still different types. Well, see the code as below:
The Option factor will combine Some[T] and None to Option[T] which could be used as both input and output, then the chain of functions will work in monad way:
What’s monad in picture?
Functors, Applicatives, And Monads In Pictures
What’re the monadic factors?
Option,Either, Future,Seq,and etc.