Programatically accessing the definition of typespec - reflection

How might I access the definition of a typespec within my code? I wish to use it within a macro in order to perform some code generation.
Something like this would be ideal.
Given this module and typespec:
defmodule MyMod do
#type t :: :ok | :error
end
I could call a function such as Code.get_type(MyMod, :t) and it would return the AST of the definition expression:
{:::, [], [{:my_type, [], Elixir}, {:|, [], [:ok, :error]}]}
Or alternatively just the AST of the type:
{:|, [], [:ok, :error]}

This is a hack (and works only on compilation stage,) but it does what you want:
defmodule MyMod do
#type t1 :: :ok | :error
#type t2 :: :done
#type_defs Module.get_attribute(__MODULE__, :type, [])
def type_defs,
do: Enum.map(#type_defs, fn {:type, type, _} -> type end)
end
MyMod.type_defs
#⇒ [{:::, [line: 9], [{:t2, [line: 9], nil}, :done]},
# {:::, [line: 8], [{:t1, [line: 8], nil}, {:|, [line: 8], [:ok, :error]}]}]
One might declare a module with defmacro __using__, implementing this functionality and use TypeExtractor whenever needed.

Related

How can I write a generic function that takes either an ndarray Array or ArrayView as input?

I am writing a set of mathematical functions using ndarray which I would like to perform on any type of ArrayBase. However, I'm having trouble specifying the traits/types involved.
This basic function works on either OwnedRepr or ViewRepr data:
use ndarray::{prelude::*, Data}; // 0.13.1
fn sum_owned(x: Array<f64, Ix1>) -> f64 {
x.sum()
}
fn sum_view(x: ArrayView<f64, Ix1>) -> f64 {
x.sum()
}
fn main() {
let a = Array::from_shape_vec((4,), vec![1.0, 2.0, 3.0, 4.0]).unwrap();
println!("{:?}", sum_owned(a.clone()));
let b = a.slice(s![..]);
println!("{:?}", sum_view(b));
// Complains that OwnedRepr is not ViewRepr
//println!("{:?}", sum_view(a.clone()));
}
I can understand why the commented out section doesn't compile, but I don't understand generics well enough to write something more... generic.
Here is what I tried:
use ndarray::prelude::*;
use ndarray::Data;
fn sum_general<S>(x: ArrayBase<S, Ix1>) -> f64
where
S: Data,
{
x.sum()
}
The compiler error suggests that Data is not specific enough, but I just can't parse it well enough to figure out what the solution should be:
error[E0277]: the trait bound `<S as ndarray::data_traits::RawData>::Elem: std::clone::Clone` is not satisfied
--> src/lib.rs:8:7
|
6 | S: Data,
| - help: consider further restricting the associated type: `, <S as ndarray::data_traits::RawData>::Elem: std::clone::Clone`
7 | {
8 | x.sum()
| ^^^ the trait `std::clone::Clone` is not implemented for `<S as ndarray::data_traits::RawData>::Elem`
error[E0277]: the trait bound `<S as ndarray::data_traits::RawData>::Elem: num_traits::identities::Zero` is not satisfied
--> src/lib.rs:8:7
|
6 | S: Data,
| - help: consider further restricting the associated type: `, <S as ndarray::data_traits::RawData>::Elem: num_traits::identities::Zero`
7 | {
8 | x.sum()
| ^^^ the trait `num_traits::identities::Zero` is not implemented for `<S as ndarray::data_traits::RawData>::Elem`
error[E0308]: mismatched types
--> src/lib.rs:8:5
|
4 | fn sum_general<S>(x: ArrayBase<S, Ix1>) -> f64
| --- expected `f64` because of return type
...
8 | x.sum()
| ^^^^^^^ expected `f64`, found associated type
|
= note: expected type `f64`
found associated type `<S as ndarray::data_traits::RawData>::Elem`
= note: consider constraining the associated type `<S as ndarray::data_traits::RawData>::Elem` to `f64`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
If you look at the definition of the ndarray::ArrayBase::sum function that you're attempting to invoke:
impl<A, S, D> ArrayBase<S, D>
where
S: Data<Elem = A>,
D: Dimension,
{
pub fn sum(&self) -> A
where
A: Clone + Add<Output = A> + Zero
{
// etc.
}
}
It's clear that in your case A = f64 and D = Ix1, but you still need to specify the constraint S: Data<Elem = f64>. Therefore:
use ndarray::prelude::*;
use ndarray::Data;
fn sum_general<S>(x: ArrayBase<S, Ix1>) -> f64
where
S: Data<Elem = f64>,
{
x.sum()
}
Which is exactly what the compiler meant when it suggested:
= note: expected type `f64`
found associated type `<S as ndarray::data_traits::RawData>::Elem`
= note: consider constraining the associated type `<S as ndarray::data_traits::RawData>::Elem` to `f64`

read a file of specific content in erlang

I followed this method to read files - how to read the contents of a file In Erlang?
The answer mentioned works good. However, my file content looks like this:
{0, {data, node, 0}}
{1, {someData, node, 1}}
Every line will have this data. So while reading file I want to filter on how many lines it should read
something like this:
read(Node, FirstIndex, LastIndex, F, Acc)
Here, F is the fold function, which takes a single log entry and the current accumulator and returns the new accumulator value.
How can I incorporate the solution with this function that I need??
-module(my).
-compile(export_all).
get_section(Start, End, Fname) when Start>0, End>=Start ->
{ok, Io} = file:open(Fname, read),
ok = advance_file_pointer(Start, Io),
Lines = getNLines(End-Start+1, Io),
file:close(Io),
Lines.
advance_file_pointer(1, _Io) ->
ok;
advance_file_pointer(Start, Io) ->
{ok, _Line} = file:read_line(Io),
advance_file_pointer(Start-1, Io).
getNLines(N, Io) ->
getNLines(N, Io, []).
getNLines(0, _Io, Acc) ->
lists:reverse(Acc);
getNLines(N, Io, Acc) ->
{ok, Line} = file:read_line(Io),
getNLines(N-1, Io, [Line|Acc]).
In the shell:
~/erlang_programs$ cat data.txt
{1, {data, node, 1}}
{2, {someData, node, 2}}
{3, {otherData, node, 3}}
~/erlang_programs$ erl
Erlang/OTP 20 [erts-9.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V9.2 (abort with ^G)
1> c(my).
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}
2> my:get_section(1, 1, "data.txt").
["{1, {data, node, 1}}\n"]
3> my:get_section(1, 2, "data.txt").
["{1, {data, node, 1}}\n","{2, {someData, node, 2}}\n"]
4> my:get_section(1, 3, "data.txt").
["{1, {data, node, 1}}\n","{2, {someData, node, 2}}\n",
"{3, {otherData, node, 3}}\n"]
5> my:get_section(2, 3, "data.txt").
["{2, {someData, node, 2}}\n","{3, {otherData, node, 3}}\n"]
6> my:get_section(2, 2, "data.txt").
["{2, {someData, node, 2}}\n"]
7> my:get_section(0, 2, "data.txt").
** exception error: no function clause matching my:get_section(0,2,"data.txt") (my.erl, line 4)
8> my:get_section(1, 4, "data.txt").
** exception error: no match of right hand side value eof
in function my:getNLines/3 (my.erl, line 20)
in call from my:get_section/3 (my.erl, line 7)
9>
You can apply your fold function in the second clause of getNLines/3.

Ecto.Repo receives a struct that does not implement Access behaviour

i have a problem with an Ecto Repo and a schema in one of my
tests. The schema is the following:
defmodule Elixirserver.Transactions.Bank do
#behaviour Elixirserver.ContentDump
use Ecto.Schema
import Ecto.Changeset
alias Elixirserver.Transactions.Account
#attrs [:name, :code]
schema "banks" do
field(:name, :string)
field(:code, :string)
has_many(:account, Account)
timestamps()
end
#doc false
def changeset(bank, attrs \\ []) do
bank
|> cast(attrs, #attrs)
|> validate_required(#attrs)
end
def to_json(bank) do
%{
id: bank.id,
name: bank.name,
code: bank.code,
type: "BANK"
}
end
end
When i try to execute a test i obtain the following:
(UndefinedFunctionError) function
Elixirserver.Transactions.Bank.fetch/2 is undefined
(Elixirserver.Transactions.Bank does not implement the Access behaviour)
The test is this:
def create(conn, %{"bank" => bank_params}) do
with {:ok, %Bank{} = bank} <- Transactions.create_bank(bank_params) do
conn
|> put_status(:created)
|> put_resp_header("location", bank_path(conn, :show, bank))
|> render("show.json", id: bank["id"])
end
end
Now, apparently this is because the Access behaviour is not implemented. Do i have to provide it explicitly ?
I am using ExMachina to generate fixtures, and i generated the resources with mix phx.gen.json.
bank["id"] is most probably the problem. Structs don't implement the access interface, you should use the dot so this should work: bank.id.
Details can be found here.

How do I share data between worker processes in Elixir?

I have 2 workers
worker(Mmoserver.MessageReceiver, []),
worker(Mmoserver.Main, [])
The MessageReceiver will wait until messages are received on TCP and process them, the Main loop will take that information and act on it. How do I share the info obtained by worker1 with worker2?
Mmoserver.ex
This is the main file that starts the workers
defmodule Mmoserver do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
IO.puts "Listening for packets..."
children = [
# We will add our children here later
worker(Mmoserver.MessageReceiver, []),
worker(Mmoserver.Main, [])
]
# Start the main supervisor, and restart failed children individually
opts = [strategy: :one_for_one, name: AcmeUdpLogger.Supervisor]
Supervisor.start_link(children, opts)
end
end
MessageReceiver.ex
This will just start a tcp listener. It should be able to get a message, figure out what it is (by it's id) then parse data and send it to a specific function in Main
defmodule Mmoserver.MessageReceiver do
use GenServer
require Logger
def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, :ok, opts)
end
def init (:ok) do
{:ok, _socket} = :gen_udp.open(21337)
end
# Handle UDP data
def handle_info({:udp, _socket, _ip, _port, data}, state) do
parse_packet(data)
# Logger.info "Received a secret message! " <> inspect(message)
{:noreply, state}
end
# Ignore everything else
def handle_info({_, _socket}, state) do
{:noreply, state}
end
def parse_packet(data) do
# Convert data to string, then split all data
# WARNING - SPLIT MAY BE EXPENSIVE
dataString = Kernel.inspect(data)
vars = String.split(dataString, ",")
# Get variables
packetID = Enum.at(vars, 0)
x = Enum.at(vars, 1)
# Do stuff with them
IO.puts "Packet ID:"
IO.puts packetID
IO.puts x
# send data to main
Mmoserver.Main.handle_data(vars)
end
end
Main.ex
This is the main loop. It will process all the most recent data received by the tcp listener and act on it. Eventually it will update the game state too.
defmodule Mmoserver.Main do
use GenServer
#tickDelay 33
def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, [], name: Main)
end
def init (state) do
IO.puts "Main Server Loop started..."
# start the main loop, parameter is the initial tick value
mainLoop(0)
# return, why 1??
{:ok, 1}
end
def handle_data(data) do
GenServer.cast(:main, {:handle_data, data})
end
def handle_info({:handle_data, data}, state) do
# my_function(data)
IO.puts "Got here2"
IO.puts inspect(data)
{:noreply, state}
end
# calls respective game functions
def mainLoop(-1) do
IO.inspect "Server Loop has ended!" # base case, end of loop
end
def mainLoop(times) do
# do shit
# IO.inspect(times) # operation, or body of for loop
# sleep
:timer.sleep(#tickDelay);
# continue the loop RECURSIVELY
mainLoop(times + 1)
end
end
Because Mmoserver.MessageReceiver is going to send messages to Mmoserver.Main, Main has to be started in first place, plus, it needs to have name associated:
worker(Mmoserver.Main, []),
worker(Mmoserver.MessageReceiver, [])
The easiest way could be, in your Mmoserver.Main, assuming it is a GenServer:
defmodule Mmoserver.Main do
use GenServer
def start_link do
GenServer.start_link(__MODULE__, [], name: :main)
end
# ...
end
You can add convenience function, plus the implementation one like:
defmodule Mmoserver.Main do
# ...
def handle_data(data) do
GenServer.cast(:main, {:handle_data, data})
end
def handle_info({:handle_data, data}, state) do
my_function(data)
{:noreply, state}
end
end
So, your MessageReceiver, can send a message like:
defmodule Mmoserver.MessageReceiver do
def when_data_received(data) do
Mmoserver.Main.handle_data(data)
end
end
This assumes Mmoserver.MessageReceiver doesn't expect Mmoserver.Main to respond. I've decided to do it this way as you didn't specify the way you want to handle the data and this seems the easies example of how to do this.

send calls via an endpoint to a GenServer

Given a running GenServer, is there a known way to send synchronous/asynchronous calls to the pid via an endpoint, without using the Phoenix framework?
Here's an example call (using python's requests library) that maps the reply term to JSON:
iex> give_genserver_endpoint(pid, 'http://mygenserverendpoint/api')
iex> {:ok, 'http://mygenserverendpoint/api'}
>>> requests.get(url='http://mygenserverendpoint/getfood/fruits/colour/red')
>>> '{ "hits" : ["apple", "plum"]}'
You can write a complete elixir http server using cowboy and plug:
Application Module
defmodule MyApp do
use Application
def start(_type, _args) do
import Supervisor.Spec
children = [
worker(MyGenServer, []),
Plug.Adapters.Cowboy.child_spec(:http, MyRouter, [], [port: 4001])
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
end
Router Module
defmodule MyRouter do
use Plug.Router
plug :match
plug :dispatch
get "/mygenserverendpoint/getfood/fruits/colour/:colour" do
response_body = MyGenServer.get_fruit_by_colour(colour)
conn
|> put_resp_content_type("application/json")
|> send_resp(conn, 200, Poison.encode(response_body))
end
match _ do
send_resp(conn, 404, "oops")
end
end
GenServer module
defmodule MyGenServer do
use GenServer
def start_link do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end
def get_fruit_by_colour(colour) do
GenServer.call(__MODULE__, {:get_by_colour, colour})
end
def handle_call({:get_by_colour, colour}, _from, state) do
{:reply, %{"hits" : ["apple", "plum"]}, state}
end
end

Resources