Skip to content

Commit

Permalink
added another example for broadcasting customization
Browse files Browse the repository at this point in the history
  • Loading branch information
chethega committed Feb 2, 2019
1 parent a811eac commit 4bf723b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions doc/src/manual/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,30 @@ ways of doing so:
* Iterating over the `CartesianIndices` of the `axes(::Broadcasted)` and using
indexing with the resulting `CartesianIndex` object to compute the result.

#### Example: Preventing materialization for reductions

An instructive example is to permit reductions on broadcasted objects, without
materializing the intermediate results. We can prevent materialization by
```julia
struct lazy end
struct Lazy{T}
arg::T
end
Base.Broadcast.materialize(ell::Lazy) = ell.arg
Base.Broadcast.broadcasted(::Type{lazy}, arg) = Lazy(arg)
```
Now, we can write e.g.
```julia
v = rand(10_000)
w = rand(10_000)
lazyprod = lazy.(v .* w)
dotproduct = sum(lazyprod)
```
This works because `Broadcasted` objects behave like an `Array` in many ways: They are
iterable, have a shape, and appropriate `getindex` to compute elements on demand. However,
they do not have an `eltype`, even if they are correctly inferred, which makes it somewhat
awkward to write type-stable reductions, and is part of the reason that they are not `<:AbstractArray`.

### [Writing binary broadcasting rules](@id writing-binary-broadcasting-rules)

The precedence rules are defined by binary `BroadcastStyle` calls:
Expand Down

0 comments on commit 4bf723b

Please sign in to comment.