Welcome to Snippets of Code, a programming language live journal. The current focus is on the Elixir programming language. Feel free to leave comments or suggestions!
Getting Started with LiveBook
If you work with Elixir, LiveBook is a must-have tool. It allows you to make collaborative and interactive code notebooks. The notebook is divided into cells, marked by either markdown for formatted text or Elixir for code.
Elixir cells are interactive code blocks that allow for testing and experimenting with Elixir code. The notebook displays the results of the code execution.
Executing the code within the Elixir cell evaluates it in LiveBook. You can trigger the evaluation of the cell by clicking on the "Run" or "Evaluate" button associated with it. This sends the code within the cell to the Elixir runtime, which processes and executes the code.
By evaluating an Elixir cell, you can see the output, errors, or side effects produced by the code. It enables you to test and verify the behavior of your code, experiment with different inputs, and observe the results in real time.
In LiveBook, you can format Elixir cells using Markdown syntax. Markdown allows you to add headings, code blocks, lists, emphasis, and other formatting elements to your cells.
To create a code block in a LiveBook Elixir cell, use triple backticks (\```) and specify the language as "elixir" to enable syntax highlighting.
defmodule MyModule do
def my_function do
IO.puts "Hello, LiveBook!"
end
end
Use hashtags (#) in your text to create headings of varying levels.
# Heading 1
## Heading 2
### Heading 3
Lists can be created using hyphens (-) and numbers. Ordered lists are numbered, while unordered lists are bulleted.
- Item 1
- Item 2
- Item 3
1. Item 1
2. Item 2
3. Item 3
One way to emphasize text is by using an asterisk (*) or underscores (_).
*Italic text*
_Italic text_
**Bold text**
__Bold text__
LiveBook allows you to improve the appearance and organization of your cells using Markdown syntax. You have a variety of options to choose from to make your cells more appealing.
Manipulating Markdown cells in LiveBook is straightforward. You can add, change, or remove content in several ways.
To add content in a Markdown cell, click on the cell and start typing. You can include headings, paragraphs, lists, code snippets, images, links, and other Markdown elements.
To edit the content of a Markdown cell, click inside the cell and make the necessary changes. You can change the text, add or remove Markdown formatting, and update the content according to your needs.
Do you have a lengthy Markdown cell that you want to divide into smaller ones? Place your cursor at the desired splitting point, and then select the "Split cell at cursor" button from the LiveBook toolbar. This will create two separate Markdown cells for you.
To merge many Markdown cells into one, you can select the cells by clicking and dragging over them or holding the Shift key and clicking on many cells. After selecting the cells, look for the "Merge selected cells" button in the toolbar (represented by two merging arrows) and click it. This will merge the selected cells into a single cell.
You can change the order of Markdown cells in your LiveBook document by clicking on a cell and dragging it to a new position. This feature allows you to rearrange the sequence of cells as needed.
To delete a Markdown cell, select the cell and click the "Delete selected cells" button (which looks like a trash bin) in the toolbar. The selected cell will then be removed.
Self-Code Review
Language List in Elixir on Exercism
The following code creates a module named "LanguageList". It contains functions that allow users to manipulate a list of programming languages.
defmodule LanguageList do
def new() do
[]
end
def add(list, language) do
[ language | list]
end
def remove(list) do
[ head | tail ] = list
tail
end
def first(list) do
List.first(list)
end
def count(list) do
length(list)
end
def functional_list?(list) do
cond do
hd(list) == "Elixir" -> true
tl(list) == [] -> false
true -> functional_list?(tl(list))
end
end
end
The new/0
function creates a new language list by returning an empty list [].
The add/2
function expects a list of languages and a new language as inputs. It will insert the new language at the beginning of the list and then return the updated list.
The remove/1
function requires a language list as input. It will delete the first language in the list and return the remaining items.
The first/1
function requires a language list as input. It then searches for and outputs the first language in the list.
The count/1
function requires a list of languages as input. It will then determine the total number of languages in the list and provide that number as output.
The functional_list?/1
function checks if a given language list meets a specific condition. It uses pattern matching and conditional statements (cond
) to determine if the list can be classified as a "functional" list. The function checks if the first element of the list is "Elixir."
The function will return `true` if the list is not empty. Yet, the function will return' false' if the list is empty ([]
). If neither condition is met, the function will continue to check with the tail of the list (excluding the first element). These functions provide basic operations to create, change, and inspect a list of programming languages. They can manage and manipulate language lists in an Elixir program.