Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add scala-js support for fibonacci & loops #289

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions fibonacci/scala/base.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//> using scala 3.6.2
import scala.annotation.switch

object Fibonacci {
def run(number: String): Unit =
val u = number.toInt
var r = 0
var i = 0
while i < u do
r += fibonacci(i)
i += 1
println(r)

def fibonacci(n: Int): Int =
(n: @switch) match
case 0 => 0
case 1 => 1
case _ => fibonacci(n - 1) + fibonacci(n - 2)
}
20 changes: 2 additions & 18 deletions fibonacci/scala/code.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
//> using scala 3.6.2

package jvm

import scala.annotation.switch
//> using file base.scala

@main
def main(number: String): Unit =
val u = number.toInt
var r = 0
var i = 0
while i < u do
r += fibonacci(i)
i += 1
println(r)

def fibonacci(n: Int): Int =
(n: @switch) match
case 0 => 0
case 1 => 1
case _ => fibonacci(n - 1) + fibonacci(n - 2)
def main(number: String): Unit = Fibonacci.run(number)
18 changes: 18 additions & 0 deletions fibonacci/scala/codeJS.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//> using scala 3.6.2
//> using file base.scala

import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobal

@js.native
@JSGlobal("process")
object NodeProcess extends js.Object {
val argv: js.Array[String] = js.native
}

object MainJS {
def main(args: Array[String]): Unit = {
val params = NodeProcess.argv.drop(2).toArray
Fibonacci.run(params(0))
}
}
18 changes: 18 additions & 0 deletions loops/scala/base.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//> using scala 3.6.2

import java.util.Random

object Loop {
def run(number: String): Unit =
val u = number.toInt // Get an input number from the command line
val r = Random().nextInt(10000) // Get a random number 0 <= r < 10k
val a = new Array[Int](10000) // Array of 10k elements initialized to 0
var i, j = 0
while i < 10000 do // 10k outer loop iterations
while j < 100000 do // 100k inner loop iterations, per outer loop iteration
a(i) = a(i) + j % u // Simple sum
j += 1
a(i) += r // Add a random value to each element in array
i += 1
println(a(r)) // Print out a single element from the array
}
20 changes: 3 additions & 17 deletions loops/scala/code.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
//> using scala 3.6.2

package jvm

import java.util.Random
///> using scala 3.6.2
//> using file base.scala

@main
def main(number: String): Unit =
val u = number.toInt // Get an input number from the command line
val r = Random().nextInt(10000) // Get a random number 0 <= r < 10k
val a = new Array[Int](10000) // Array of 10k elements initialized to 0
var i, j = 0
while i < 10000 do // 10k outer loop iterations
while j < 100000 do // 100k inner loop iterations, per outer loop iteration
a(i) = a(i) + j % u // Simple sum
j += 1
a(i) += r // Add a random value to each element in array
i += 1
println(a(r)) // Print out a single element from the array
def main(number: String): Unit = Loop.run(number)
18 changes: 18 additions & 0 deletions loops/scala/codeJS.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//> using scala 3.6.2
//> using file base.scala

import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobal

@js.native
@JSGlobal("process")
object NodeProcess extends js.Object {
val argv: js.Array[String] = js.native
}

object MainJS {
def main(args: Array[String]): Unit = {
val params = NodeProcess.argv.drop(2).toArray
Loop.run(params(0))
}
}
Loading