Skip to content

Instantly share code, notes, and snippets.

@kirisakow
Last active May 29, 2023 15:55
Show Gist options
  • Save kirisakow/69e251071e594979756d11b1a8251229 to your computer and use it in GitHub Desktop.
Save kirisakow/69e251071e594979756d11b1a8251229 to your computer and use it in GitHub Desktop.
Run Scala in Google Colab with Almond
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"collapsed_sections": [],
"toc_visible": true,
"include_colab_link": true
},
"kernelspec": {
"display_name": "Scala",
"name": "scala"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/kirisakow/69e251071e594979756d11b1a8251229/run_scala_in_colab_with_almond.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Run Scala in Google Colab with Almond\n\n",
"### <u>**Deprecation warning:**</u> Google Colab interface has seemingly undergone changes and does not allow to use side kernels the way it used to be. I myself have stopped using Google Colab and have been using Docker images and containers instead. To run Scala in Jupyter Notebook as a Docker container, you can use this [guide of mine](https://github.com/kirisakow/scala-jupyter-container) based on `jupyter/all-spark-notebook` Docker image, the latest Almond and Scala. Happy coding!"
],
"metadata": {
"id": "tnRm0YwmdLhl"
}
},
{
"cell_type": "markdown",
"source": [
"## Important prerequisite 1 / 3\n",
"\n",
"Open your Colab Notebook with a text editor and make sure the `kernelspec` key is set to work with Scala, like so:\n",
"\n",
"```json\n",
"{\n",
" ⋮\n",
" \"kernelspec\": {\n",
" \"display_name\": \"Scala\",\n",
" \"name\": \"scala\"\n",
" }\n",
" ⋮\n",
"}\n",
"```"
],
"metadata": {
"id": "UMudsO4-dQ03"
}
},
{
"cell_type": "markdown",
"metadata": {
"id": "QVJoUDPtb9gX"
},
"source": [
"## Important prerequisite 2 / 3\n",
"\n",
"Run the cell below to [install the Almond kernel](https://almond.sh/docs/quick-start-install) into the global Jupyter kernels:"
]
},
{
"cell_type": "code",
"source": [
"! curl -sS -Lo coursier https://git.io/coursier-cli\n",
"! chmod +x coursier\n",
"! ./coursier launch --fork almond -- --install 1>/dev/null 2>&1\n",
"! rm -f coursier"
],
"metadata": {
"id": "j-1b2BcOm6py"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Important prerequisite 3 / 3\n",
"\n",
"Reload Google Colab page for Scala to activate."
],
"metadata": {
"id": "wyH-FiPgxfIL"
}
},
{
"cell_type": "markdown",
"source": [
"Now you can work in Scala:"
],
"metadata": {
"id": "5hDyl5WedYRK"
}
},
{
"cell_type": "code",
"source": [
"println(scala.util.Properties.versionString)"
],
"metadata": {
"outputId": "e6fd6754-a061-4138-a880-320c9a7af1c6",
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ujAul9CtDJ2A"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"version 2.13.8\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"for (day <- 1 to 16) {\n",
" if (0 < day % 7 && day % 7 < 6) println(f\"$day%02d: work day\")\n",
" else println(f\"$day%02d: week end!!!\")\n",
"}"
],
"metadata": {
"id": "yyo4HCFCwzWP",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "8eaebd8b-6b9a-4540-c95c-577cdaceb1e0"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"01: work day\n",
"02: work day\n",
"03: work day\n",
"04: work day\n",
"05: work day\n",
"06: week end!!!\n",
"07: week end!!!\n",
"08: work day\n",
"09: work day\n",
"10: work day\n",
"11: work day\n",
"12: work day\n",
"13: week end!!!\n",
"14: week end!!!\n",
"15: work day\n",
"16: work day\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import scala.language.postfixOps\n",
"\n",
"def sorter(a: Array[Int]): Array[Int] = {\n",
" if (a.length <= 1) a\n",
" else {\n",
" val pivot = a(a.length / 2)\n",
" Array.concat(\n",
" sorter(a filter (pivot >)),\n",
" a filter (pivot ==),\n",
" sorter(a filter (pivot <)),\n",
" )\n",
" }\n",
"}\n",
"\n",
"val arr = Array(3, 5, 6, 8, 2, 9, 7, 7)\n",
"val sorted_arr = sorter(arr)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Tb3ZeBrlDzIW",
"outputId": "f6c8835f-54dc-4671-d974-3335a1b7500c"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"\u001b[32mimport \u001b[39m\u001b[36mscala.language.postfixOps\n",
"\n",
"\u001b[39m\n",
"defined \u001b[32mfunction\u001b[39m \u001b[36msorter\u001b[39m\n",
"\u001b[36marr\u001b[39m: \u001b[32mArray\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mArray\u001b[39m(\u001b[32m3\u001b[39m, \u001b[32m5\u001b[39m, \u001b[32m6\u001b[39m, \u001b[32m8\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m9\u001b[39m, \u001b[32m7\u001b[39m, \u001b[32m7\u001b[39m)\n",
"\u001b[36msorted_arr\u001b[39m: \u001b[32mArray\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mArray\u001b[39m(\u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m, \u001b[32m5\u001b[39m, \u001b[32m6\u001b[39m, \u001b[32m7\u001b[39m, \u001b[32m7\u001b[39m, \u001b[32m8\u001b[39m, \u001b[32m9\u001b[39m)"
]
},
"metadata": {},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"source": [
"val l0 = List(1, 2, 3, 4)\n",
"val l1 = l0.foreach { case i => i + 10 }\n",
"val l2 = l0.map((i: Int) => i * 2)\n",
"val l3 = l0.filter((i: Int) => i % 2 == 0)\n",
"val l4 = l0.zip(l2)\n",
"val summ = l0.foldLeft(0) { (acc, num) => acc + num }"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "LTjSzesAHm2_",
"outputId": "d5206e19-918c-4256-8011-6cd35e52aa4e"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"\u001b[36ml0\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m, \u001b[32m4\u001b[39m)\n",
"\u001b[36ml2\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m2\u001b[39m, \u001b[32m4\u001b[39m, \u001b[32m6\u001b[39m, \u001b[32m8\u001b[39m)\n",
"\u001b[36ml3\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m2\u001b[39m, \u001b[32m4\u001b[39m)\n",
"\u001b[36ml4\u001b[39m: \u001b[32mList\u001b[39m[(\u001b[32mInt\u001b[39m, \u001b[32mInt\u001b[39m)] = \u001b[33mList\u001b[39m((\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m), (\u001b[32m2\u001b[39m, \u001b[32m4\u001b[39m), (\u001b[32m3\u001b[39m, \u001b[32m6\u001b[39m), (\u001b[32m4\u001b[39m, \u001b[32m8\u001b[39m))\n",
"\u001b[36msumm\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m10\u001b[39m"
]
},
"metadata": {},
"execution_count": 5
}
]
},
{
"cell_type": "code",
"source": [
"val names0 = Vector(\"Bob\", \"Fred\", \"Joe\", \"Julia\", \"Kim\")\n",
"val names1 = for (name <- names0 if name.startsWith(\"J\")) yield name"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1qw4nQm5Lgkk",
"outputId": "90ae0178-acdf-4bd5-8814-56635a752c7e"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"\u001b[36mnames0\u001b[39m: \u001b[32mVector\u001b[39m[\u001b[32mString\u001b[39m] = \u001b[33mVector\u001b[39m(\u001b[32m\"Bob\"\u001b[39m, \u001b[32m\"Fred\"\u001b[39m, \u001b[32m\"Joe\"\u001b[39m, \u001b[32m\"Julia\"\u001b[39m, \u001b[32m\"Kim\"\u001b[39m)\n",
"\u001b[36mnames1\u001b[39m: \u001b[32mVector\u001b[39m[\u001b[32mString\u001b[39m] = \u001b[33mVector\u001b[39m(\u001b[32m\"Joe\"\u001b[39m, \u001b[32m\"Julia\"\u001b[39m)"
]
},
"metadata": {},
"execution_count": 6
}
]
},
{
"cell_type": "code",
"source": [
"case class User(name: String, age: Int)\n",
"\n",
"val userBase = List(\n",
" User(\"Travis\", 28),\n",
" User(\"Kelly\", 33),\n",
" User(\"Jennifer\", 44),\n",
" User(\"Dennis\", 23)\n",
")\n",
"\n",
"val twentySomethings = for (user <- userBase if user.age >=20 && user.age < 30) yield user.name\n",
"\n",
"twentySomethings.foreach(println)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "f8DiIy2pNiu-",
"outputId": "872233aa-65f5-4c95-9d7e-3b6f2c3e1393"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Travis\n",
"Dennis\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"defined \u001b[32mclass\u001b[39m \u001b[36mUser\u001b[39m\n",
"\u001b[36muserBase\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mUser\u001b[39m] = \u001b[33mList\u001b[39m(\n",
" \u001b[33mUser\u001b[39m(name = \u001b[32m\"Travis\"\u001b[39m, age = \u001b[32m28\u001b[39m),\n",
" \u001b[33mUser\u001b[39m(name = \u001b[32m\"Kelly\"\u001b[39m, age = \u001b[32m33\u001b[39m),\n",
" \u001b[33mUser\u001b[39m(name = \u001b[32m\"Jennifer\"\u001b[39m, age = \u001b[32m44\u001b[39m),\n",
" \u001b[33mUser\u001b[39m(name = \u001b[32m\"Dennis\"\u001b[39m, age = \u001b[32m23\u001b[39m)\n",
")\n",
"\u001b[36mtwentySomethings\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mString\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m\"Travis\"\u001b[39m, \u001b[32m\"Dennis\"\u001b[39m)"
]
},
"metadata": {},
"execution_count": 7
}
]
},
{
"cell_type": "code",
"source": [
"val m = Map(\n",
" \"un\" -> 1,\n",
" \"deux\" -> 2,\n",
" \"trois\" -> 3,\n",
")\n",
"m.foreach {\n",
" case (cle, valeur) => println(s\"$cle — $valeur\")\n",
"}"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bMaPhdBTQQlj",
"outputId": "b5e8a26f-3b4c-4832-9174-eace7f638f6d"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"un — 1\n",
"deux — 2\n",
"trois — 3\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"\u001b[36mm\u001b[39m: \u001b[32mMap\u001b[39m[\u001b[32mString\u001b[39m, \u001b[32mInt\u001b[39m] = \u001b[33mMap\u001b[39m(\u001b[32m\"un\"\u001b[39m -> \u001b[32m1\u001b[39m, \u001b[32m\"deux\"\u001b[39m -> \u001b[32m2\u001b[39m, \u001b[32m\"trois\"\u001b[39m -> \u001b[32m3\u001b[39m)"
]
},
"metadata": {},
"execution_count": 8
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment