This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def addThree(list: List[Int]): List[Int] = { | |
| var i = 0 | |
| var result = new ListBuffer[Int]() | |
| while (i < list.length) { | |
| result += list(i) + 3 | |
| i += 1 | |
| } | |
| result.toList | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def countSpaces(word: List[Char]): Int = { | |
| var i = 0 | |
| var count = 0 | |
| while (i <= word.length) { | |
| if (word(i) == ' ') { | |
| count += 1 | |
| } | |
| i += 1 | |
| } | |
| count |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait Functor[F[_]] { | |
| def map[A, B](fa: F[A])(f: A => B): F[B] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def buyCoffees(creditCard: CreditCard, n: Int): (List[Coffee], Charge) = { | |
| val purchases = (1 to n).map(i => buyCoffee(creditCard, i)) | |
| val (coffees, charges) = purchases.unzip | |
| val reducedCharge = charges.reduce((c1, c2) => | |
| Charge(c1.creditCard, c1.amount + c2.amount)) | |
| (coffees, reducedCharge) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Do you know this annoying feeling when you repeat a small function twice? Like | |
| val x = X.doSomeStuff.notWorthRefactoring.butAnnoying | |
| val y = Y.doSomeStuff.notWorthRefactoring.butAnnoying | |
| // Now I can replace it with this satisfying pattern | |
| val func = (_: String).doSomeStuff.notWorthRefactoring.butAnnoying | |
| val (x, y) = (X, Y).bimap(func, func) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package client | |
| import chandu0101.scalajs.react.components.elementalui._ | |
| import japgolly.scalajs.react.component.Scala.Unmounted | |
| import japgolly.scalajs.react.vdom.html_<^._ | |
| import japgolly.scalajs.react.{ BackendScope, Callback, ReactEventFromInput, ScalaComponent } | |
| import scala.scalajs.js | |
| import scala.scalajs.js.`|` |
NewerOlder