5 ms·
The benefit of Map/Keyword is that you look at the code and you know right away what type it takes. Access is more universal but you look at it and have multipl
by pawelduda 5y ago
The benefit of Map/Keyword is that you look at the code and you know right away what type it takes. Access is more universal but you look at it and have multiple possibilities.
- jolux 5y agoI would recommend writing specs for your functions and using Dialyzer, too :)
- arthurcolle 5y agoI've never been able to figure out how to write custom types in Erlang. Any resources in particular that you recommend? I am not trying to be obtuse but the Erlang docs are unreadable and I have admittedly written quite a bit of Elixir code. Most of the time I have to just figure things out ad-hoc from my own interactions with the shell.
- jolux 5y agoDepends what you mean by “custom types.” Elixir supports structs, but they’re not really as rich as classes in another language, for example. If you just want to write type signatures for your functions though, this is a pretty good guide: https://hexdocs.pm/elixir/1.12/typespecs.html https://hexdocs.pm/elixir/1.12/typespecs.html
- arthurcolle 5y agoRight, using structs for things I'm doing have been extremely painful, to say the least. Let's say I'm trying to create a module that represents a European option and I want to encapsulate things like "strike price", "expiration date", and the various Greeks in a way that are subsequently modifiable (FWIW, it would be perfectly fine to then just respawn a new proc to have the new data if that is absolutely necessary...). I haven't been able to identify a way to even reference other PIDs that exist which might be able to (with some wizardry) solve some of these problems. Right now I'm basically wrapping basically all my data in Agents with maps inside, but that is so lame. Thanks for the quick response. Maybe I'm trying too hard to use a saw to hammer a nail. EDIT: I don't care about type signatures on functions. I'd just like to be able to have a real time system that updates based on ingested values, that I can distribute easily. It seems like it's possible, even close, but the structs just don't get me quite where I want them to be.
- deleted 5y ago[deleted]
- jkhdigital 5y agoYour description is a bit hard to follow, but it smells kind of like something Ecto schemas could deal with? That is, by defining an Ecto schema for your data structure you can go almost directly from raw json to clean, correctly casted data. To use an example similar to yours, this is how I process data from cryptocurrency exchange APIs—just write up a schema for the shape of the response, parse the json into a map and throw the map into Ecto.Changeset.cast. Even if everything is a string in the raw data, it will be correctly cast into integer/float/whatever as defined in the schema.
- arthurcolle 5y agoSorry about that. Vacationing in Miami Beach right now so I might not be 100% in my descriptive capacity. Anyways, yeah, that obviously could work, but I'm kind of trying to make this all just run in memory because I'm already persisting all the data separately.
- jkhdigital 5y agoYou can use Ecto schemas and changesets without a database! It is an incredibly useful library, once you get over the somewhat steep learning curve.
- pawelduda 5y agoI support that too, but it's yet another step as opposed to simply using a specific module to suit the data type :) On another note, I find Dialyzer a bit slow to run even on modest codebases.