Skip to content

Commit

Permalink
Edit YARD-README.md to match README.md
Browse files Browse the repository at this point in the history
One of these is for Yard to generate documentation (also for rubydoc.info), and
the other is for GitHub. People usually send patches with updates to README.md,
but they do not edit YARD-README.md to match.
  • Loading branch information
alexdowad committed Nov 5, 2017
1 parent 17dcb44 commit 7947361
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ an item using an integral index:

``` ruby
set = Immutable::SortedSet['toast', 'jam', 'bacon'] # => Immutable::SortedSet["bacon", "jam", "toast"]
set.first # => "bacon"
set.last # => "toast"
set[1] # => "jam"
set.first # => "bacon"
set.last # => "toast"
set[1] # => "jam"
```

You can also specify the sort order using a block:
Expand Down Expand Up @@ -181,6 +181,8 @@ original = Immutable::List[1, 2, 3]
copy = original.add(0) # => Immutable::List[0, 1, 2, 3]
```

Notice how modifying a list actually returns a new list.

### Laziness

`Immutable::List` is lazy where possible. It tries to defer processing items until
Expand Down
28 changes: 12 additions & 16 deletions YARD-README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ person.key?(:name) # => true
Since it is immutable, `Immutable::Hash` doesn't provide an assignment (`Hash#[]=`) method. However, `Hash#put` can accept a block which transforms the value associated with a given key:

``` ruby
counters.put(:odds) { |value| value + 1 } # => Immutable::Hash[:odds => 1, :evens => 0]
counters = Immutable::Hash[evens: 0, odds: 0]
counters.put(:odds) { |n| n + 1 } # => Immutable::Hash[:odds => 1, :evens => 0]
```

Or more succinctly:
Expand Down Expand Up @@ -167,34 +168,29 @@ copy = original.add(0) # => Immutable::List[0, 1, 2, 3]

Notice how modifying a list actually returns a new list.


### Laziness

`Immutable::List` is lazy where possible. It tries to defer processing items until absolutely necessary. For example, given a crude function to detect prime numbers:
`Immutable::List` is lazy where possible. It tries to defer processing items until
absolutely necessary. For example, the following code will only call
`Prime.prime?` as many times as necessary to generate the first 3 prime numbers
between 10,000 and 1,000,000:

``` ruby
def prime?(number)
2.upto(Math.sqrt(number).round) do |integer|
return false if (number % integer).zero?
end
true
end
```

The following code will only call `#prime?` as many times as necessary to generate the first 3 prime numbers between 10,000 and 1,000,000:
require 'prime'

``` ruby
Immutable.interval(10_000, 1_000_000).select do |number|
prime?(number)
Prime.prime?(number)
end.take(3)
# => 0.0009s
```

Compare that to the conventional equivalent which needs to calculate all possible values in the range before taking the first three:
Compare that to the conventional equivalent which needs to
calculate all possible values in the range before taking the
first three:

``` ruby
(10000..1000000).select do |number|
prime?(number)
Prime.prime?(number)
end.take(3)
# => 10s
```
Expand Down

0 comments on commit 7947361

Please sign in to comment.