Isupercase with String Array how tu use?

#1

isupercase with String Array how tu use ? How to convet Array a to Array of Char?

julia> a
3-element Array{String,1}:
“a”
“5”
“A”

julia> isuppercase.(a)
ERROR: MethodError: no method matching isuppercase(::String)
Closest candidates are:
isuppercase(::AbstractChar) at strings/unicode.jl:313
Stacktrace:
[1] _broadcast_getindex_evalf at .\broadcast.jl:574 [inlined]
[2] _broadcast_getindex at .\broadcast.jl:547 [inlined]
[3] getindex at .\broadcast.jl:507 [inlined]
[4] copy at .\broadcast.jl:758 [inlined]
[5] materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(isuppercase),Tuple{Array{String,1}}}) at .\broadcast.jl:724
[6] top-level scope at none:0
Paul

#3

[s[1] for s in a]
isuppercase.([s[1] for s in a])

#4

Won’t this fail for unicode characters with lengths longer than 1? Not at my computer now, but how about isuppercase.(first.(a))?

Are all the strings guaranteed to consist of a single character?

#5

first looks good to me. Although I think the index [1] works too (because its 1). I have no info about the string’s charcount.

#6

Thanks , and one more ~!

[a[i] not works
[a[i][1] this works :

julia> isuppercase.([a[i][1] for i=1:length(a)])
3-element BitArray{1}:
false
false
true

Paul

#7

Its firstindex rather than first.

#8

I’m a bit surprised by the performance here:

julia> a = ["a", "5", "A"]

julia> @btime [isuppercase(first(s)) for s in $a]
  79.761 ns (2 allocations: 112 bytes)
3-element Array{Bool,1}:
 false
 false
  true

julia> @btime isuppercase.(first.($a))
  433.894 ns (3 allocations: 4.31 KiB)
3-element BitArray{1}:
 false
 false
  true
#9

The second is allocating much, much more memory.

#10

Yes, but it shouldn’t, as far as I can tell.

#11

The glitch is in broadcasting isuppercase. With map instead of broadcast, its fast again.


julia> @btime map(isuppercase,first.($a))
  72.614 ns (3 allocations: 208 bytes)

julia> @btime isuppercase.(first.($a))
  326.126 ns (3 allocations: 4.31 KiB)
#12

Seems to me that there is some issue when a BitArray is produced instead of a Vector{Bool}. I can see the same thing here:

julia> foo(x, val) = x .> val

julia> bar(x, val) = [y > val for y in x]

julia> @btime foo(1:7, 3)
  397.475 ns (3 allocations: 4.31 KiB)
7-element BitArray{1}:
 false
 false
 false
  true
  true
  true
  true

julia> @btime bar(1:7, 3)
  39.051 ns (1 allocation: 96 bytes)
7-element Array{Bool,1}:
 false
 false
 false
  true
  true
  true
  true

A lot of allocations with BitArray.

#13

hmm … seems like you are on to something!