diff options
author | Andrew <andrewdimola@gmail.com> | 2022-07-15 20:52:44 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-15 20:52:44 -0400 |
commit | cc9abf1c46a3047862d58f30d0613bf3581045d0 (patch) | |
tree | fbe98d65255c54a5038b4607d1c3334085362132 /hack.html.markdown | |
parent | 13743fc49a16934babe480f87d98a344201bec30 (diff) |
Run Hackfmt and improve examples
Run hackfmt (the hack formatter) on the file
Clean up examples in the hack arrays section
Add a section on the unique binary pipe operator (and variable)
Diffstat (limited to 'hack.html.markdown')
-rw-r--r-- | hack.html.markdown | 47 |
1 files changed, 39 insertions, 8 deletions
diff --git a/hack.html.markdown b/hack.html.markdown index 89147dbc..1ab958b5 100644 --- a/hack.html.markdown +++ b/hack.html.markdown @@ -81,7 +81,7 @@ namespace LearnHackinYMinutes { $the_answer = 42; $is_answer = process_key($the_answer); - // Similarily, `num` represents either an int or float. + // Similarly, `num` represents either an int or float. $lucky_number = 7; $lucky_square = calculate_square($lucky_number); } @@ -107,7 +107,7 @@ namespace LearnHackinYMinutes { } // In contrast, an enum class can be of any value type! - enum class Random : mixed { + enum class Random: mixed { int X = 42; string S = 'foo'; } @@ -117,27 +117,39 @@ namespace LearnHackinYMinutes { * ================================== */ + // The following line lets us use functions in the `C\` namespace. + use namespace HH\Lib\C; // the `C` library operates on containers + function demo_hack_arrays(): void { - // vec: ordered + // vec: ordered $v = vec[1, 2, 3]; $letters = vec['a', 'b', 'c']; - $letters[0]; // indexing at `0` returns 'a' + + $letters[0]; // returns 'a' $letters[] = 'd'; // appends 'd' - // unset($letters['a']); error: remove-at-index is unsupported for vec - // keyset: ordered, without duplicates + // `inout` provides pass-by-reference behavior + C\pop_back(inout $letters); // removes 'd' + C\pop_front(inout $letters); // removes 'a' + + // keyset: ordered, without duplicates $k = keyset[1, 2, 3]; // values must be int or string $colors = keyset['red', 'blue', 'green']; - // $colors[0]; error: indexing not supported for keyset + + // keyset keys are identical to their values + $colors['blue']; // returns 'blue'. + $colors[] = 'yellow'; // appends 'yellow' unset($colors['red']); // removes 'red' // dict: ordered, by key-value $d = dict['a' => 1, 'b' => 3]; // keys must be int or string $alphabet = dict['a' => 1, 'b' => 2]; + $alphabet['a']; // indexing at 'a' returns `1` $alphabet['c'] = 3; // adds a new key-value pair of `c => 3` + unset($alphabet['b']); // removes 'b' } @@ -149,7 +161,6 @@ namespace LearnHackinYMinutes { // The Hack Standard Library is a set of functions and classes for the Hack language. // Namespace use declarations are ideally at the top of your file but are placed here for instruction purposes. - use namespace HH\Lib\C; // the `C` library operates on containers (like Hack Arrays) use namespace HH\Lib\Str; // The `Str` library operates on strings function demo_hack_standard_library(): void { @@ -217,6 +228,26 @@ namespace LearnHackinYMinutes { // $f = (int $x): int ==> $x + 1; /* ================================== + * PIPE OPERATOR + * ================================== + */ + + // The pipe operator, `|>`, evaluates the result of a left-hand expression + // and stores the result in `$$`, the predefined pipe variable. + + use namespace HH\Lib\Vec; + + function demo_pipe_operator(): void { + + Vec\sort(Vec\map(vec[2, 1, 3], $a ==> $a * $a)); // vec[1,4,9] + + // the same result, but using the pipe operator and pipe variable: + $x = vec[2, 1, 3] + |> Vec\map($$, $a ==> $a * $a) // $$ with value vec[2,1,3] + |> Vec\sort($$); // $$ with value vec[4,1,9] + } + + /* ================================== * ATTRIBUTES * ================================== */ |