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

Extend documentation with recipies of constructing instances of Hashing for composite types (sums, products, enums) #4

Open
nikita-volkov opened this issue Sep 6, 2019 · 2 comments

Comments

@nikita-volkov
Copy link
Owner

It's not obvious currently how the API can be used to achieve that.

This thread is intended for collection of examples, which you're very welcome to post as comments.

@nikita-volkov
Copy link
Owner Author

As a starter, here's two alternative recipies for an instance of a sum-type:

type FontLocation =
  GoogleFontLocation | UriFontLocation String | LocalFontLocation

fontLocationThruEither : Hashing FontLocation
fontLocationThruEither =
  either int (string 3) |>
  map (\ a -> case a of
    GoogleFontLocation -> Either.Left 0
    UriFontLocation b -> Either.Right b
    LocalFontLocation -> Either.Left 1)

fontLocationThruProduct : Hashing FontLocation
fontLocationThruProduct =
  concat
    [
      map .tag int,
      map .uri (string 3)
    ] |>
  map (\ a -> case a of
    GoogleFontLocation -> {tag = 0, uri = ""}
    UriFontLocation b -> {tag = 1, uri = b}
    LocalFontLocation -> {tag = 2, uri = ""})

@nikita-volkov
Copy link
Owner Author

Here's an example of an enum:

type FontWeight =
  ThinFontWeight |
  UltraLightFontWeight |
  LightFontWeight |
  NormalFontWeight |
  MediumFontWeight |
  SemiBoldFontWeight |
  BoldFontWeight |
  UltraBoldFontWeight |
  BlackFontWeight |
  UltraBlackFontWeight


fontWeight : Hashing FontWeight
fontWeight =
  int |>
  map (\ a -> case a of
    ThinFontWeight -> 0
    UltraLightFontWeight -> 1
    LightFontWeight -> 2
    NormalFontWeight -> 3
    MediumFontWeight -> 4
    SemiBoldFontWeight -> 5
    BoldFontWeight -> 6
    UltraBoldFontWeight -> 7
    BlackFontWeight -> 8
    UltraBlackFontWeight -> 9)

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

1 participant