github.com/jbomanson/flatten_as.cr
master / published Jun 30, 2018 / repository
Adds `Enumerable#flatten_as` that is like `flatten` with compile time control over what is flattened.
flatten_as
Adds Enumerable#flatten_as that is like flatten with compile time control
over what is flattened.
Installation
Add this to your application's shard.yml:
dependencies:
flatten_as:
github: jbomanson/flatten_as
Usage
This library adds the following method to Enumerable:
def flatten_as(target_type : U.class) : U forall U
Flattens self partially and converts the result into an enumerable of type
U.
require "flatten_as"
# Flattening into a one dimensional array.
[2, [4, [1]], [[3]]].flatten_as(Array(Int32)) # => [2, 4, 1, 3]
# Flattening into a two dimensional array.
[[4, [1]], [[3]]].flatten_as(Array(Array(Int32))) # => [[4, 1], [3]]
# Flattening into a two dimensional set.
[[4, [1]], [[3]]].flatten_as(Set(Set(Int32))) # => Set{Set{4, 1}, Set{3}}
# Casting elements.
[2, 4, 1, 3].flatten_as(Array(Int32 | Float32)) # => [2, 4, 1, 3]
More specifically, this flattening works as follows.
Suppose self is a type similar to Array(Indexable(Set(Iterator(X)))).
In general, it can be any type starting with some number of nested
Enumerable types.
The last type, X, can be anything other than a plain Enumerable type.
In other words, it can be an Int32 or some union type for example, even if
that union type includes an Enumerable.
The target type U must also an Enumerable.
Suppose it is Set(Array(Y)).
The important thing here is that all the Enumerable types that U begins
with must be constructible with a new method that takes no arguments and
must respond to a << method.
The last type, Y, is again anything other than a plain Enumerable type.
In this setting, the flattening method will:
-
cast the non Enumerable part
Xof self asY -
flatten the Enumerable part of self that extends beyond
Uin depth, namelySet(Iterator(...)) -
recreate the leading Enumerable part of self that is as deep as
Uas an instance of the target type, which in this case means to recreateArray(Indexable(...))asSet(Array(Y)).
Contributing
- Fork it ( https://github.com/jbomanson/flatten_as/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- jbomanson Jori Bomanson - creator, maintainer
API
- Enumerable(T)
The
Enumerablemixin provides collection classes with several traversal, searching, filtering and querying methods.