From 8d55fa5bfadcf701d1c7f9c397c6ad477c23ee2e Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Fri, 24 Apr 2020 13:56:52 -0600 Subject: [PATCH 1/7] don't wrap `UInt8` in a tuple --- doc/src/manual/arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/src/manual/arrays.md b/doc/src/manual/arrays.md index a411856c8013e..c5aa51b549319 100644 --- a/doc/src/manual/arrays.md +++ b/doc/src/manual/arrays.md @@ -933,7 +933,7 @@ julia> convert.(Float32, [1, 2]) 1.0 2.0 -julia> ceil.((UInt8,), [1.2 3.4; 5.6 6.7]) +julia> ceil.(UInt8, [1.2 3.4; 5.6 6.7]) 2×2 Array{UInt8,2}: 0x02 0x04 0x06 0x07 From 9159a5c6619c487a4eaf060497b329ef99f29344 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Fri, 24 Apr 2020 14:02:15 -0600 Subject: [PATCH 2/7] Recommend to use tuples to scalarize an item. --- doc/src/manual/arrays.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/doc/src/manual/arrays.md b/doc/src/manual/arrays.md index c5aa51b549319..e0c563b410440 100644 --- a/doc/src/manual/arrays.md +++ b/doc/src/manual/arrays.md @@ -945,6 +945,16 @@ julia> string.(1:3, ". ", ["First", "Second", "Third"]) "3. Third" ``` +Sometimes, you have a non-scalar object like an array that you wish to treat as a scalar in a particular +broadcast expression. This is best done by wrapping that item in a [`Tuple`](@ref) +```jldoctest +julia> ([1, 2, 3], [4, 5, 6]) .+ ([1, 2, 3],) +([2, 4, 6], [5, 7, 9]) + +julia> ([1, 2, 3], [4, 5, 6]) .+ tuple([1, 2, 3]) +([2, 4, 6], [5, 7, 9]) +``` + ## Implementation The base array type in Julia is the abstract type [`AbstractArray{T,N}`](@ref). It is parameterized by From 2aa899ee765ff1d6b27204e4d308d9393ebad9b4 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Fri, 24 Apr 2020 17:03:01 -0600 Subject: [PATCH 3/7] Don't recommend to use Ref for scalarizing --- base/refpointer.jl | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/base/refpointer.jl b/base/refpointer.jl index 416a748261414..12a1f8826b9e4 100644 --- a/base/refpointer.jl +++ b/base/refpointer.jl @@ -18,17 +18,6 @@ converted to a native pointer to the data it references. There is no invalid (NULL) `Ref` in Julia, but a `C_NULL` instance of `Ptr` can be passed to a `ccall` Ref argument. - -# Use in broadcasting - -Broadcasting with `Ref(x)` treats `x` as a scalar: -```jldoctest -julia> isa.(Ref([1,2,3]), [Array, Dict, Int]) -3-element BitArray{1}: - 1 - 0 - 0 -``` """ Ref From 17d984f7b00638489170b147f4d293b64d24c470 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Tue, 28 Apr 2020 14:59:24 -0600 Subject: [PATCH 4/7] Update arrays.md --- doc/src/manual/arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/src/manual/arrays.md b/doc/src/manual/arrays.md index e0c563b410440..071d32142d9bc 100644 --- a/doc/src/manual/arrays.md +++ b/doc/src/manual/arrays.md @@ -945,7 +945,7 @@ julia> string.(1:3, ". ", ["First", "Second", "Third"]) "3. Third" ``` -Sometimes, you have a non-scalar object like an array that you wish to treat as a scalar in a particular +Sometimes, you have a non-scalar object like an array that you don't wish to broadcast into in a particular broadcast expression. This is best done by wrapping that item in a [`Tuple`](@ref) ```jldoctest julia> ([1, 2, 3], [4, 5, 6]) .+ ([1, 2, 3],) From f2d92c555904c8e1f5ca3c021fe8ec784f6985f5 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Tue, 28 Apr 2020 15:17:26 -0600 Subject: [PATCH 5/7] Update refpointer.jl --- base/refpointer.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/base/refpointer.jl b/base/refpointer.jl index 12a1f8826b9e4..88e59ae6a3cc5 100644 --- a/base/refpointer.jl +++ b/base/refpointer.jl @@ -18,6 +18,17 @@ converted to a native pointer to the data it references. There is no invalid (NULL) `Ref` in Julia, but a `C_NULL` instance of `Ptr` can be passed to a `ccall` Ref argument. + +# Use in broadcasting +`Ref` is sometimes used in broadcasting in order to treat the referenced values as a scalar: + +```jldoctest +julia> isa.(Ref([1,2,3]), [Array, Dict, Int]) +3-element BitArray{1}: + 1 + 0 + 0 +``` """ Ref From 329c1e45cea80cf5ae64b4d288a05c82991f3e4a Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Tue, 28 Apr 2020 15:17:28 -0600 Subject: [PATCH 6/7] add Matt's recommendation --- doc/src/manual/arrays.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/src/manual/arrays.md b/doc/src/manual/arrays.md index 071d32142d9bc..ded47f800a122 100644 --- a/doc/src/manual/arrays.md +++ b/doc/src/manual/arrays.md @@ -945,8 +945,9 @@ julia> string.(1:3, ". ", ["First", "Second", "Third"]) "3. Third" ``` -Sometimes, you have a non-scalar object like an array that you don't wish to broadcast into in a particular -broadcast expression. This is best done by wrapping that item in a [`Tuple`](@ref) +Sometimes, you want a container (like an array) that would normally participate in broadcast to be "protected" +from broadcast's behavior of iterating over all of its elements. By placing it inside another container +(like a single element [`Tuple`](@ref)) broadcast will treat it as a single value. ```jldoctest julia> ([1, 2, 3], [4, 5, 6]) .+ ([1, 2, 3],) ([2, 4, 6], [5, 7, 9]) From 1fc03258cfa0509d9eb1f490d1be784ee03a3f5b Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Wed, 29 Apr 2020 10:27:14 -0600 Subject: [PATCH 7/7] remove whitespace --- doc/src/manual/arrays.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/src/manual/arrays.md b/doc/src/manual/arrays.md index ded47f800a122..d789378091fd9 100644 --- a/doc/src/manual/arrays.md +++ b/doc/src/manual/arrays.md @@ -945,8 +945,8 @@ julia> string.(1:3, ". ", ["First", "Second", "Third"]) "3. Third" ``` -Sometimes, you want a container (like an array) that would normally participate in broadcast to be "protected" -from broadcast's behavior of iterating over all of its elements. By placing it inside another container +Sometimes, you want a container (like an array) that would normally participate in broadcast to be "protected" +from broadcast's behavior of iterating over all of its elements. By placing it inside another container (like a single element [`Tuple`](@ref)) broadcast will treat it as a single value. ```jldoctest julia> ([1, 2, 3], [4, 5, 6]) .+ ([1, 2, 3],)