Skip to content

Commit 736b8fe

Browse files
Merge pull request #2 from CliMA/ck/rename
Rename to NullBroadcasts + NullBroadcasted
2 parents d35d52a + 0a21cc8 commit 736b8fe

File tree

9 files changed

+113
-107
lines changed

9 files changed

+113
-107
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
AbsentTypes.jl Release Notes
1+
NullBroadcasts.jl Release Notes
22
============================
33

44
Main

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name = "AbsentTypes"
1+
name = "NullBroadcasts"
22
uuid = "0d71be07-595a-4f89-9529-4065a4ab43a6"
33
authors = ["CliMA Contributors <[email protected]>"]
44
version = "0.1.0"

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
# AbsentTypes.jl
2-
A small package for defining absent (i.e., null) types and behaviors
1+
# NullBroadcasts.jl
2+
3+
A small package for defining null broadcasted types and behaviors

docs/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
33
DocumenterCitations = "daee34ce-89f3-4625-b898-19384cb65244"
4-
AbsentTypes = "0d71be07-595a-4f89-9529-4065a4ab43a6"
4+
NullBroadcasts = "0d71be07-595a-4f89-9529-4065a4ab43a6"

docs/make.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Documenter, DocumenterCitations
2-
import AbsentTypes
2+
import NullBroadcasts
33

44
bib = DocumenterCitations.CitationBibliography(joinpath(@__DIR__, "refs.bib"))
55

@@ -15,17 +15,17 @@ format = Documenter.HTML(
1515

1616
Documenter.makedocs(;
1717
plugins = [bib],
18-
sitename = "AbsentTypes.jl",
18+
sitename = "NullBroadcasts.jl",
1919
format = format,
2020
checkdocs = :exports,
2121
clean = true,
2222
doctest = true,
23-
modules = [AbsentTypes],
23+
modules = [NullBroadcasts],
2424
pages = Any["Home"=>"index.md", "API"=>"api.md", "References"=>"references.md"],
2525
)
2626

2727
Documenter.deploydocs(
28-
repo = "github.com/CliMA/AbsentTypes.jl.git",
28+
repo = "github.com/CliMA/NullBroadcasts.jl.git",
2929
target = "build",
3030
push_preview = true,
3131
devbranch = "main",

docs/src/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# AbsentTypes.jl
1+
# NullBroadcasts.jl
22

src/AbsentTypes.jl

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/NullBroadcasts.jl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
module NullBroadcasts
2+
3+
"""
4+
NullBroadcasted()
5+
6+
A `Base.AbstractBroadcasted` that represents arithmetic object.
7+
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.
10+
11+
For example, the following rules hold when broadcasting instances of `NullBroadcasted`:
12+
```
13+
1 + NullBroadcasted() == 1
14+
NullBroadcasted() + 1 == 1
15+
1 - NullBroadcasted() == 1
16+
1 * NullBroadcasted() == NullBroadcasted()
17+
1 / NullBroadcasted() == NullBroadcasted()
18+
```
19+
"""
20+
struct NullBroadcasted <: Base.AbstractBroadcasted end
21+
Base.broadcastable(x::NullBroadcasted) = x
22+
23+
struct NullBroadcastedStyle <: Base.BroadcastStyle end
24+
Base.BroadcastStyle(::Type{<:NullBroadcasted}) = NullBroadcasted()
25+
26+
# Specialize on AbstractArrayStyle to avoid ambiguities with AbstractBroadcasted.
27+
Base.BroadcastStyle(::NullBroadcasted, ::Base.Broadcast.AbstractArrayStyle) =
28+
NullBroadcasted()
29+
Base.BroadcastStyle(::Base.Broadcast.AbstractArrayStyle, ::NullBroadcasted) =
30+
NullBroadcasted()
31+
32+
# Add another method to avoid ambiguity between the previous two.
33+
Base.BroadcastStyle(::NullBroadcasted, ::NullBroadcasted) = NullBroadcasted()
34+
35+
broadcasted_sum(args) =
36+
if isempty(args)
37+
NullBroadcasted()
38+
elseif length(args) == 1
39+
args[1]
40+
else
41+
Base.broadcasted(+, args...)
42+
end
43+
Base.broadcasted(::NullBroadcasted, ::typeof(+), args...) =
44+
broadcasted_sum(filter(arg -> !(arg isa NullBroadcasted), args))
45+
46+
Base.broadcasted(op::typeof(-), ::NullBroadcasted, arg) = Base.broadcasted(op, arg)
47+
Base.broadcasted(op::typeof(-), arg, ::NullBroadcasted) =
48+
Base.broadcasted(Base.identity, arg)
49+
Base.broadcasted(op::typeof(-), a::NullBroadcasted) = NullBroadcasted()
50+
Base.broadcasted(op::typeof(-), a::NullBroadcasted, ::NullBroadcasted) =
51+
Base.broadcasted(op, a)
52+
53+
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+
58+
Base.broadcasted(op::typeof(*), ::NullBroadcasted, args...) = NullBroadcasted()
59+
Base.broadcasted(op::typeof(*), arg, ::NullBroadcasted) = NullBroadcasted()
60+
Base.broadcasted(op::typeof(*), ::NullBroadcasted, ::NullBroadcasted) = NullBroadcasted()
61+
Base.broadcasted(op::typeof(/), ::NullBroadcasted, args...) = NullBroadcasted()
62+
Base.broadcasted(op::typeof(/), arg, ::NullBroadcasted) = NullBroadcasted()
63+
Base.broadcasted(op::typeof(/), ::NullBroadcasted, ::NullBroadcasted) = NullBroadcasted()
64+
65+
function skip_materialize(dest, bc::Base.Broadcast.Broadcasted)
66+
if typeof(bc.f) <: typeof(+) || typeof(bc.f) <: typeof(-)
67+
if length(bc.args) == 2 &&
68+
bc.args[1] === dest &&
69+
bc.args[2] === Base.Broadcast.Broadcasted(NullBroadcasted, ())
70+
return true
71+
else
72+
return false
73+
end
74+
else
75+
return false
76+
end
77+
end
78+
79+
Base.Broadcast.instantiate(bc::Base.Broadcast.Broadcasted{NullBroadcastedStyle}) = x
80+
81+
end # module NullBroadcasts

test/runtests.jl

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ julia --project
33
using Revise; using TestEnv; TestEnv.activat(); include("test/runtests.jl")
44
=#
55
using Test
6-
using AbsentTypes
7-
using AbsentTypes: Absent
6+
using NullBroadcasts
7+
using NullBroadcasts: NullBroadcasted
88
using Aqua
99
using LazyBroadcast: lazy
1010
import Base.Broadcast: instantiate, materialize, Broadcasted, DefaultArrayStyle
1111

12-
@testset "Absent" begin
12+
@testset "NullBroadcasted" begin
1313
x = [1]
14-
a = Absent()
14+
a = NullBroadcasted()
1515
@test typeof(lazy.(x .+ a)) <: Broadcasted{
1616
DefaultArrayStyle{1},
1717
Tuple{Base.OneTo{Int64}},
@@ -24,8 +24,8 @@ import Base.Broadcast: instantiate, materialize, Broadcasted, DefaultArrayStyle
2424
typeof(+),
2525
Tuple{Vector{Int64}},
2626
}
27-
@test lazy.(a .* x) isa Absent
28-
@test lazy.(a ./ x) isa Absent
27+
@test lazy.(a .* x) isa NullBroadcasted
28+
@test lazy.(a ./ x) isa NullBroadcasted
2929

3030
# +
3131
@test materialize(lazy.(a .+ x .+ 1)) == [2]
@@ -38,30 +38,30 @@ import Base.Broadcast: instantiate, materialize, Broadcasted, DefaultArrayStyle
3838
@test materialize(lazy.(a .- 1 .- x)) == [-2]
3939
@test materialize(lazy.(1 .- a .- x)) == [0]
4040
@test materialize(lazy.(1 .- x .- a)) == [0]
41-
@test materialize(lazy.(a .- a)) == Absent()
41+
@test materialize(lazy.(a .- a)) == NullBroadcasted()
4242
@test materialize(lazy.(1 .- 1 .+ a .- a)) == 0
4343
@test materialize(lazy.(x .- x .+ a .- a)) == [0]
4444

4545
# *
46-
@test materialize(lazy.(a .* x .* 1)) == Absent()
47-
@test materialize(lazy.(a .* 1 .* x)) == Absent()
48-
@test materialize(lazy.(1 .* a .* x)) == Absent()
49-
@test materialize(lazy.(1 .* x .* a)) == Absent()
46+
@test materialize(lazy.(a .* x .* 1)) == NullBroadcasted()
47+
@test materialize(lazy.(a .* 1 .* x)) == NullBroadcasted()
48+
@test materialize(lazy.(1 .* a .* x)) == NullBroadcasted()
49+
@test materialize(lazy.(1 .* x .* a)) == NullBroadcasted()
5050

5151
# /
52-
@test materialize(lazy.(a ./ x ./ 1)) == Absent()
53-
@test materialize(lazy.(a ./ 1 ./ x)) == Absent()
54-
@test materialize(lazy.(1 ./ a ./ x)) == Absent()
55-
@test materialize(lazy.(1 ./ x ./ a)) == Absent()
52+
@test materialize(lazy.(a ./ x ./ 1)) == NullBroadcasted()
53+
@test materialize(lazy.(a ./ 1 ./ x)) == NullBroadcasted()
54+
@test materialize(lazy.(1 ./ a ./ x)) == NullBroadcasted()
55+
@test materialize(lazy.(1 ./ x ./ a)) == NullBroadcasted()
5656

57-
@test_throws MethodError Absent() + 1
58-
@test_throws MethodError Absent() - 1
59-
@test_throws MethodError Absent() * 1
60-
@test_throws MethodError Absent() / 1
57+
@test_throws MethodError NullBroadcasted() + 1
58+
@test_throws MethodError NullBroadcasted() - 1
59+
@test_throws MethodError NullBroadcasted() * 1
60+
@test_throws MethodError NullBroadcasted() / 1
6161

62-
@test materialize(Absent()) isa Absent
62+
@test materialize(NullBroadcasted()) isa NullBroadcasted
6363
end
6464

6565
@testset "Aqua" begin
66-
Aqua.test_all(AbsentTypes)
66+
Aqua.test_all(NullBroadcasts)
6767
end

0 commit comments

Comments
 (0)