Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MallikarjunaG/7dec36029d0d1d03911fd70c98777e5d to your computer and use it in GitHub Desktop.
Save MallikarjunaG/7dec36029d0d1d03911fd70c98777e5d to your computer and use it in GitHub Desktop.
01GettingStartedWithTheScalableLanguage
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Why Learn Scala?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Reason 1 ** —Your Code Will Be Better using functional programming techniques and reduce issues that arise from unintended side effects.By switching from mutable data structures to immutable data structures and from regular methods to pure functions that have no effect on their environment, your code will be safer, more stable, and much easier to comprehend. Your code will also be simpler and more expressive. If you currently work in a dynamic\n",
"language such as Python, Ruby, or JavaScript, you already are familiar with the benefits\n",
"of using a short, expressive syntax, avoiding unnecessary punctuation, and condensing\n",
"map, filter, and reduce operations to simple one-liners. If you are more familiar with\n",
"statically typed languages like Java, C#, or C++, you’ll be able to shed explicit types,\n",
"punctuation, and boilerplate code. You will also be able to pick up an expressive syntax\n",
"rarely seen in other compiled languages.\n",
"Finally, your code will be strongly typed (even without specifying explicit types) and\n",
"support both multiple inheritance and mixin capabilities. Also, any type incompatibilities\n",
"will be caught before your code ever runs. Developers in statically typed languages\n",
"will be familiar with the type safety and performance available in Scala. Those using\n",
"dynamic languages will be able to drastically increase safety and performance while\n",
"staying with an expressive language."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Reason 2 ** - You’ll Be a Better Engineer\n",
"An engineer who can write short and expressive code (as one expects in Ruby or Python) while also delivering a type-safe and high-performance application (as one expects from Java or C++) would be considered both impressive and valuable. Taking up a functional programming language like Scala will help to shape how you view the concepts of data mutability, higher-order functions, and side effects, not only as new ideas but how they apply to your current coding work and designs.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Intro"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Scala programming language has a wonderfully continental ring to its name, as\n",
"befits its origins at the École polytechnique fédérale de Lausanne (EPFL) in Lausanne,\n",
"Switzerland. The Scala logo represents a circular stairway, which may lead you to believe\n",
"its origin is the term La Scala, meaning a staircase or ladder in Italian, or that it derives\n",
"from the famous Italian opera house Teatro alla Scala. In fact the name Scala is an\n",
"abbreviation of the term SCAlable LAnguage, a fitting description of its intention. Professor\n",
"Martin Odersky and his group at EPFL created the language in 2003 to provide\n",
"a high-performance, concurrent-ready environment for functional programming and\n",
"object-oriented programming on the Java Virtual Machine (JVM) platform."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the below cell type println(\"Hello, World!\") and press Shift + Enter. The Kernel will run your println() command and print the output on a line below your command. \n",
"\n",
"Here is how the input and response should appear in the REPL:\n",
"\n",
" println(\"Hello, World\")\n",
" Hello, World\n",
"\n",
"The println() function, universally available to all Scala code, prints a message to the JVM’s stdout stream. When used in application servers that stdout stream is typically logged to a file"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, World\n"
]
}
],
"source": [
"println(\"Hello, World\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The println() function, universally available to all Scala code, prints a message to the JVM’s stdout stream. When used in application servers that stdout stream is typically logged to a file\n",
"\n",
"Let’s try performing a simple arithmetic operation. At a new prompt type 5 * 7 and press Shift + Enter."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[36mres1\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m35\u001b[39m"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5 * 7"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This time your Scala command did not print output, but instead returned a value, the product of 5 and 7. When your commands return (or are themselves) values, the REPL will assign them to a new, constant variable so that you can refer to the value in later operations. These “res” variables (a shortened version of “result,” perhaps) are sequentially numbered so that there will always be a unique container for your command’s result."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that res1 contains the output of the multiplication command, lets make use of it.\n",
"\n",
"Type 2 * res1 at a fresh prompt and press Return. You should see something like this:\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[36mres2\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m70\u001b[39m"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2 * res1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercises\n",
"\n",
"** 1. Although println() is a good way to print a string, can you find a way to print a string without println? Also, what kinds of numbers, strings, and other data does the REPL support?**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** Answer: **\n",
"\n",
"An alternate method to using `println()` is the `print()` command, which prints the given string without appending a newline. "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Prints a linePrints on the same line next to above line"
]
}
],
"source": [
"print(\"Prints a line\")\n",
"print(\"Prints on the same line next to above line\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** 2. When you're working in the REPL (Read-Eval-Print-Loop), you can also just enter your string on its own line. The \"Print\" functionality in the REPL will just print the given value. **\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Answer:**"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[36mres5\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"Hello, REPL\"\u001b[39m"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"Hello, REPL\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** 3. In the Scala REPL, convert the temperature value of 22.5 Centigrade to Fahrenheit. The conversion formula is cToF(x) = (x * 9/5) + 32. **"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** Answer:**\n",
"The standard math operators and precedences used in other mainstream programming languages also work in Scala. Thus you can be assured that multiplication and division operators take precedence over addition and subtraction operators."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[36mres6\u001b[39m: \u001b[32mDouble\u001b[39m = \u001b[32m72.5\u001b[39m"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"22.5 * 9 / 5 + 32"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** 4. Take the result from exercise 2, and convert it back to Centigrade. You can use the generated constant variable (e.g., “res0”) instead of copying and pasting the value yourself.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"** Answer:** \n",
"Using parentheses around the subtraction, which needs to occur before the multiplication and division, will give it highest precedence."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[36mres7\u001b[39m: \u001b[32mDouble\u001b[39m = \u001b[32m22.5\u001b[39m"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(res6 - 32) * 5 / 9"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Scala",
"language": "scala",
"name": "scala"
},
"language_info": {
"codemirror_mode": "text/x-scala",
"file_extension": ".scala",
"mimetype": "text/x-scala",
"name": "scala211",
"nbconvert_exporter": "script",
"pygments_lexer": "scala",
"version": "2.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment