Fetch with tuple call

32 views
Skip to first unread message

OvermindDL1

unread,
Jun 17, 2016, 4:20:30 PM6/17/16
to elixir-lang-talk
Currently I have an Array module modeled on Erlangs for speed with extra functions/macros that work like the Dict interface (except mine accepts the Array passed in at the beginning or the end of the parameter list since Dict has it at the beginning and tuple function calls put it at the end) except it only takes integers as keys and hard dies on anything else, but is faster than maps (a little less than twice as fast on gets, but substantially faster, well over ten times as fast and gets faster the larger the data that I am using over maps on erts-7.3/BEAM18, unsure if 19 is any faster):
```elixir
iex(1)> a = Array.new()
{Array, 0, 10, nil, 10}
iex(2)> a = a.resize(400)
{Array, 400, 1000, nil, 1000}
iex(3)> a = a.put_new(42, "testing")
{Array, 400, 1000, nil,
 {{10, 10, 10, 10, {nil, nil, "testing", nil, nil, nil, nil, nil, nil, nil}, 10,
   10, 10, 10, 10, 10}, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}}
```

How can I also enable this syntax:
```elixir
iex> a[42]
nil
```

Right now it instead raises a `FunctionClauseError` because `Access.fetch` is hard-coded to only a couple of things.  Could not something like this be added as an `Access.fetch/2` clause so it follows the same pattern as maps/structs?
```elixir
  def fetch(tup, key) when is_tuple(tup) and is_atom(elem(tup, 0)) do
    elem(tup, 0).fetch(tup, key)
  end
```

José Valim

unread,
Jun 17, 2016, 4:24:08 PM6/17/16
to elixir-l...@googlegroups.com
Access works like a protocol where you can only extend it for structs.



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/c29cfa0f-513d-4c92-8985-9ebc1ae94548%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

OvermindDL1

unread,
Jun 17, 2016, 4:27:59 PM6/17/16
to elixir-lang-talk, jose....@plataformatec.com.br
As a protocol it would be considered to have no tuple implementation though, of which I could then supply.  However it appears it is not a protocol in the slightest and it is instead pure function matching with the compiler doing magic to turn `a[1]` into `Access.fetch(a, 1)`, thus one more overload would be wonderfully helpful.  :-)

José Valim

unread,
Jun 17, 2016, 4:32:11 PM6/17/16
to OvermindDL1, elixir-lang-talk
As a protocol it would be considered to have no tuple implementation though, of which I could then supply.

Yes and no. You could but it wouldn't be semantically correct. What if someone else implement their own "fake" data type with tuples and want to use the tuple implementation? It wouldn't compose and the code would just fail.

OvermindDL1

unread,
Jun 17, 2016, 4:39:42 PM6/17/16
to elixir-lang-talk, overm...@gmail.com, jose....@plataformatec.com.br
Indeed, which is why using it as a tuple function call makes the most sense for tuples, the first element being an atom that is the module that the 'fetch' function should be called on, if not an atom then the match fails, thus consistent with normal tuple call syntax.  :-)

On another note, is there really no Elixir'ish array module.  I used maps before but it was taking almost thirty seconds to generate some of these reports, replaced it with erlangs array module and it fell to <1s, so I made a replacement module but I would really love not to have to upkeep it, especially if one already exists elsewhere.  :-)

OvermindDL1

unread,
Jun 17, 2016, 4:44:51 PM6/17/16
to elixir-lang-talk, overm...@gmail.com, jose....@plataformatec.com.br
I just got a fascinating idea, I wonder how a map of maps would work as such a tree like this instead of tuples...  I may have to test that next...
Reply all
Reply to author
Forward
0 new messages