Re: [julia-users] Re: Creating a show method for type alias

2015-08-16 Thread Ben Ward
I did debate doing this, but went with the typealias as, the way I 
understood it, CIGARString would be a variable containing a reference, to a 
vector or CIGARS. When in my mind, where a String is thought of as the 
array of chars, so it should be with CIGARString, it is the array of 
CIGARS. 

I should mention I had failed in the past to provide my own show methods 
for arrays containing some types, which is - at the heart of the matter - 
what I'm trying to do; create my own show method for and array of CIGARs. 

I solved this problem with the addition of a write mime method. This 
solution was thanks to the wonderful D.C. Jones':

function writemime(io::IO, ::MIME{symbol(text/plain)}, cs::CIGARString)
show(io, cs)
end






On Monday, August 17, 2015 at 12:25:44 AM UTC+1, Colin Bowers wrote:

 No. Apologies. I didn't read the question closely enough.

 However, upon closer reading, I cannot see any reason why you need 
 CIGARString to be a type alias. Wouldn't it make more sense for CIGARString 
 to also be its own type?

 type CIGARString
 x::Vector{CIGAR}
 end

 I think this would solve all your problems in one hit and you would end up 
 with something that is more versatile than using a type alias... If you 
 want to treat an instance of CIGARString like you would any old vector, 
 then just overload the standard methods, e.g. getindex, setindex!, e.t.c.

 On 15 August 2015 at 09:26, Ben Ward axolotl...@gmail.com javascript: 
 wrote:

 Would that work? The variable to be printed isn't fed in as the second 
 option? 


 On Wednesday, August 12, 2015 at 1:34:16 AM UTC+1, colint...@gmail.com 
 wrote:

 Does the following work?

 function Base.show(io::IO, ::Type{CIGARString})
 #your code here
 end



 On Tuesday, 11 August 2015 03:07:15 UTC+10, Ben Ward wrote:

 Hi, I have implemented a basic immutable type with a type alias for a 
 vector of said type:

 immutable CIGAR
 OP::Operation
 Size::Int
 end


 function CIGAR(op::Char, size::Int)
 return CIGAR(Operation(op), size)
 end


 function convert(::Type{String}, cigar::CIGAR)
 return $(cigar.Size)$(Char(cigar.OP))
 end


 function show(io::IO, cigar::CIGAR)
 write(io, convert(String, cigar))
 end


 typealias CIGARString Vector{CIGAR}


 function convert(::Type{CIGARString}, str::String)
 matches = matchall(r(\d+)(\w), str)
 cigarString = Vector{CIGAR}(length(matches))
 @inbounds for i in 1:length(matches)
 m = matches[i]
 cigarString[i] = CIGAR(m[end], parse(Int, m[1:end-1]))
 end
 return cigarString
 end


 macro cigar_str(str)
 return CIGARString(str)
 end

 I also want to define a show method for the alias CIGARString, so as it 
 is converted to a string that can be used with a show method:

 function convert(::Type{String}, cigarString::CIGARString)
 outString = 
 for cigar in cigarString
 outString *= String(cigar)
 end
 return outString
 end


 function show(io::IO, cigarstr::CIGARString)
 write(io, convert(String, cigarstr))
 end

 However the output a see on the REPL is:

 *3-element Array{Bio.Align.CIGAR,1}:*

 * 5M*

 * 5N*

 * 5M*

 So, rather than the show method for CIGARString being called, the show 
 method for CIGAR is being called repeatedly for every element in the 
 CIGARString vector. How do I get Julia to use the show method I want for 
 CIGARString?

 Thanks.




Re: [julia-users] Re: Creating a show method for type alias

2015-08-16 Thread Stefan Karpinski
The whole premise of an alias is that it's a different name for the same thing.

 On Aug 14, 2015, at 8:16 PM, Jeffrey Sarnoff jeffrey.sarn...@gmail.com 
 wrote:
 
 Ben, much as I would like there to be a second kind of typealias, typealiased 
 -- that let us work with the renanamings wiithout risk to the objects of the 
 type originally aliased -- this is not on the radar now.  It is hard to peel 
 off enough to form the CIGAR when the role of typealias is to preclude 
 introducing distictiction outside of recognizing a the alias as if it were a 
 nickname.
 
 
 
 On Friday, August 14, 2015 at 7:26:07 PM UTC-4, Ben Ward wrote:
 Would that work? The variable to be printed isn't fed in as the second 
 option? 
 
 On Wednesday, August 12, 2015 at 1:34:16 AM UTC+1, colint...@gmail.com 
 wrote:
 Does the following work?
 
 function Base.show(io::IO, ::Type{CIGARString})
 #your code here
 end
 
 
 
 On Tuesday, 11 August 2015 03:07:15 UTC+10, Ben Ward wrote:
 Hi, I have implemented a basic immutable type with a type alias for a 
 vector of said type:
 
 immutable CIGAR
 OP::Operation
 Size::Int
 end
 
 
 function CIGAR(op::Char, size::Int)
 return CIGAR(Operation(op), size)
 end
 
 
 function convert(::Type{String}, cigar::CIGAR)
 return $(cigar.Size)$(Char(cigar.OP))
 end
 
 
 function show(io::IO, cigar::CIGAR)
 write(io, convert(String, cigar))
 end
 
 
 typealias CIGARString Vector{CIGAR}
 
 
 function convert(::Type{CIGARString}, str::String)
 matches = matchall(r(\d+)(\w), str)
 cigarString = Vector{CIGAR}(length(matches))
 @inbounds for i in 1:length(matches)
 m = matches[i]
 cigarString[i] = CIGAR(m[end], parse(Int, m[1:end-1]))
 end
 return cigarString
 end
 
 
 macro cigar_str(str)
 return CIGARString(str)
 end
 
 I also want to define a show method for the alias CIGARString, so as it is 
 converted to a string that can be used with a show method:
 
 function convert(::Type{String}, cigarString::CIGARString)
 outString = 
 for cigar in cigarString
 outString *= String(cigar)
 end
 return outString
 end
 
 
 function show(io::IO, cigarstr::CIGARString)
 write(io, convert(String, cigarstr))
 end
 
 However the output a see on the REPL is:
 
 3-element Array{Bio.Align.CIGAR,1}:
 
 
  5M
 
 
  5N
 
 
  5M
 
 
 So, rather than the show method for CIGARString being called, the show 
 method for CIGAR is being called repeatedly for every element in the 
 CIGARString vector. How do I get Julia to use the show method I want for 
 CIGARString?
 
 Thanks.