Skip to content

Commit

Permalink
2024 c1
Browse files Browse the repository at this point in the history
  • Loading branch information
bossiernesto committed May 9, 2024
1 parent d6d32c8 commit 8cd199f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
uses: erlef/setup-beam@v1

with:
elixir-version: '1.14.3' # Define the elixir version [required]
elixir-version: '1.15.7' # Define the elixir version [required]
otp-version: '25.2' # Define the OTP version [required]
- name: Install dependencies
run: mix deps.get
Expand Down
33 changes: 25 additions & 8 deletions intro_actors/lib/genserver_example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,46 @@ defmodule Post do
use GenServer
#Implementación con actores y Genserver...

def start(state, name) do
GenServer.start(__MODULE__, state, name: name)
# funciones de creacion
def start(cantidad_inicial_likes, name) do
GenServer.start(__MODULE__, cantidad_inicial_likes, name: name)
end

def start_link(state, name) do
GenServer.start_link(__MODULE__, state, name: name)
def start_link(cantidad_inicial_likes, name) do
GenServer.start_link(__MODULE__, cantidad_inicial_likes, name: name)
end

# estado inicial
def init(cantidad_inicial_likes) do
{:ok, cantidad_inicial_likes}
end

def handle_call(:get, from, state) do
IO.puts 'Recibi pedido de cant de likes de #{inspect from}'
## handle

## envio sincronico
def handle_call(:get, from_pid, state) do
IO.puts 'Recibi pedido de cant de likes de #{inspect from_pid}'
{:reply, state, state}
end

def handle_call(:blah, from_pid, state) do
IO.puts 'Recibi pedido de blah de #{inspect from_pid}'
{:reply, state+1, state}
end

## envio asincronico
def handle_cast({:like, pid}, state) do
IO.puts "Recibi :like de #{inspect pid}"
send pid, {:ok, "Recibi tu like"}
nuevo_estado = state + 1
{:noreply, nuevo_estado}
end

def handle_cast({:like_n, n}, state) do
nuevo_estado = state + n
{:noreply, nuevo_estado}
end

# --- funciones de uso ---

def like(post, pid) do
Expand All @@ -37,10 +53,11 @@ defmodule Post do
end
end

#{:ok, post} = Post.start_link(0, :post_principal)

#{:ok, post} = Post.start(0, :post_principal)
#for _ <- 1..1000, do: Post.like(:post_principal)
# aumentar_like = fn -> for _ <- 1..1000, do
# Post.like(:post_principal)
# Post.like(:post_principal)
# :timer.sleep(50)
# end
# end
Expand Down
6 changes: 6 additions & 0 deletions intro_actors/lib/intro_actors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ defmodule IntroActors do
spawn(fn -> loop() end)
end

def start_link do
spawn_link(fn -> loop() end)
end

def loop() do
receive do
{_, :camina} -> IO.puts 'Jose empieza a caminar. Proceso #{inspect self()}'
{_, :corre} -> IO.puts 'Ni ahi. Proceso #{inspect self()}'
{pid, _ } -> send pid, {:error, 'Accion Invalida de #{inspect pid}'}
_ -> IO.puts 'Accion Invalida...'
end
loop()
end
end

Expand Down
8 changes: 5 additions & 3 deletions intro_actors/lib/intro_agent.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
defmodule KV.Agent do
use Agent

def start do
Agent.start(fn -> %{} end)
end

def start_link do
Agent.start_link(fn -> %{} end)
end
Expand All @@ -15,14 +19,12 @@ defmodule KV.Agent do
def put(agent, key, value) do
Agent.update(agent, &Map.put(&1, key, value))
end


end


# iex(5)> {:ok, pid} = KV.Agent.start_link
# {:ok, #PID<0.166.0>}
# iex(9)> KV.Agent.put(pid, :hello, 1)
# :ok
# iex(10)> KV.Agent.get(pid, :hello)
# iex(10)> KV.Agent.get(pid, :hello)
# 1
18 changes: 16 additions & 2 deletions intro_actors/lib/intro_consola._ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ iex(6)> flush
:bleh
:ok

receive do
{:hello, msg} -> msg
{:world, msg} -> 'dont match'
after
10_000 -> "No paso nada después de 10s"
end

#veamos algo ahora un poco más complejo
iex(9)> send self(), {:hello, "world"}
{:hello, "world"}
Expand Down Expand Up @@ -102,7 +109,7 @@ iex(23)> flush

#hay secuencialidad?

g = fn(x) -> :timer.sleep(50); IO.puts "#{x}" end
g = fn(x) -> :timer.sleep(100); IO.puts "#{x}" end
for n <- 1..10, do: spawn(fn-> g.(n) end)

iex(1)> g = fn(x) -> :timer.sleep(50); IO.puts "#{x}" end
Expand Down Expand Up @@ -212,4 +219,11 @@ iex(3)> :shell_default.i(0,62,0)
minor_gcs: 1
],
suspending: []
]
]


spawn fn -> raise "ups" end

spawn_link fn -> raise "ups" end

Process.info(jose, :links)
6 changes: 5 additions & 1 deletion intro_actors/lib/intro_task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ defmodule IASC.WriteHaiku do
Task.start_link(__MODULE__, :run, [arg])
end

def start_run(arg) do
Task.start(__MODULE__, :run, [arg])
end

def start_link_read() do
Task.start_link(__MODULE__, :read_haiku, [])
end
Expand Down Expand Up @@ -43,4 +47,4 @@ end

# {:ok, pid} = IASC.WriteHaiku.start_link_read()
#haiku= "Arbol sereno\nque en el bosque protege\na tantos seres"
#{:ok, pid} = IASC.WriteHaiku.start_link_run(haiku)
#{:ok, pid} = IASC.WriteHaiku.start_link_run(haiku)
4 changes: 2 additions & 2 deletions intro_actors/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Actors.Mixfile do
def project do
[app: :intro_actors,
version: "0.0.3",
elixir: "~> 1.9",
elixir: "~> 1.15",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps()]
Expand All @@ -14,7 +14,7 @@ defmodule Actors.Mixfile do
#
# Type `mix help compile.app` for more information
def application do
[applications: [:logger]]
[applications: [:logger, :eex, :wx, :observer, :runtime_tools]]
end

# Dependencies can be Hex packages:
Expand Down

0 comments on commit 8cd199f

Please sign in to comment.