Elixir is a dynamic, functional programming language that runs on the BEAM virtual machine, the same VM that powers Erlang. It inherits Erlang’s capabilities for building highly concurrent, distributed, and fault - tolerant systems.
Enum.map
instead of mutating the list in - place.list = [1, 2, 3]
new_list = Enum.map(list, fn x -> x * 2 end)
IO.inspect(new_list) # [2, 4, 6]
defmodule Greeter do
def start do
spawn(Greeter, :loop, [])
end
def loop do
receive do
{:greet, name} ->
IO.puts("Hello, #{name}!")
loop()
end
end
end
pid = Greeter.start()
send(pid, {:greet, "Alice"})
TypeScript is a statically typed superset of JavaScript. It adds types to JavaScript, which helps catch errors at compile - time rather than at runtime.
let message: string = "Hello, TypeScript!";
function add(a: number, b: number): number {
return a + b;
}
interface Person {
name: string;
age: number;
}
function greet(person: Person) {
return `Hello, ${person.name}! You are ${person.age} years old.`;
}
let alice: Person = { name: "Alice", age: 25 };
console.log(greet(alice));
# Create a new Phoenix application
mix phx.new my_app
cd my_app
mix ecto.create
mix phx.server
defmodule MyCSVParser do
alias NimbleCSV.RFC4180, as: CSV
def parse_csv(file_path) do
File.stream!(file_path)
|> CSV.parse_stream()
|> Enum.to_list()
end
end
data = MyCSVParser.parse_csv("data.csv")
import React from'react';
import ReactDOM from'react-dom';
interface Props {
name: string;
}
const Greeter: React.FC<Props> = ({ name }) => {
return <h1>Hello, {name}!</h1>;
};
ReactDOM.render(<Greeter name="TypeScript" />, document.getElementById('root'));
tsc
) and set up a tsconfig.json
file.// index.ts
import http from 'http';
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, TypeScript on Node.js!');
});
server.listen(3000, () => {
console.log('Server running on port 3000');
});
defmodule MyMath do
def add(a, b) do
a + b
end
end
defmodule MyMathTest do
use ExUnit.Case, async: true
test "add function" do
assert MyMath.add(2, 3) == 5
end
end
function multiply(a: number, b: number): number {
return a * b;
}
test('multiply function', () => {
expect(multiply(2, 3)).toBe(6);
});
try - catch
blocks sparingly. Instead, rely on pattern matching and returning {:ok, result}
or {:error, reason}
tuples.def read_file(file_path) do
case File.read(file_path) do
{:ok, content} ->
{:ok, content}
{:error, reason} ->
{:error, reason}
end
end
strict: true
in your tsconfig.json
file. This helps catch more errors at compile - time.any
type as much as possible, as it defeats the purpose of static typing.Elixir and TypeScript are both powerful programming languages, but they are suited for different use cases. Elixir shines in building concurrent, distributed, and fault - tolerant systems, especially in the backend and data processing domains. TypeScript, on the other hand, is a great choice for large - scale web applications, where static typing helps catch errors early and improves code maintainability. When choosing between the two, consider the requirements of your project, the existing technology stack, and the skills of your development team.