Elixir
IO.puts "Hola, mundo en Elixir!!"
Sitios
defmodule Math do | |
def sum(a, b) do | |
a + b | |
end | |
end |
defmodule Math do | |
def sum(a, b) do | |
a + b | |
end | |
end | |
IO.puts Math.sum(1, 2) |
defmodule Math do | |
def sum(a, b) do | |
a + b | |
end | |
defp do_sum(a, b) do | |
a + b | |
end | |
end | |
IO.puts Math.sum(1, 2) | |
IO.puts Math.do_sum(1, 2) |
defmodule Math do | |
def div(_, 0) do | |
{:error, "Cannot divide by zero"} | |
end | |
def div(x, y) do | |
{ :ok, "value is #{x/y}" } | |
end | |
end | |
IO.inspect Math.div(1, 0) | |
IO.inspect Math.div(5, 3) |
defmodule Math do | |
def zero?(0), do: true | |
def zero?(x) when is_integer(x), do: false | |
def zero?(_), do: "Cannot calculate for non-integers" | |
end | |
IO.puts Math.zero?(0) | |
IO.puts Math.zero?(5) | |
IO.puts Math.zero?("String") |
defmodule Concat do | |
def join(a, b, sep \\ " ") do | |
a <> sep <> b | |
end | |
end | |
IO.puts Concat.join("Hello", "World") | |
IO.puts Concat.join("Hello", "World", "_") |
defmodule PipeTest do | |
def square(x), do: x * x | |
def sum(l, start \\ 0) do | |
start + Enum.sum(l) | |
end | |
# tl | |
def sst(the_list) do | |
the_list | |
|> tl | |
# |> IO.inspect | |
|> sum(2) | |
# |> IO.inspect | |
|> square | |
end | |
end | |
IO.puts PipeTest.sst([1, 2, 5]) |
defmodule Math do | |
[1, 2, 5] | |
# 0 acc | |
# 1 [2, 5] | |
# 1 acc | |
# 2 [ 5] | |
# 3 acc | |
# 5 [] | |
# 8 | |
def sum_list([], acc), do: acc | |
def sum_list([head | tail], acc) do | |
sum_list(tail, acc+head) | |
end | |
end | |
IO.puts Math.sum_list([1, 2, 5], 0) | |
IO.puts Math.sum_list([], 0) |
defmodule User do | |
defstruct name: "John", age: 29 | |
end | |
defmodule Test do | |
require User | |
def main do | |
john = %User{} | |
IO.inspect john | |
IO.puts "Another one" | |
jane = %User{name: "Jane"} | |
IO.inspect jane | |
jane = %{jane | age: 31 } | |
IO.inspect jane | |
IO.puts "Jane's age is: #{jane.age}" | |
# IO.puts "Occupation" | |
# jane = %{jane | occ: "Lawyer"} | |
# IO.inspect jane | |
end | |
end | |
Test.main |
defmodule Server do | |
def listen do | |
receive do | |
{:ok, :ping} -> IO.puts "Pong" | |
{:ok, :ding} -> IO.puts "Dong" | |
end | |
listen | |
end | |
end |
# definimos lista | |
lista = [1,2,3,4] | |
# con Enum | |
IO.puts(["Mi lista: ", Enum.join(lista, " ")]) | |
# con inspect | |
IO.puts("Mi listas: #{inspect(list)}") |
defmodule Mate do | |
def resta(x,y) do | |
restando(x,y) | |
end | |
defp restando(x,y) do | |
x-y | |
end | |
end | |
n=9 | |
m=3 | |
resta = Mate.resta(n.m) | |
IO.puts "Resta = #{resta}" |
Elixir
IO.puts "Hola, mundo en Elixir!!"
Sitios
# Entendiendo Atoms | |
# $ elixir atoms.exs | |
if :hello == :hello do | |
IO.puts "Esto es verdadero" | |
else | |
IO.puts "Esto no es verdadero" | |
end |
# Cadenas en elixir | |
cadena = "elixir como si estuviera en primero." | |
IO.puts "\t [Cadenas en Elixir]" | |
IO.puts "Capitalize: #{String.capitalize(cadena)}" | |
IO.puts "Longitud: #{String.length(cadena)}" | |
codePoints = String.codepoints(cadena) | |
IO.puts "Codepoints: #{codePoints}" | |
IO.puts "Codepoints: #{String.codepoints("GERT")}" | |
IO.puts "Lista: #{String.to_charlist(cadena)}" | |
IO.puts String.split("foo bar", " ") | |
IO.puts String.split(cadena, " ") | |
IO.puts "#{String.upcase(cadena)}" | |
IO.puts "#{String.downcase(cadena)}" | |
saludo = " Este es un saludo espacial. " | |
IO.puts "#{String.trim(saludo)}" | |
if String.contains?(cadena, "primero") do | |
IO.puts "Contiene la palabra." | |
else | |
IO.puts "No contiene la palabra." | |
end | |
IO.puts "#{String.at(cadena, 0)}" | |
IO.puts "#{String.at(cadena, 1)}" | |
IO.puts "#{String.at(cadena, 2)}" | |
IO.puts "#{String.at(cadena, 3)}" | |
IO.puts "#{String.at(cadena, 4)}" | |
IO.puts "#{String.at(cadena, 5)}" | |
defmodule Hola do | |
def msg(nombre) do | |
IO.puts "Hola, #{nombre}" | |
end | |
end | |
Hola.msg("Fernando") | |
nombre="Juana" | |
Hola.msg(nombre) | |
# $ elixir cadenas.exs | |
# Ver: https://hexdocs.pm/elixir/String.html |
case :hello do | |
:world -> | |
IO.puts "Mundo" | |
:hello -> | |
IO.puts "Hola, mundo" | |
_ -> | |
IO.puts "Esto no se vera" | |
end | |
opcion=2 | |
case opcion do | |
1 -> | |
IO.puts "Alef" | |
400 -> | |
IO.puts "Tav" | |
_ -> | |
IO.puts "Bet" | |
end | |
cond do | |
8+2 == 13 -> | |
IO.puts "Alfa" | |
true == true -> | |
IO.puts "Beta" | |
true -> | |
IO.puts "Gamma" | |
end | |
case {1,2,3} do | |
{1, 2, x} -> | |
IO.puts "El valor es: #{x}" | |
_ -> | |
IO.puts "Lo sentimos, pero no aplica el caso" | |
end | |
opcion=5 | |
cond do | |
opcion == 5 -> IO.puts "A" | |
opcion == 6 -> IO.puts "X" | |
true -> IO.puts "Z" | |
end | |
IO.puts "Fin" | |
#opcion=5 --> A |
#Condicionales en Elixir | |
if is_float(12.3) do | |
IO.puts "Es flotante" | |
else | |
IO.puts "No es flotante" | |
end | |
if is_integer(11.44) do | |
IO.puts "Es integer" | |
else | |
IO.puts "No es integer" | |
end | |
if Float.round(12.555, 2) == 12.55 do | |
IO.puts "Cumple la condicion" | |
else | |
IO.puts "No cumple la condicion" | |
end | |
if (round(12.555 * 100) / 100) == 12.56 do | |
IO.puts "X" | |
else | |
IO.puts "Y" | |
end | |
if rem(120, 100) == 20 do | |
IO.puts "20" | |
else | |
IO.puts "Nada" | |
end |
$ iex | |
> "Hola, mundo" | |
> 45 + 5 | |
> 32 - 4 | |
> 34 / 3 | |
> rem(120,100) | |
> true and true | |
> false or true | |
> true or true | |
> false and false | |
> false or false | |
> h() # Ayuda | |
> defmodule Hola do | |
> def msg(nombre) do | |
> IO.puts "Hola, #{nombre}" | |
> end | |
> end | |
> suma = fn(x,y)-> x+y end | |
> suma.(3,4) # 7 | |
> resta = &(&1-&2) | |
> resta.(3,2) # 1 | |
> |
# Enum y listas | |
lista = Enum.to_list 1..10 | |
IO.puts "\t [ Enumeraciones y listas ]" | |
IO.puts "#{lista}" | |
if Enum.all?(lista, fn x -> rem(x, 2) == 0 end) do | |
IO.puts "Alfa" | |
else | |
IO.puts "Omega" | |
end |
IO.puts "*** Fundamentos de Elixir ***" | |
numero = 11 | |
titulo = "El generalisimo General Don Blanco." | |
lista = [1,2,3,4,5] | |
IO.puts length(lista) | |
IO.inspect {:list, lista} | |
tupla = {1,:ok,:hello,true, 65,[1,2,3],false,"ABC"} | |
IO.puts elem(tupla,0) # 1 | |
IO.puts elem(tupla,3) # true | |
tam = tuple_size(tupla) | |
IO.puts "Tamaño: #{tam}" | |
verdadero = true | |
real=45.3 | |
mapa = %{:ok => 1, :hello => 2, :a => 3} | |
IO.puts mapa.ok | |
IO.puts mapa.a | |
IO.puts "Numero: #{numero}" | |
IO.puts "Titulo: #{titulo}" | |
IO.puts "Lista: #{lista}" | |
IO.puts "Verdadero: #{verdadero}" | |
IO.puts "Real: #{real}" | |
IO.puts div(10,5) | |
IO.puts rem(120,100) | |
IO.puts 12>10 || false | |
IO.puts true && false | |
IO.puts !(34 > 100) | |
IO.puts round(3.58) | |
IO.puts trunc(3.58) | |
IO.puts "**************************************" | |
if true do | |
IO.puts "TRUE" | |
end | |
IO.puts "**************************************" | |
if nil do | |
IO.puts "X" | |
else | |
IO.puts "Y" | |
end | |
IO.puts "**************************************" | |
Enum.each(0..2, fn(x) -> | |
IO.puts "Hola no. #{x}" | |
end) | |
opc=2 | |
case opc do | |
1 -> IO.puts "A" | |
2 -> IO.puts "B" | |
_ -> IO.puts "Default" | |
end | |
[head|tail] = [1,2,3,4,5,6] | |
IO.puts "Head: #{head}" | |
IO.puts "Tail: #{tail}" | |
if is_list('ABC') do | |
IO.puts "Es una lista." # true | |
else | |
IO.puts "No es una lista." | |
end | |
map = %{'A' => 'U', 'C' => 'G', 'T' => 'A', 'G' => 'C'} | |
Enum.map('ACTG', fn c -> map[[c]] end) | |
for c <- 'ACTG', do: map[[c]] | |
# Módulo | |
defmodule Hola do | |
def suma(x,y) do | |
x+y | |
end | |
def saludo do | |
"Hola, mundo en Elixir!!" | |
end | |
end | |
IO.puts Hola.saludo() | |
IO.puts Hola.suma(3,5) | |
defmodule Mate do | |
def sumar(x,y) do | |
sumando(x,y) | |
end | |
# funcion private | |
defp sumando(x,y) do | |
x+y | |
end | |
end | |
mateSum = Mate.sumar(4,5) | |
IO.puts("La suma es #{mateSum}") | |
## | |
# Funciones anonimas | |
# 1era forma | |
suma = fn(x,y)-> x+y end | |
IO.puts suma.(3,6) | |
# 2da forma | |
resta =&(&1-&2) | |
IO.inspect resta.(12,8) | |
# modulos | |
defmodule HolaTwo do | |
def msg(nombre) do | |
IO.puts "Hola, #{nombre}" | |
end | |
end | |
nombre="Mariana" | |
HolaTwo.msg("Fernando") | |
HolaTwo.msg(nombre) | |
# listas clave-valor | |
lista_llave_valor = [nombre: "Fernando", blog: "alquimista", url: "alquimistadecodigo.com"] | |
# Esto es un comentario | |
IO.puts "Hola, mundo en Elixir!!" |
# Mapas en Elixir | |
letras = %{:alef => 1, :bet => 2, :guimel => 3, :dalet => 4} | |
nombres = %{:juan => "JN",:ana => "AN",:maria => "MR",:jose => "JS"} | |
mapa = %{:a => 1, 2 => :b} | |
IO.puts "#{mapa[:a]}" | |
IO.puts "#{mapa[2]}" | |
IO.puts "#{letras[:alef]}" | |
IO.puts "#{nombres[:maria]}" |
# Esto es un comentario | |
a=3 | |
b=20 | |
if a>b do | |
IO.puts "#{a} es mayor a #{b}" | |
else | |
IO.puts "#{b} es mayor a #{a}" | |
end |
defmodule Hola do | |
def mundo do | |
IO.puts "Hola, mundo en una funcion Elixir!!" | |
end | |
def saludar(nombre) do | |
IO.puts "Hola, #{nombre}" | |
end | |
end | |
Hola.mundo | |
Hola.saludar "Fernando" |
# Operaciones en Elixir | |
defmodule MyMath do | |
def residuo(x, y) do | |
rem(x, y) | |
end | |
def dividir(x, y) do | |
if y == 0 do | |
0.0 | |
else | |
div(x, y) | |
end | |
end | |
def redondear(x) do | |
round(x) | |
end | |
defp trunquear(x) do | |
trunc(x) | |
end | |
def my_trunc(x) do | |
trunquear(x) | |
end | |
end | |
IO.puts "\t ===== Operaciones en Elixir =====" | |
IO.puts "Residuo de 200 % 200 nos da: #{MyMath.residuo(220,200)}" | |
IO.puts "Residuo de 345 % 120 nos da: #{MyMath.residuo(345,120)}" | |
IO.puts "Division de 23 / 0 nos da: #{MyMath.dividir(23,0)}" | |
IO.puts "Division de 189 / 32 nos da: #{MyMath.dividir(189,32)}" | |
IO.puts "El redondeo de 23.54409 nos da : #{MyMath.redondear(23.54409)}" | |
IO.puts "El redondeo de -8.980322 nos da : #{MyMath.redondear(-8.980322)}" | |
x = 8 | |
y = 7 | |
resultado = MyMath.dividir(x,y) | |
IO.puts "La division de #{x} / #{y} nos da: #{resultado}" | |
z = 23.954545 | |
IO.puts "Redondeo de #{z} nos da #{MyMath.my_trunc(z)}" | |
IO.puts "\t For loop en Elixir" | |
for n <- 1..5 do | |
IO.puts "n = #{n}" | |
end | |
IO.puts "\n" | |
for n <- [1, 2, 3, 4, 5] do | |
IO.puts "n = #{n}" | |
end | |
IO.puts "\n" | |
for << n <- "ABCDEFG" >> do | |
IO.puts "n = #{n}" | |
end | |
IO.puts "\n" | |
names = ~w[James John Patricia] | |
surnames = ~w[Johnson Smith Williams] | |
for name <- names, | |
surname <- surnames do | |
IO.puts "Nombre >> #{name} #{surname}" | |
end | |
IO.puts "\n" | |
people = [ | |
%{name: "John", active: true}, | |
%{name: "Patricia", active: false} | |
] | |
for %{active: true, name: name} <- people do | |
IO.puts "Nombre >> #{name}" | |
end | |
IO.puts "\n" | |
people = [ | |
%{name: "John", active: true}, | |
%{name: "Patricia", active: false} | |
] | |
for %{active: active, name: name} when active == true <- people do | |
IO.puts "Nombre >> #{name}" | |
end | |
IO.puts "\n " | |
people = [ | |
%{name: "John", active: true, age: 30}, | |
%{name: "Patricia", active: false, age: 45} | |
] | |
for person <- people, | |
person.age > 40 do | |
IO.puts "Nombre >> #{person.name}" | |
end | |
IO.puts "\n" | |
for person <- people, | |
name = person.name do | |
IO.puts "Nombre >> #{name}" | |
end | |
IO.puts "\n" | |
#for person <- people, | |
# %{wrong_name: name} = person do | |
# IO.puts "Nombre >> #{name}" | |
#end | |
IO.puts "\n" | |
for person <- people, | |
name = person.name, | |
active = person.active do | |
IO.puts "#{name} status is: #{active}" | |
end | |
IO.puts "\n" | |
for person <- people, | |
name = person.name do | |
IO.puts"#{name} status is: #{person.active}" | |
end | |
IO.puts "\n" | |
#for i <- 1..2, IO.inspect(i), j <- 5..6 do | |
# IO.puts {i, j} | |
#end | |
IO.puts "\n" | |
for n <- [1, 1, 2, 2, 2, 3], uniq: true do | |
IO.puts "#{n}" | |
end | |
IO.puts "\n" | |
people = [ | |
%{name: "John", active: true}, | |
%{name: "Patricia", active: false} | |
] | |
#for person <- people, into: %{} do | |
# IO.puts {person.name, person} | |
#end | |
numero=120 | |
resto=rem(numero,100) | |
IO.puts "Resto: #{resto}" | |
unless resto > 30 do | |
IO.puts "El mejor de todos" | |
else | |
IO.puts "Podria ser mejor" | |
end | |
# rem(120, 100) = 20 |
#if-else-end | |
numero=3 | |
if numero > 4 do | |
IO.puts "Esto no es verdadero" | |
else | |
IO.puts "Esto si es verdadero" | |
end |
numero=123 | |
str="ABC" | |
verdadero=true | |
binario=0b101 | |
IO.puts "Numero: #{numero}" | |
IO.puts "Cadena: #{str}" | |
IO.puts "Binario: #{binario}" | |
if verdadero do | |
IO.puts "Esto es verdadero" | |
end |