Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Countries and Currencies #5

Open
EricForgy opened this issue Feb 16, 2019 · 6 comments
Open

Countries and Currencies #5

EricForgy opened this issue Feb 16, 2019 · 6 comments

Comments

@EricForgy
Copy link
Member

Hi guys 👋

I spent more time than I'd like to admit thinking about countries and currencies 😅

In #4 , we discussed a few ways to handle countries and currencies.

An obvious starting point would be to make them enums, but then to dispatch we'd need to dispatch on values and that did not seem ideal. Furthermore, with enums, it is difficult for a user to extend the list of countries or currencies. Given the cryptocurrency craze the last couple years, it makes sense to be able to define your own currency. Similarly, for similations, you may want to create your own country.

We also discussed a little bit about a vision of the future involving Holy traits.

Taking all this together, plus a few days of trial an error, I finally came up with something I think might be "OK" and I'm really interested in getting some feedback.

Countries and currencies are now singleton types defined as

struct Country{T} end
struct Currency{T} end

Then I run through a list and define

US = Country{:US}()
HK = Country{:HK}()
...
USD = Currency{:USD}()
HKD = Currency{:HKD}()
...

Currencies.jl

There are three methods defined for currencies:

  1. name
  2. code
  3. unit

used as follows:

julia> using Currencies
julia> import Currencies: unit, name, code, USD, PHP, HKD, SGD
julia> for ccy in [USD,PHP,HKD,SGD]
       println("Currency: $(ccy)")
       println("Name: $(name(ccy))")
       println("Code: $(code(ccy))")
       println("Minor Unit: $(unit(ccy))\n")
       end
       
Currency: USD
Name: US Dollar
Code: 840
Minor Unit: 2

Currency: PHP
Name: Philippine Piso
Code: 608
Minor Unit: 2

Currency: HKD
Name: Hong Kong Dollar
Code: 344
Minor Unit: 2

Currency: SGD
Name: Singapore Dollar
Code: 702
Minor Unit: 2

Countries.jl

There are eight methods defined for countries:

  1. name
  2. code
  3. capital
  4. continent
  5. isdeveloping
  6. region
  7. subregion
  8. currencies

used as follows:

julia> using Countries
julia> import Countries: name, code, capital, continent, isdeveloping, region, subregion, currencies, US, PH, HK, SG
julia> cs = [US,PH,HK,SG];
julia> for c in cs
       println("Country: $(c)")
       println("Name: $(name(c))")
       println("Code: $(code(c))")
       println("Capital: $(capital(c))")
       println("Continent: $(continent(c))")
       println("Developing: $(isdeveloping(c))")
       println("Region: $(region(c))")
       println("Subregion: $(subregion(c))")
       println("Currencies: $(currencies(c))\n")
       end

Country: US
Name: United States of America
Code: 840
Capital: Washington
Continent: NA
Developing: false
Region: Americas
Subregion: Northern America
Currencies: Currencies.Currency[USD]

Country: PH
Name: Philippines
Code: 608
Capital: Manila
Continent: AS
Developing: true
Region: Asia
Subregion: South-eastern Asia
Currencies: Currencies.Currency[PHP]

Country: HK
Name: China, Hong Kong Special Administrative Region
Code: 344
Capital: Hong Kong
Continent: AS
Developing: true
Region: Asia
Subregion: Eastern Asia
Currencies: Currencies.Currency[HKD]

Country: SG
Name: Singapore
Code: 702
Capital: Singapore
Continent: AS
Developing: true
Region: Asia
Subregion: South-eastern Asia
Currencies: Currencies.Currency[SGD]

Finally, I moved the original Currencies.jl written by @TotalVerb to CurrencyBaskets.jl.

Please let me know what you think 😊

@simonbyrne @felipenoris @tk3369 @alecloudenback et al

@cpfiffer
Copy link

Looks good to me. I like the dispatchability here.

@roshii
Copy link
Member

roshii commented Apr 14, 2019

Hi All,

I forked Currencies.jl to create its equivalent for CryptoCurrencies.

If it make sense I'm happy to move the later to JuliaFinance or maybe integrate it to Currencies.jl in some way? In the former case, an AbstractCurrency type may need to be created to cover both types.

@EricForgy
Copy link
Member Author

Hi @roshii ,

I'm a little confused. I'm curious why you needed to fork when Currencies.jl was already designed with cryptocurrency in mind.

What was missing? What is different about CryptoCurrencies.jl?

@roshii
Copy link
Member

roshii commented Apr 14, 2019

Hi @EricForgy

I have just created a custom csv with cryptocurrencies data and removed the numeric code parameter. Alphabetical code length rule is also more relax : from 3 to 8 character.

Your package only refers to the official ISO currency list on which crypto aren't listed

@EricForgy
Copy link
Member Author

Hi @roshii,

That makes sense, but if you are ok, it might be better to include your CSV file in Currencies.jl somehow rather than create a separate repo with a large code overlap.

I think if someone does:

julia> using Currencies

it should just pick up the ISO currencies without the crypto stuff. To get the cryptostuff, I think you should do something like

julia> using Currencies, Currencies.Crypto

I think this would make a good addition to Currencies.jl. What do you think?

@roshii
Copy link
Member

roshii commented Apr 15, 2019

That would be great!
I'll send you a pull request and we can take it from there as basis of discussion.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants