Elixir
2011fragletMCP + fragletc
Elixir is a dynamic, functional language for building scalable and maintainable applications. Elixir runs on the Erlang VM, known for creating low-latency, distributed, and fault-tolerant systems. These capabilities and Elixir tooling allow developers to be productive in several domains, such as web development, embedded software, machine learning, data pipelines, and multimedia processing, across a wide range of industries.
Here is a peek:
iex> "Elixir" |> String.graphemes() |> Enum.frequencies()
%{"E" => 1, "i" => 2, "l" => 1, "r" => 1, "x" => 1}Platform features Scalability All Elixir code runs inside lightweight threads of execution (called processes) that are isolated and exchange information via messages:
Due to their lightweight nature, you can run hundreds of thousands of processes concurrently in the same machine, using all machine resources efficiently (vertical scaling). Processes may also communicate with other processes running on different machines to coordinate work across multiple nodes (horizontal scaling).
Hello World
#!/usr/bin/env elixir
import IO, only: [puts: 1]
# BEGIN_FRAGLET
puts("Hello World!")
# END_FRAGLETCoding Guide
Language Version
Elixir 1.x (runs on Erlang/OTP)
Execution Model
- Interpreted, runs directly from source
- Code executes at the top level
- Scripts use
.exsextension (executable scripts)
Key Characteristics
- Functional programming language
- Immutable data structures
- Pattern matching
- Pipe operator (
|>) - Case-sensitive
Fragment Authoring
Write normal Elixir code; your fragment becomes the script body. The file already imports IO.puts, so add modules, functions, or expressions as you normally would.
Available Packages
Standard Elixir library is available. No additional packages are pre-installed.
Common Patterns
- Print:
IO.puts("message")orIO.puts "message" - String interpolation:
"Total: #{count}" - Lists:
[1, 2, 3] - Maps:
%{key: "value"} - Pattern matching:
{a, b} = {1, 2} - Pipe operator:
list |> Enum.map(fn x -> x * 2 end) - Functions:
defmodule MyModule do ... end
Examples
# Simple output
IO.puts("Hello, World!")
# Function definition
greet = fn name -> "Hello, #{name}!" end
IO.puts(greet.("Alice"))
# List processing
numbers = [1, 2, 3, 4, 5]
squared = Enum.map(numbers, fn x -> x * x end)
sum = Enum.sum(squared)
IO.puts("Sum of squares: #{sum}")
# Using pipe operator
[1, 2, 3, 4, 5]
|> Enum.map(fn x -> x * x end)
|> Enum.sum()
|> IO.puts()
# Module example
defmodule Math do
def sum(numbers) do
Enum.sum(numbers)
end
end
IO.puts("Sum: #{Math.sum([1, 2, 3, 4, 5])}")Caveats
- Elixir uses immutable data structures
- Functions are first-class citizens
- Pattern matching is a core feature
- The pipe operator (
|>) is commonly used for data transformation
Fraglet Scripts
Echo Args
#!/usr/bin/env -S fragletc --vein=elixir
args = System.argv()
puts("Args: #{Enum.join(args, " ")}")Factorial
#!/usr/bin/env -S fragletc --vein=elixir
defmodule Math do
def fact(n), do: Enum.reduce(1..n, 1, &*/2)
end
IO.puts(Enum.join(Enum.map(1..5, &Math.fact/1), ","))Stdin Upper
#!/usr/bin/env -S fragletc --vein=elixir
input = IO.read(:stdio, :all)
puts(String.upcase(String.trim(input)))Test
#!/usr/bin/env -S fragletc --vein=elixir
puts("Hello World!")