Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,3 @@
def repeat[A](n:Int)(f: => A)= ( 0 until n).foreach(_ => f)
repeat(3) { println("Example") }

View 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
}

View 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)
}