Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jul 2, 2021
1 parent 2044fbf commit 5e6df77
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 8 deletions.
22 changes: 14 additions & 8 deletions crystal/arrays.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Arrays
{id: arrays}
{id: array}


## Arrays intro
{id: arrays-intro}
{id: array-intro}

* [Array API](https://crystal-lang.org/api/Array.html)
* [Array reference](https://crystal-lang.org/reference/syntax_and_semantics/literals/array.html)
Expand All @@ -23,7 +23,7 @@
![](examples/arrays/array_iterate.cr)

## Array push, append, <<
{id: arrays-push}
{id: array-push}
{i: push}
{i: append}
{i: <<}
Expand All @@ -38,12 +38,12 @@
![](examples/arrays/empty_array.cr)

## Count digitis
{id: count-digits}
{id: array-count-digits}

![](examples/arrays/count_digits.cr)

## Operations on arrays
{id: operations-on-arrays}
{id: array-operations}

* add +
* repeat *
Expand Down Expand Up @@ -94,7 +94,7 @@
![](examples/arrays/map.out)

## Sample from array
{id: arrays-sample}
{id: array-sample}
{i: sample}

* Using [Random](random) behind the scenes.
Expand All @@ -105,7 +105,7 @@


## Shuffle array
{id: arrays-shuffle}
{id: array-shuffle}
{i: shuffle}

* Using [Random](random) behind the scenes.
Expand All @@ -116,7 +116,7 @@


## join
{id: arrays-join}
{id: array-join}

![](examples/arrays/join.cr)

Expand Down Expand Up @@ -174,6 +174,12 @@
![](examples/arrays/uniq.cr)
![](examples/arrays/uniq.out)

## First element of the array
{id: array-first}
{i: first}
{i: first?}

![](examples/arrays/first.cr)

## Permutations
{id: array-permutations}
Expand Down
15 changes: 15 additions & 0 deletions crystal/examples/arrays/first.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
names = ["Foo", "Bar"]
puts names.first # Foo
puts names[0] # Foo

names = [] of String
# puts names.first
# Unhandled exception: Empty enumerable (Enumerable::EmptyError)

# puts names[0]
# Unhandled exception: Index out of bounds (IndexError)

first = names.first?
puts first.nil? # true
first = names[0]?
puts first.nil? # true
8 changes: 8 additions & 0 deletions crystal/examples/hashes/clear.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
planets = {
"Mars" => 1,
"Jupyter" => 2,
"Saturn" => 3,
"Earth" => 4,
}
puts planets.clear # {}
puts planets # {}
8 changes: 8 additions & 0 deletions crystal/examples/hashes/delete.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
planets = {
"Mars" => 1,
"Jupyter" => 2,
"Saturn" => 3,
"Earth" => 4,
}
puts planets.delete("Mars")
puts planets
2 changes: 2 additions & 0 deletions crystal/examples/hashes/delete.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
{"Jupyter" => 2, "Saturn" => 3, "Earth" => 4}
19 changes: 19 additions & 0 deletions crystal/examples/hashes/dig.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
planets = {
"Mars" => {
"Traveler" => "Elon",
"People" => "Green",
},
"Jupyter" => 2,
"Saturn" => 3,
"Earth" => 4,
}
# puts planets["Mars"]["People"]
# Error: undefined method '[]' for Int32 (compile-time type is (Hash(String, String) | Int32))

puts planets.dig "Mars", "People" # Green

# puts planets.dig "Mars", "Date"
# Unhandled exception: Missing hash key: "Date" (KeyError)

date = puts planets.dig? "Mars", "Date" # nil
puts date.nil? # true
22 changes: 22 additions & 0 deletions crystal/examples/hashes/multi_dimensional.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
planets = {
"Mars" => {
"color" => "Red, brown and tan.",
},
"Jupyter" => {
"color" => "Brown, orange and tan, with white cloud stripes.",
},
"Saturn" => {
"color" => "Golden, brown, and blue-grey.",
},
"Earth" => {
"color" => "Blue, brown green and white.",
},
}

puts planets["Mars"]["color"]

# puts planets["Mercury"]
# Unhandled exception: Missing hash key: "Mercury" (KeyError)

# puts planets["Mars"]["rover"]
# Unhandled exception: Missing hash key: "rover" (KeyError)
11 changes: 11 additions & 0 deletions crystal/examples/hashes/reject.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
planets = {
"Mars" => 1,
"Jupyter" => 2,
"Saturn" => 3,
"Earth" => 4,
}
puts planets.reject("Mars")
puts planets

puts planets.reject!("Mars")
puts planets
4 changes: 4 additions & 0 deletions crystal/examples/hashes/reject.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"Jupyter" => 2, "Saturn" => 3, "Earth" => 4}
{"Mars" => 1, "Jupyter" => 2, "Saturn" => 3, "Earth" => 4}
{"Jupyter" => 2, "Saturn" => 3, "Earth" => 4}
{"Jupyter" => 2, "Saturn" => 3, "Earth" => 4}
12 changes: 12 additions & 0 deletions crystal/examples/hashes/select.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
planets = {
"Mars" => 1,
"Jupyter" => 2,
"Saturn" => 3,
"Earth" => 4,
}

puts planets.select { |name, number| number > 2 && name.includes?("r") }
puts planets

puts planets.select! { |name, number| number > 2 && name.includes?("r") }
puts planets
4 changes: 4 additions & 0 deletions crystal/examples/hashes/select.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"Saturn" => 3, "Earth" => 4}
{"Mars" => 1, "Jupyter" => 2, "Saturn" => 3, "Earth" => 4}
{"Saturn" => 3, "Earth" => 4}
{"Saturn" => 3, "Earth" => 4}
44 changes: 44 additions & 0 deletions crystal/hashes.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,47 @@
![](examples/hashes/merge.cr)
![](examples/hashes/merge.out)

## Delete - remove an element from a hash
{id: hash-delete}
{i: delete}

* `delete!` does not exist.

![](examples/hashes/delete.cr)
![](examples/hashes/delete.out)


## Reject - remove an element from a hash
{id: hash-reject}
{i: reject}
{i: reject!}

![](examples/hashes/reject.cr)
![](examples/hashes/reject.out)

## Clear - empty a hash
{id: hash-clear}
{i: clear}

![](examples/hashes/clear.cr)

## Select - keep certain key-value pairs
{id: hash-select}
{i: select}
{i: select!}

![](examples/hashes/select.cr)
![](examples/hashes/select.out)

## Multi-dimensional hash
{id: hash-multi-dimensional}

![](examples/hashes/multi_dimensional.cr)

## Dig a hash
{id: hash-dig}
{i: dig}
{i: dig?}

![](examples/hashes/dig.cr)

4 changes: 4 additions & 0 deletions crystal/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,7 @@ require "some_name" # Find it somewhere (standard library, src directory
* [Crystal Weekly](https://crystalweekly.com/)
* [Friends of Crystal](https://friendsofcrystal.com/)

* [Facebook page](https://www.facebook.com/Crystallang-1457045254320264/)
* [Facebook group](https://www.facebook.com/groups/crystallang/)
* [LinkedIn page](https://www.linkedin.com/company/crystal-language/)

0 comments on commit 5e6df77

Please sign in to comment.