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

Add 'Burger Discounts' chapter #34

Merged
merged 27 commits into from
Apr 9, 2024
Merged

Add 'Burger Discounts' chapter #34

merged 27 commits into from
Apr 9, 2024

Conversation

feihong
Copy link
Collaborator

@feihong feihong commented Apr 3, 2024

  • Type inference is less effective inside functions that don't call other
    functions. In those cases, you can give the compiler more information:
    • Type annotate the function arguments
    • Use the full name for value
  • The Stdlib module is opened by default
  • Records are immutable
  • Use record copy syntax to make copies of records that have different values
    for some fields
  • OCaml doesn't allow you to ignore the return value of functions (unless the
    value is ()), so you can use Stdlib.ignore to explicitly discard return
    values
  • Runtime representations of common data types:
    • Variant constructor without argument -> integer
    • Variant constructor with argument -> JS object
    • Record -> JS object
    • None -> undefined
    • Some(value) -> value
  • Array facts:
    • Arrays are mutable, just like in JS
    • You can pattern match on arrays of fixed length
    • Array access is unsafe by default
    • What looks like operator usage in array[index] is actually just a call to
      Array.get(array, index)
    • You can create your own Array module to override the behavior of
      Array.get

@feihong feihong marked this pull request as ready for review April 5, 2024 01:06
@feihong feihong requested a review from jchavarri April 5, 2024 01:06
Copy link
Member

@jchavarri jchavarri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found some outdated paths and a typo, but otherwise all are minor comments. Amazing work! 💪

docs/burger-discounts/index.md Outdated Show resolved Hide resolved
docs/burger-discounts/index.md Show resolved Hide resolved
docs/burger-discounts/index.md Show resolved Hide resolved
docs/burger-discounts/index.md Show resolved Hide resolved
docs/burger-discounts/index.md Show resolved Hide resolved
docs/burger-discounts/index.md Outdated Show resolved Hide resolved
src/burger-discounts/Discount.re Outdated Show resolved Hide resolved
docs/burger-discounts/index.md Show resolved Hide resolved
src/cram-tests/Index.re Outdated Show resolved Hide resolved
docs/burger-discounts/index.md Show resolved Hide resolved
@feihong feihong requested a review from jchavarri April 9, 2024 04:46
Comment on lines +7 to +8
> Given an order comprising multiple items, find the price of the second most
> expensive burger. Subtract this number from the pre-tax total of the order.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be something like this? To make it more generic:

Suggested change
> Given an order comprising multiple items, find the price of the second most
> expensive burger. Subtract this number from the pre-tax total of the order.
> Given an order comprising multiple items, for every two burgers ordered find the
> price of the less expensive one. Subtract this number from the pre-tax total of the
> order.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes more sense, but I would like to implement it in the next chapter

Comment on lines 355 to 361
As an extra precaution, you might want to catch the exception inside the switch
expression:

<<< Discount.re#catch-exception{5}

With the current logic, it's not impossible to reach the exception branch, but
who knows how the code might evolve in the future.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this could be shared as an alternative approach? Rather than "as an extra precaution", something like "as an alternative approach", and then share the code with exn handling and without the .length check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, catching the exception is described as alternate approach in 4184366

Comment on lines 365 to 367
It looks like `burger[0]` is using an array access operator, but it's actually
just a function call. That is, `burger[0]` is completely equivalent to
`Array.get(burger, 0)`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It looks like `burger[0]` is using an array access operator, but it's actually
just a function call. That is, `burger[0]` is completely equivalent to
`Array.get(burger, 0)`.
In OCaml, array access operators like `burger[0]` are actually just a
function call. That is, `burger[0]` is completely equivalent to
`Array.get(burger, 0)`.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworded in 20a777b

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

Successfully merging this pull request may close these issues.

2 participants