Qqwy

Qqwy

TypeCheck Core Team

Design problem with three similar structs

I am working on the Tensor library. This adds three modules to your project: Vector, Matrix and Tensor.

As you may or may not know, a Vector is just a one-dimensional Tensor, and a Matrix is a two-dimensional Tensor. Therefore, the Vector and Matrix modules contain some extra functions that only make sense when working with these low-dimensional structures(from_list only makes sense for Vectors, rotate_clockwise only makes sense for Matrices, etc.), but besides that, they delegate many functions to the Tensor module.

Right now, there is only one Struct being manipulated by all three modules: %Tensor{dimensions: [], contents: %{}, identity: 0}. A Vector can be pattern-matched as follows: vector = %Tensor{dimensions: [length]}. A Matrix can be pattern-matched as follows: matrix = %Tensor{dimensions: [height, width]}.

Needless to say, this kind of pattern-matching takes a bit of effort to grasp, and forces people using the library to know about the internal structure of the Vector and Matrix data structures they create.

Because of the impossibility of looking inside a map during guard-clauses, and on the other hand the impossibility to write macros that manage pattern-matching, we find ourselves an interesting Catch-22. This makes things like is_vector() or is_matrix() impossible.

An alternate approach that is possible is to pattern-match a vector or matrix as %Vector{} or %Matrix{}. This looks like the principle of least surprise to me.

The drawback of this second approach, is that it is no longer possible to pattern match on any tensor; in that case, indirection such as the following is required:

def some_function(tensor = %tensor_module{}) when is_tensor_module(tensor_module) do
  ...
end

defmacro is_tensor_module(module) do
  quote do
    unquote(module) in [Vector, Matrix, Tensor]
  end
end

which looks a bit clumsy, and at least also harder to understand to me. All functions inside the library (and all functions outside of it that are supposed to work on any order Tensor) would need this kind of pattern-match/guard macro combo.

So, now I am wondering: What is the better approach here?

  • Keep a single %Tensor{} struct, and accept that people have a higher learning- and pattern-matching curve.

or:

  • Have %Vector{}, %Matrix{} and %Tensor{} as three structs(that are the same, except in name) and modify all functions in the library to accept any of them using above-mentioned guard-macro.

Most Liked

andre1sk

andre1sk

what if you have %Tensor{type: :vector}

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New

We're in Beta

About us Mission Statement