Giter Site home page Giter Site logo

intersection problem about elixir-datastructures HOT 16 CLOSED

meh avatar meh commented on September 15, 2024
intersection problem

from elixir-datastructures.

Comments (16)

gvaughn avatar gvaughn commented on September 15, 2024

nevermind me. I figured it out.

from elixir-datastructures.

meh avatar meh commented on September 15, 2024

Just curious, what was it?

from elixir-datastructures.

gvaughn avatar gvaughn commented on September 15, 2024

Well, that code above will generate that error. I think my issue was both that I expected more magic, and it's too magic for my understanding. You can't (or didn't) add your Data.Set protocol to a raw list object as I naively tried. I looked at your implementation and I can't figure out what L.new is referring to, but it plainly works if I pass in parameters of record Data.Set.BalancedTree.

If you're more curious (and I'm happy for any suggestions to improve) my code that brings in this lib is on the datastructures branch of my elixir_kata repo: https://github.com/gvaughn/elixir_kata/tree/datastructures/conway
I kept it separate from master because I'm not honestly sure this is an overall improvement. It was a good learning experience for me. On master I'm calling into the raw erlang :set to do set behaviors in a single place, otherwise working with a list.

I also know things are changing quickly in elixir-land. I see that there is a Set protocol now for elixir 0.10.0, which I don't think was there when I wrote this on the last release. But I don't see a sorted set in the standard lib. Do you picture this library moving into the standard lib?

from elixir-datastructures.

meh avatar meh commented on September 15, 2024

I started this when I started working on a Stack and Queue implementations, and they were refused.

I sincerely doubt this will be ever merged in, but this isn't going to be abandoned, I use it in all my projects including ones that are going in production.

Also Set in the stdlib is not a protocol, but rather a behaviour like Dict.

Nonetheless I think this is actually a bug, so I'll look at it right now.

from elixir-datastructures.

meh avatar meh commented on September 15, 2024
iex(1)> Data.Set.intersection([1,2,3],[1,2])
[1, 2]

🐼

from elixir-datastructures.

gvaughn avatar gvaughn commented on September 15, 2024

Sorry. The dividing line between behaviours and protocols are still fuzzy to me.

Are you riding elixir HEAD? I'm on the 0.10.0 release, with erlang R16B (if that matters) and the error is still there.

[me@GGV-LS conway (datastructures)]$ iex -S mix
Erlang R16B (erts-5.10.1) [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

/Users/enduser/code/elixir_kata/conway/lib/conway.ex:2: unused import Data.Set.BalancedTree
Compiled lib/conway.ex
Generated conway.app
Interactive Elixir (0.10.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Data.Set.intersection([1,2,3],[1,2])
** (UndefinedFunctionError) undefined function: L.new/1
L.new([1, 2])
/Users/enduser/code/elixir_kata/conway/deps/datastructures/lib/data/set.ex:64: Data.Set.List.intersection/2
erl_eval.erl:569: :erl_eval.do_apply/6
src/elixir.erl:147: :elixir.eval_forms/3
/private/tmp/elixir-FC3J/elixir-0.10.0/lib/iex/lib/iex/server.ex:112: IEx.Server.eval/4
/private/tmp/elixir-FC3J/elixir-0.10.0/lib/iex/lib/iex/server.ex:52: IEx.Server.wait_input/1
/private/tmp/elixir-FC3J/elixir-0.10.0/lib/iex/lib/iex/server.ex:31: IEx.Server.start/1

from elixir-datastructures.

meh avatar meh commented on September 15, 2024

Did you run mix deps.update before trying that?

from elixir-datastructures.

gvaughn avatar gvaughn commented on September 15, 2024

No. I didn't realize you had pushed a fix. I thought you couldn't reproduce the problem. After deps.update I get the proper response! Thanks.

Now I need to go rewrite all my code if that was supposed to work ;-)

from elixir-datastructures.

meh avatar meh commented on September 15, 2024

My fault, I wasn't clear about it :)

from elixir-datastructures.

gvaughn avatar gvaughn commented on September 15, 2024

@meh I got around to changing my code and I'm seeing another bug in intersection when I use tuple elements:

iex(4)> Data.Set.intersection([{1,2},{1,1},{1,0}], [{1,1}])
[]

My older implementation of this logic before finding this library is:

iex(1)> :sets.from_list([{1,2},{1,1},{1,0}]) |> :sets.intersection(:sets.from_list([{1,1}])) |> :sets.to_list
[{1, 1}]

from elixir-datastructures.

meh avatar meh commented on September 15, 2024

Sadly there's nothing I can do, unless I reimplement ordsets.

iex(2)> :ordsets.intersection([{1,2},{1,1},{1,0}], [{1,1}])
[]

You can always use Data.Set.BalancedTree or Data.Set.Standard.

from elixir-datastructures.

gvaughn avatar gvaughn commented on September 15, 2024

Wow. That's core erlang stuff. I wonder if this is a tuple specific problem.

Anyway, I've got another implementation that I'll be able to show-n-tell tonight at our local elixir meet up. Thanks for your help

from elixir-datastructures.

meh avatar meh commented on September 15, 2024

You're welcome.

from elixir-datastructures.

gvaughn avatar gvaughn commented on September 15, 2024

I figured out what's up. ordsets is optimized to assume the inputs are ordered already. Notice:

iex(1)> :ordsets.intersection([{1,0},{1,1},{1,2}], [{1,1}])
[{1, 1}]

from elixir-datastructures.

meh avatar meh commented on September 15, 2024

Oh yeah, the first parameter is always assumed to be already an ordset, only the second one is converted, forgot about it, sorry.

from elixir-datastructures.

meh avatar meh commented on September 15, 2024
Data.Set.List.new([{1,2},{1,1},{1,0}]) |> Data.Set.intersection([{1,1}])

This would be the "proper" way to do it.

from elixir-datastructures.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.