Skip to content

Commit b768fae

Browse files
Update readme
Docs + fixes
1 parent 736b8fe commit b768fae

File tree

4 files changed

+71
-7
lines changed

4 files changed

+71
-7
lines changed

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
11
# NullBroadcasts.jl
22

3-
A small package for defining null broadcasted types and behaviors
3+
|||
4+
|---------------------:|:----------------------------------------------|
5+
| **Docs Build** | [![docs build][docs-bld-img]][docs-bld-url] |
6+
| **Documentation** | [![dev][docs-dev-img]][docs-dev-url] |
7+
| **GHA CI** | [![gha ci][gha-ci-img]][gha-ci-url] |
8+
| **Code Coverage** | [![codecov][codecov-img]][codecov-url] |
9+
10+
[docs-bld-img]: https://github.com/CliMA/NullBroadcasts.jl/actions/workflows/docs.yml/badge.svg
11+
[docs-bld-url]: https://github.com/CliMA/NullBroadcasts.jl/actions/workflows/docs.yml
12+
13+
[docs-dev-img]: https://img.shields.io/badge/docs-dev-blue.svg
14+
[docs-dev-url]: https://CliMA.github.io/NullBroadcasts.jl/dev/
15+
16+
[gha-ci-img]: https://github.com/CliMA/NullBroadcasts.jl/actions/workflows/ci.yml/badge.svg
17+
[gha-ci-url]: https://github.com/CliMA/NullBroadcasts.jl/actions/workflows/ci.yml
18+
19+
[codecov-img]: https://codecov.io/gh/CliMA/NullBroadcasts.jl/branch/main/graph/badge.svg
20+
[codecov-url]: https://codecov.io/gh/CliMA/NullBroadcasts.jl
21+
22+
This package defines `NullBroadcasted()`, which can be added to, subtracted
23+
from, or multiplied by any value in a broadcast expression without incurring a
24+
runtime performance penalty.
25+
26+
# Example
27+
28+
```julia
29+
using NullBroadcasts: NullBroadcasted
30+
using Test
31+
32+
x = [1]; y = [1]
33+
a = NullBroadcasted()
34+
@. x = x + a + y
35+
@test x[1] == 2 # passes
36+
37+
x = [1]; y = [1]
38+
@. x = x - a + y
39+
@test x[1] == 2 # passes
40+
41+
@. x = x + (x * a) # equivalent to @. x = x
42+
43+
@. x = x * a # not allowed, errors
44+
@. x = a # not allowed, errors
45+
```

docs/src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# NullBroadcasts.jl
22

3+
Under construction. Please see the readme for the latest documentation.

src/NullBroadcasts.jl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ module NullBroadcasts
55
66
A `Base.AbstractBroadcasted` that represents arithmetic object.
77
8-
An `NullBroadcasted()` can be added to, subtracted from, or multiplied by any value in a
9-
broadcast expression without incurring a runtime performance penalty.
8+
An `NullBroadcasted()` can be added to, subtracted from, or multiplied by any
9+
value in a broadcast expression without incurring a runtime performance
10+
penalty.
11+
12+
For example, the following rules hold when broadcasting instances of
13+
`NullBroadcasted`:
1014
11-
For example, the following rules hold when broadcasting instances of `NullBroadcasted`:
1215
```
1316
1 + NullBroadcasted() == 1
1417
NullBroadcasted() + 1 == 1
@@ -51,9 +54,10 @@ Base.broadcasted(op::typeof(-), a::NullBroadcasted, ::NullBroadcasted) =
5154
Base.broadcasted(op, a)
5255

5356
Base.broadcasted(op::typeof(+), ::NullBroadcasted, args...) = Base.broadcasted(op, args...)
54-
Base.broadcasted(op::typeof(+), arg, ::NullBroadcasted) = Base.broadcasted(op, arg)
55-
Base.broadcasted(op::typeof(+), a::NullBroadcasted, ::NullBroadcasted) =
56-
Base.broadcasted(op, a)
57+
Base.broadcasted(op::typeof(+), arg, ::NullBroadcasted, args...) =
58+
Base.broadcasted(op, arg, args...)
59+
Base.broadcasted(op::typeof(+), a::NullBroadcasted, ::NullBroadcasted, args...) =
60+
Base.broadcasted(op, a, args...)
5761

5862
Base.broadcasted(op::typeof(*), ::NullBroadcasted, args...) = NullBroadcasted()
5963
Base.broadcasted(op::typeof(*), arg, ::NullBroadcasted) = NullBroadcasted()
@@ -62,6 +66,8 @@ Base.broadcasted(op::typeof(/), ::NullBroadcasted, args...) = NullBroadcasted()
6266
Base.broadcasted(op::typeof(/), arg, ::NullBroadcasted) = NullBroadcasted()
6367
Base.broadcasted(op::typeof(/), ::NullBroadcasted, ::NullBroadcasted) = NullBroadcasted()
6468

69+
Base.broadcasted(op::typeof(identity), a::NullBroadcasted) = a
70+
6571
function skip_materialize(dest, bc::Base.Broadcast.Broadcasted)
6672
if typeof(bc.f) <: typeof(+) || typeof(bc.f) <: typeof(-)
6773
if length(bc.args) == 2 &&
@@ -78,4 +84,9 @@ end
7884

7985
Base.Broadcast.instantiate(bc::Base.Broadcast.Broadcasted{NullBroadcastedStyle}) = x
8086

87+
Base.Broadcast.materialize!(dest, x::NullBroadcasted) =
88+
error("NullBroadcasted objects cannot be materialized.")
89+
Base.Broadcast.materialize(dest, x::NullBroadcasted) =
90+
error("NullBroadcasted objects cannot be materialized.")
91+
8192
end # module NullBroadcasts

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import Base.Broadcast: instantiate, materialize, Broadcasted, DefaultArrayStyle
3232
@test materialize(lazy.(a .+ 1 .+ x)) == [2]
3333
@test materialize(lazy.(1 .+ a .+ x)) == [2]
3434
@test materialize(lazy.(1 .+ x .+ a)) == [2]
35+
@test materialize(lazy.(x .+ a .+ x)) == [2]
36+
@test materialize(lazy.(x .+ a .+ x)) == [2]
3537

3638
# -
3739
@test materialize(lazy.(a .- x .- 1)) == [-2]
@@ -47,6 +49,10 @@ import Base.Broadcast: instantiate, materialize, Broadcasted, DefaultArrayStyle
4749
@test materialize(lazy.(a .* 1 .* x)) == NullBroadcasted()
4850
@test materialize(lazy.(1 .* a .* x)) == NullBroadcasted()
4951
@test materialize(lazy.(1 .* x .* a)) == NullBroadcasted()
52+
@test materialize(lazy.(x .+ (x .* a))) == [1]
53+
@test materialize(lazy.(x .- (x .* a))) == [1]
54+
@test materialize(lazy.((x .* a) .+ x)) == [1]
55+
@test materialize(lazy.((x .* a) .- x)) == [-1]
5056

5157
# /
5258
@test materialize(lazy.(a ./ x ./ 1)) == NullBroadcasted()
@@ -60,6 +66,10 @@ import Base.Broadcast: instantiate, materialize, Broadcasted, DefaultArrayStyle
6066
@test_throws MethodError NullBroadcasted() / 1
6167

6268
@test materialize(NullBroadcasted()) isa NullBroadcasted
69+
70+
@test_throws ErrorException("NullBroadcasted objects cannot be materialized.") @. x =
71+
x * a
72+
@test_throws ErrorException("NullBroadcasted objects cannot be materialized.") @. x = a
6373
end
6474

6575
@testset "Aqua" begin

0 commit comments

Comments
 (0)