scala. inception

Download Scala. Inception

Post on 18-May-2015

342 views

Category:

Technology

0 download

Embed Size (px)

TRANSCRIPT

  • 1. SCALA.INCEPTION.@alexkorotkikh

2. WHY SCALA? 3. Not 4. expressiveJAVA IS... 5. Not 6. expressiveNo 7. lambdasJAVA IS... 8. Not 9. expressiveNo 10. lambdasNo 11. mixinsJAVA IS... 12. Not 13. expressiveNo 14. lambdasNo 15. mixinsNothing 16. fun 17. :(JAVA IS... 18. Not 19. expressiveNo 20. lambdasNo 21. mixinsNothing 22. fun 23. :(Poor 24. standard 25. libraryJAVA IS... 26. Not 27. expressiveNo 28. lambdasNo 29. mixinsNothing 30. fun 31. :(Poor 32. standard 33. libraryThats 34. why 35. we 36. have 37. Guava 38. and 39. Apache 40. CommonsJAVA IS... 41. Still 42. JVM 43. basedBUT SCALA IS... 44. Still 45. JVM 46. basedWorld 47. class 48. performanceBUT SCALA IS... 49. Still 50. JVM 51. basedWorld 52. class 53. performanceVariety 54. of 55. libraries 56. and 57. componentsBUT SCALA IS... 58. Still 59. JVM 60. basedWorld 61. class 62. performanceVariety 63. of 64. libraries 65. and 66. componentsExpressiveBUT SCALA IS... 67. Still 68. JVM 69. basedWorld 70. class 71. performanceVariety 72. of 73. libraries 74. and 75. componentsExpressiveAll 76. cool 77. things 78. youve 79. heard 80. aboutBUT SCALA IS... 81. Still 82. JVM 83. basedWorld 84. class 85. performanceVariety 86. of 87. libraries 88. and 89. componentsExpressiveAll 90. cool 91. things 92. youve 93. heard 94. aboutCompact 95. while 96. still 97. readable 98. codeBUT SCALA IS... 99. FIRST STEPSFIRST STEPS 100. ONCE UPON A TIME... 101. ONCE UPON A TIME...We 102. had 103. a 104. module 105. ONCE UPON A TIME...We 106. had 107. a 108. modulefull 109. of 110. magic 111. ONCE UPON A TIME...We 112. had 113. a 114. modulefull 115. of 116. magicThey 117. called 118. it 119. business 120. logic 121. DROOLS DECISION TABLEDo 122. not 123. ever 124. try 125. to 126. repeat 127. it 128. at 129. home 130. DROOLS DECISION TABLEDo 131. not 132. ever 133. try 134. to 135. repeat 136. it 137. at 138. homeWe 139. had 140. to 141. rewrite 142. it 143. 144. DROOLS DECISION TABLEDo 145. not 146. ever 147. try 148. to 149. repeat 150. it 151. at 152. homeWe 153. had 154. to 155. rewrite 156. it 157. for 158. the 159. sake 160. of 161. readability 162. and 163. testability 164. DROOLS DECISION TABLEDo 165. not 166. ever 167. try 168. to 169. repeat 170. it 171. at 172. homeWe 173. had 174. to 175. rewrite 176. it 177. for 178. the 179. sake 180. of 181. readability 182. and 183. testabilitySo 184. weve 185. decided 186. to 187. give 188. Scala 189. a 190. chance 191. FIRST FUN 192. trait Logging {val log: Logger = LoggerFactory getLogger getClass} 193. class ImportantLogic { self: { val log: Logger } =def doSomething() {log warn Something important}}trait Logging {val log: Logger = LoggerFactory getLogger getClass} 194. class ImportantLogic { self: { val log: Logger } =def doSomething() {log warn Something important}}trait Logging {val log: Logger = LoggerFactory getLogger getClass}val logic = new ImportantLogic with Logginglogic.doSomething() 195. class ImportantLogic { self: { val log: Logger } =def doSomething() {log warn Something important}}trait Logging {val log: Logger = LoggerFactory getLogger getClass}val logic = new ImportantLogic with Logginglogic.doSomething()val logic2 = new ImportantLogic // compile errorlogic2.doSomething() 196. (1 to 20) // [1, 20] 197. (1 to 20) // [1, 20].map(d = d * d) // 1, 4, 9, ..., 400 198. (1 to 20) // [1, 20].map(d = d * d) // 1, 4, 9, ..., 400.filter(_ % 3 == 0) // 9, 36, 81, ... 199. (1 to 20) // [1, 20].map(d = d * d) // 1, 4, 9, ..., 400.filter(_ % 3 == 0) // 9, 36, 81, ....foldLeft(0)(_ + _) // 819 200. val x: Int = ... 201. val x: Int = ...(1 to 20) // [1, 20] 202. val x: Int = ...(1 to 20) // [1, 20].find(_ == x) // Option[Int] 203. val x: Int = ...(1 to 20) // [1, 20].find(_ == x) // Option[Int].map(_ / 2) // Option[Int], still 204. val x: Int = ...(1 to 20) // [1, 20].find(_ == x) // Option[Int].map(_ / 2) // Option[Int], still.getOrElse(0) // x / 2 if 1 = x = 20 205. val x: Int = ...(1 to 20) // [1, 20].find(_ == x) // Option[Int].map(_ / 2) // Option[Int], still.getOrElse(0) // x / 2 if 1 = x = 20// 0 otherwise 206. def patternMatching(x: Any) = x match {} 207. def patternMatching(x: Any) = x match {}case 1 | 2 | 3 = one or two or three 208. def patternMatching(x: Any) = x match {}case 1 | 2 | 3 = one or two or threecase d: Double = double is here 209. def patternMatching(x: Any) = x match {}case 1 | 2 | 3 = one or two or threecase d: Double = double is herecase s: String if s.size5 = short str 210. def patternMatching(x: Any) = x match {}case 1 | 2 | 3 = one or two or threecase d: Double = double is herecase s: String if s.size5 = short strcase _ = anything else 211. Implicit 212. conversionsAND MUCH MORE 213. Implicit 214. conversionsParallel 215. collectionsAND MUCH MORE 216. Implicit 217. conversionsParallel 218. collectionsXML 219. as 220. built-in 221. data 222. typeAND MUCH MORE 223. Implicit 224. conversionsParallel 225. collectionsXML 226. as 227. built-in 228. data 229. typeSingleton 230. as 231. build-in 232. data 233. typeAND MUCH MORE 234. Implicit 235. conversionsParallel 236. collectionsXML 237. as 238. built-in 239. data 240. typeSingleton 241. as 242. build-in 243. data 244. typeMacros 245. (from 246. 2.10.x)AND MUCH MORE 247. FIRST FAILS 248. SOEASY... 249. Functional code is hard to read at first. 250. def sort(list: List[Int]): List[Int] =list match { case Nil = Nil case head :: tail =sort(tail.filter(_head)) :::head ::sort(tail.filter(_ = head))}Functional code is hard to read at first. 251. def sort(list: List[Int]): List[Int] =list match { case Nil = Nil case head :: tail =sort(tail.filter(_head)) :::head ::sort(tail.filter(_ = head))}Functional code is hard to read at first. 252. No backward compatibilities. 253. No backward compatibilities.Libraries compiled with 2.9.x compilermay not work with 2.10.x.And vice versa. 254. No backward compatibilities.Libraries compiled with 2.9.x compilermay not work with 2.10.x.And vice versa. 255. SUMMARY 256. All 257. new 258. features 259. are 260. written 261. in 262. ScalaWE 263. All 264. new 265. features 266. are 267. written 268. in 269. ScalaMore 270. then 271. 30% 272. of 273. production 274. codeWE 275. All 276. new 277. features 278. are 279. written 280. in 281. ScalaMore 282. then 283. 30% 284. of 285. production 286. codeWe 287. became 288. more 289. productiveWE 290. All 291. new 292. features 293. are 294. written 295. in 296. ScalaMore 297. then 298. 30% 299. of 300. production 301. codeWe 302. became 303. more 304. productiveAnd 305. we 306. enjoy 307. it 308. =)WE 309. SCALACompact, 310. expressive 311. and 312. readable 313. code 314. SCALACompact, 315. expressive 316. and 317. readable 318. code...ok, 319. readable 320. after 321. some 322. time 323. SCALACompact, 324. expressive 325. and 326. readable 327. code...ok, 328. readable 329. after 330. some 331. timeAll 332. the 333. power 334. of 335. JVM 336. and 337. Java 338. libs 339. SCALACompact, 340. expressive 341. and 342. readable 343. code...ok, 344. readable 345. after 346. some 347. timeAll 348. the 349. power 350. of 351. JVM 352. and 353. Java 354. libs...altogether 355. with 356. functional 357. approach 358. 359. Check 360. out 361. some 362. quick 363. tutorial 364. (e.g. 365. Twitter 366. Scala 367. School)YOU 368. Check 369. out 370. some 371. quick 372. tutorial 373. (e.g. 374. Twitter 375. Scala 376. School)Separate 377. a 378. feature 379. (you 380. dont 381. need 382. to 383. rewrite 384. whole 385. project 386. at 387. once!)YOU 388. Check 389. out 390. some 391. quick 392. tutorial 393. (e.g. 394. Twitter 395. Scala 396. School)Separate 397. a 398. feature 399. (you 400. dont 401. need 402. to 403. rewrite 404. whole 405. project 406. at 407. once!)Start 408. with 409. tests 410. (if 411. usually 412. you 413. dont)YOU 414. Check 415. out 416. some 417. quick 418. tutorial 419. (e.g. 420. Twitter 421. Scala 422. School)Separate 423. a 424. feature 425. (you 426. dont 427. need 428. to 429. rewrite 430. whole 431. project 432. at 433. once!)Start 434. with 435. tests 436. (if 437. usually 438. you 439. dont)Enjoy 440. itYOU 441. THANK YOU!@alexkorotkikh