Scala. Inception.

597 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
597
On SlideShare
0
From Embeds
0
Number of Embeds
73
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Scala. Inception.

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

×