|
| 1 | +# Elixir Fraglet Guide |
| 2 | + |
| 3 | +## Language Version |
| 4 | +Elixir 1.x (runs on Erlang/OTP) |
| 5 | + |
| 6 | +## Execution Model |
| 7 | +- Interpreted, runs directly from source |
| 8 | +- Code executes at the top level |
| 9 | +- Scripts use `.exs` extension (executable scripts) |
| 10 | + |
| 11 | +## Key Characteristics |
| 12 | +- Functional programming language |
| 13 | +- Immutable data structures |
| 14 | +- Pattern matching |
| 15 | +- Pipe operator (`|>`) |
| 16 | +- Case-sensitive |
| 17 | +- Indentation is preserved based on the injection point |
| 18 | + |
| 19 | +## Fragment Authoring |
| 20 | +Write normal Elixir code; your fraglet becomes the script body. The file already imports `IO.puts`, so add modules, functions, or expressions as you normally would. |
| 21 | + |
| 22 | +## Available Packages |
| 23 | +Standard Elixir library is available. No additional packages are pre-installed. |
| 24 | + |
| 25 | +## Common Patterns |
| 26 | +- Print: `IO.puts("message")` or `IO.puts "message"` |
| 27 | +- String interpolation: `"Total: #{count}"` |
| 28 | +- Lists: `[1, 2, 3]` |
| 29 | +- Maps: `%{key: "value"}` |
| 30 | +- Pattern matching: `{a, b} = {1, 2}` |
| 31 | +- Pipe operator: `list |> Enum.map(fn x -> x * 2 end)` |
| 32 | +- Functions: `defmodule MyModule do ... end` |
| 33 | + |
| 34 | +## Examples |
| 35 | +```elixir |
| 36 | +# Simple output |
| 37 | +IO.puts("Hello, World!") |
| 38 | + |
| 39 | +# Function definition |
| 40 | +greet = fn name -> "Hello, #{name}!" end |
| 41 | +IO.puts(greet.("Alice")) |
| 42 | + |
| 43 | +# List processing |
| 44 | +numbers = [1, 2, 3, 4, 5] |
| 45 | +squared = Enum.map(numbers, fn x -> x * x end) |
| 46 | +sum = Enum.sum(squared) |
| 47 | +IO.puts("Sum of squares: #{sum}") |
| 48 | + |
| 49 | +# Using pipe operator |
| 50 | +[1, 2, 3, 4, 5] |
| 51 | +|> Enum.map(fn x -> x * x end) |
| 52 | +|> Enum.sum() |
| 53 | +|> IO.puts() |
| 54 | + |
| 55 | +# Module example (now possible with range-based injection) |
| 56 | +defmodule Math do |
| 57 | + def sum(numbers) do |
| 58 | + Enum.sum(numbers) |
| 59 | + end |
| 60 | +end |
| 61 | + |
| 62 | +IO.puts("Sum: #{Math.sum([1, 2, 3, 4, 5])}") |
| 63 | +``` |
| 64 | + |
| 65 | +## Caveats |
| 66 | +- Elixir uses immutable data structures |
| 67 | +- Functions are first-class citizens |
| 68 | +- Pattern matching is a core feature |
| 69 | +- The pipe operator (`|>`) is commonly used for data transformation |
| 70 | + |
0 commit comments