Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Repeat/Scala/repeat-1.scala
Normal file
3
Task/Repeat/Scala/repeat-1.scala
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def repeat[A](n:Int)(f: => A)= ( 0 until n).foreach(_ => f)
|
||||
|
||||
repeat(3) { println("Example") }
|
||||
16
Task/Repeat/Scala/repeat-2.scala
Normal file
16
Task/Repeat/Scala/repeat-2.scala
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
object Repeat2 extends App {
|
||||
|
||||
implicit class IntWithTimes(x: Int) {
|
||||
def times[A](f: => A):Unit = {
|
||||
@tailrec
|
||||
def loop( current: Int): Unit =
|
||||
if (current > 0) {
|
||||
f
|
||||
loop(current - 1)
|
||||
}
|
||||
loop(x)
|
||||
}
|
||||
}
|
||||
|
||||
5 times println("ha") // Not recommended infix for 5.times(println("ha")) aka dot notation
|
||||
}
|
||||
18
Task/Repeat/Scala/repeat-3.scala
Normal file
18
Task/Repeat/Scala/repeat-3.scala
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import scala.annotation.tailrec
|
||||
|
||||
object Repeat3 extends App {
|
||||
|
||||
implicit class UnitWithNtimes(f: => Unit) {
|
||||
def *[A](n: Int): Unit = { // Symbol * used instead of literal method name
|
||||
@tailrec
|
||||
def loop(current: Int): Unit =
|
||||
if (current > 0) {
|
||||
f
|
||||
loop(current - 1)
|
||||
}
|
||||
loop(n)
|
||||
}
|
||||
}
|
||||
|
||||
print("ha") * 5 // * is the method, effective should be A.*(5)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue