Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions demo/core/enumerable/squeeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Enumerable#squeeze

require 'facets/enumerable/squeeze'

Returns a new array with consecutive duplicate entries collapsed. Unlike
`Array#uniq`, duplicate values are only removed when they are adjacent.

[1, 2, 2, 3, 3, 2, 1].squeeze.assert == [1, 2, 3, 2, 1]

When the enumerable is sorted first, adjacent duplicates become grouped, so
the result matches `uniq`.

[1, 2, 2, 3, 3, 2, 1].sort.squeeze.assert == [1, 2, 3]

Pass one or more values to squeeze only those values.

[1, 2, 2, 3, 3, 2, 1].squeeze(3).assert == [1, 2, 2, 3, 2, 1]
14 changes: 14 additions & 0 deletions demo/core/enumerator/lazy/squeeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Enumerator::Lazy#squeeze

require 'facets/enumerator/lazy/squeeze'

Provides a lazy version of `Enumerable#squeeze`, collapsing consecutive
duplicates while preserving lazy enumeration.

enum = [1, 2, 2, 3, 3, 2, 1].lazy.squeeze
enum.to_a.assert == [1, 2, 3, 2, 1]

Pass values to squeeze only matching entries.

enum = [1, 2, 2, 3, 3, 2, 1].lazy.squeeze(3)
enum.to_a.assert == [1, 2, 2, 3, 2, 1]
17 changes: 17 additions & 0 deletions demo/core/hash/fetch_nested.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Hash#fetch_nested

require 'facets/hash/fetch_nested'

Fetches a nested value by walking a series of keys. If any key is missing,
`nil` is returned instead of raising `KeyError`.

data = {'hello' => {'world' => 42}}
data.fetch_nested('hello', 'world').assert == 42

data.fetch_nested('hello', 'missing').assert == nil

When a block is given, it is called with the requested keys when the nested
path cannot be fetched.

fallback = data.fetch_nested('hello', 'missing') { |*keys| keys.join('.') }
fallback.assert == 'hello.missing'
23 changes: 23 additions & 0 deletions demo/core/string/rotate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## String#rotate

require 'facets/string/rotate'

Returns a copy of the string rotated left by the given count.

'abcdefgh'.rotate(2).assert == 'cdefghab'

Negative counts rotate from the right.

'abcdefgh'.rotate(-2).assert == 'ghabcdef'

## String#rotate!

Performs the rotation in place.

string = 'abcdefgh'
string.rotate!(2)
string.assert == 'cdefghab'

string = 'abcdefgh'
string.rotate!(-2)
string.assert == 'ghabcdef'