Skip to content

Commit

Permalink
Merge pull request #2 from CliMA/ck/rename
Browse files Browse the repository at this point in the history
Rename to NullBroadcasts + NullBroadcasted
  • Loading branch information
charleskawczynski authored Jan 31, 2025
2 parents d35d52a + 0a21cc8 commit 736b8fe
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 107 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AbsentTypes.jl Release Notes
NullBroadcasts.jl Release Notes
============================

Main
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = "AbsentTypes"
name = "NullBroadcasts"
uuid = "0d71be07-595a-4f89-9529-4065a4ab43a6"
authors = ["CliMA Contributors <clima-software@caltech.edu>"]
version = "0.1.0"
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# AbsentTypes.jl
A small package for defining absent (i.e., null) types and behaviors
# NullBroadcasts.jl

A small package for defining null broadcasted types and behaviors
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
DocumenterCitations = "daee34ce-89f3-4625-b898-19384cb65244"
AbsentTypes = "0d71be07-595a-4f89-9529-4065a4ab43a6"
NullBroadcasts = "0d71be07-595a-4f89-9529-4065a4ab43a6"
8 changes: 4 additions & 4 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Documenter, DocumenterCitations
import AbsentTypes
import NullBroadcasts

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

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

Documenter.makedocs(;
plugins = [bib],
sitename = "AbsentTypes.jl",
sitename = "NullBroadcasts.jl",
format = format,
checkdocs = :exports,
clean = true,
doctest = true,
modules = [AbsentTypes],
modules = [NullBroadcasts],
pages = Any["Home"=>"index.md", "API"=>"api.md", "References"=>"references.md"],
)

Documenter.deploydocs(
repo = "github.com/CliMA/AbsentTypes.jl.git",
repo = "github.com/CliMA/NullBroadcasts.jl.git",
target = "build",
push_preview = true,
devbranch = "main",
Expand Down
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# AbsentTypes.jl
# NullBroadcasts.jl

76 changes: 0 additions & 76 deletions src/AbsentTypes.jl

This file was deleted.

81 changes: 81 additions & 0 deletions src/NullBroadcasts.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
module NullBroadcasts

"""
NullBroadcasted()
A `Base.AbstractBroadcasted` that represents arithmetic object.
An `NullBroadcasted()` can be added to, subtracted from, or multiplied by any value in a
broadcast expression without incurring a runtime performance penalty.
For example, the following rules hold when broadcasting instances of `NullBroadcasted`:
```
1 + NullBroadcasted() == 1
NullBroadcasted() + 1 == 1
1 - NullBroadcasted() == 1
1 * NullBroadcasted() == NullBroadcasted()
1 / NullBroadcasted() == NullBroadcasted()
```
"""
struct NullBroadcasted <: Base.AbstractBroadcasted end
Base.broadcastable(x::NullBroadcasted) = x

struct NullBroadcastedStyle <: Base.BroadcastStyle end
Base.BroadcastStyle(::Type{<:NullBroadcasted}) = NullBroadcasted()

# Specialize on AbstractArrayStyle to avoid ambiguities with AbstractBroadcasted.
Base.BroadcastStyle(::NullBroadcasted, ::Base.Broadcast.AbstractArrayStyle) =
NullBroadcasted()
Base.BroadcastStyle(::Base.Broadcast.AbstractArrayStyle, ::NullBroadcasted) =
NullBroadcasted()

# Add another method to avoid ambiguity between the previous two.
Base.BroadcastStyle(::NullBroadcasted, ::NullBroadcasted) = NullBroadcasted()

broadcasted_sum(args) =
if isempty(args)
NullBroadcasted()
elseif length(args) == 1
args[1]
else
Base.broadcasted(+, args...)
end
Base.broadcasted(::NullBroadcasted, ::typeof(+), args...) =
broadcasted_sum(filter(arg -> !(arg isa NullBroadcasted), args))

Base.broadcasted(op::typeof(-), ::NullBroadcasted, arg) = Base.broadcasted(op, arg)
Base.broadcasted(op::typeof(-), arg, ::NullBroadcasted) =
Base.broadcasted(Base.identity, arg)
Base.broadcasted(op::typeof(-), a::NullBroadcasted) = NullBroadcasted()
Base.broadcasted(op::typeof(-), a::NullBroadcasted, ::NullBroadcasted) =
Base.broadcasted(op, a)

Base.broadcasted(op::typeof(+), ::NullBroadcasted, args...) = Base.broadcasted(op, args...)
Base.broadcasted(op::typeof(+), arg, ::NullBroadcasted) = Base.broadcasted(op, arg)
Base.broadcasted(op::typeof(+), a::NullBroadcasted, ::NullBroadcasted) =
Base.broadcasted(op, a)

Base.broadcasted(op::typeof(*), ::NullBroadcasted, args...) = NullBroadcasted()
Base.broadcasted(op::typeof(*), arg, ::NullBroadcasted) = NullBroadcasted()
Base.broadcasted(op::typeof(*), ::NullBroadcasted, ::NullBroadcasted) = NullBroadcasted()
Base.broadcasted(op::typeof(/), ::NullBroadcasted, args...) = NullBroadcasted()
Base.broadcasted(op::typeof(/), arg, ::NullBroadcasted) = NullBroadcasted()
Base.broadcasted(op::typeof(/), ::NullBroadcasted, ::NullBroadcasted) = NullBroadcasted()

function skip_materialize(dest, bc::Base.Broadcast.Broadcasted)
if typeof(bc.f) <: typeof(+) || typeof(bc.f) <: typeof(-)
if length(bc.args) == 2 &&
bc.args[1] === dest &&
bc.args[2] === Base.Broadcast.Broadcasted(NullBroadcasted, ())
return true
else
return false
end
else
return false
end
end

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

end # module NullBroadcasts
42 changes: 21 additions & 21 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ julia --project
using Revise; using TestEnv; TestEnv.activat(); include("test/runtests.jl")
=#
using Test
using AbsentTypes
using AbsentTypes: Absent
using NullBroadcasts
using NullBroadcasts: NullBroadcasted
using Aqua
using LazyBroadcast: lazy
import Base.Broadcast: instantiate, materialize, Broadcasted, DefaultArrayStyle

@testset "Absent" begin
@testset "NullBroadcasted" begin
x = [1]
a = Absent()
a = NullBroadcasted()
@test typeof(lazy.(x .+ a)) <: Broadcasted{
DefaultArrayStyle{1},
Tuple{Base.OneTo{Int64}},
Expand All @@ -24,8 +24,8 @@ import Base.Broadcast: instantiate, materialize, Broadcasted, DefaultArrayStyle
typeof(+),
Tuple{Vector{Int64}},
}
@test lazy.(a .* x) isa Absent
@test lazy.(a ./ x) isa Absent
@test lazy.(a .* x) isa NullBroadcasted
@test lazy.(a ./ x) isa NullBroadcasted

# +
@test materialize(lazy.(a .+ x .+ 1)) == [2]
Expand All @@ -38,30 +38,30 @@ import Base.Broadcast: instantiate, materialize, Broadcasted, DefaultArrayStyle
@test materialize(lazy.(a .- 1 .- x)) == [-2]
@test materialize(lazy.(1 .- a .- x)) == [0]
@test materialize(lazy.(1 .- x .- a)) == [0]
@test materialize(lazy.(a .- a)) == Absent()
@test materialize(lazy.(a .- a)) == NullBroadcasted()
@test materialize(lazy.(1 .- 1 .+ a .- a)) == 0
@test materialize(lazy.(x .- x .+ a .- a)) == [0]

# *
@test materialize(lazy.(a .* x .* 1)) == Absent()
@test materialize(lazy.(a .* 1 .* x)) == Absent()
@test materialize(lazy.(1 .* a .* x)) == Absent()
@test materialize(lazy.(1 .* x .* a)) == Absent()
@test materialize(lazy.(a .* x .* 1)) == NullBroadcasted()
@test materialize(lazy.(a .* 1 .* x)) == NullBroadcasted()
@test materialize(lazy.(1 .* a .* x)) == NullBroadcasted()
@test materialize(lazy.(1 .* x .* a)) == NullBroadcasted()

# /
@test materialize(lazy.(a ./ x ./ 1)) == Absent()
@test materialize(lazy.(a ./ 1 ./ x)) == Absent()
@test materialize(lazy.(1 ./ a ./ x)) == Absent()
@test materialize(lazy.(1 ./ x ./ a)) == Absent()
@test materialize(lazy.(a ./ x ./ 1)) == NullBroadcasted()
@test materialize(lazy.(a ./ 1 ./ x)) == NullBroadcasted()
@test materialize(lazy.(1 ./ a ./ x)) == NullBroadcasted()
@test materialize(lazy.(1 ./ x ./ a)) == NullBroadcasted()

@test_throws MethodError Absent() + 1
@test_throws MethodError Absent() - 1
@test_throws MethodError Absent() * 1
@test_throws MethodError Absent() / 1
@test_throws MethodError NullBroadcasted() + 1
@test_throws MethodError NullBroadcasted() - 1
@test_throws MethodError NullBroadcasted() * 1
@test_throws MethodError NullBroadcasted() / 1

@test materialize(Absent()) isa Absent
@test materialize(NullBroadcasted()) isa NullBroadcasted
end

@testset "Aqua" begin
Aqua.test_all(AbsentTypes)
Aqua.test_all(NullBroadcasts)
end

0 comments on commit 736b8fe

Please sign in to comment.