Alex
Alex

Reputation: 4180

How to suppress function return

Suppose I have a function that has multiple returned values (shown below). However, this output is not informative as users do not know what each value stands for unless they look up the function definition. So I would like to use println() to print the results with appropriate names to the screen, while suppressing the the actual returned values from being printed on the screen. In R, the function invisible() does that, but how do you do the same thing in Julia?

function trimci(x::Array; tr=0.2, alpha=0.05, nullvalue=0)
    se=sqrt(winvar(x,tr=tr))./((1-2.*tr)*sqrt(length(x)))
    ci=cell(2)
    df=length(x)-2.*floor(tr.*length(x))-1
    ci=[tmean(x, tr=tr)-qt(1-alpha./2, df).*se, tmean(x, tr=tr)+qt(1-alpha./2, df).*se]
    test=(tmean(x,tr=tr)-nullvalue)./se
    sig=2.*(1-pt(abs(test),df))
    return ci, tmean(x, tr=tr), test, se, sig
end

Upvotes: 3

Views: 3671

Answers (3)

quinnj
quinnj

Reputation: 1258

In addition to what Harlan and Stefan said, let me share an example from the ODBC.jl package (source here). One of my favorite features of Julia over other languages is how dead simple it is to create custom types (and without performance issues either!). Here's a custom type, Metadata, that simply holds several fields of data that describe an executed query. This doesn't necessarily need its own type, but it makes it more convenient passing all this data between functions as well as allowing custom formatting of its output by overloading the Base.show() function.

type Metadata
    querystring::String
    cols::Int
    rows::Int
    colnames::Array{ASCIIString}
    coltypes::Array{(String,Int16)}
    colsizes::Array{Int}
    coldigits::Array{Int16}
    colnulls::Array{Int16}
end
function show(io::IO,meta::Metadata)
    if meta == null_meta
        print(io,"No metadata")
    else
        println(io,"Resultset metadata for executed query")
        println(io,"------------------------------------")
        println(io,"Columns: $(meta.cols)")
        println(io,"Rows: $(meta.rows)")
        println(io,"Column Names: $(meta.colnames)")
        println(io,"Column Types: $(meta.coltypes)")
        println(io,"Column Sizes: $(meta.colsizes)")
        println(io,"Column Digits: $(meta.coldigits)")
        println(io,"Column Nullable: $(meta.colnulls)")
        print(io,"Query: $(meta.querystring)")
    end 
end

Again, nothing fancy, but illustrates how easy it really is to define a custom type and produce custom output along with it.

Cheers.

Upvotes: 5

StefanKarpinski
StefanKarpinski

Reputation: 33259

The value nothing is how you return a value that won't print: the repl specifically checks for the value nothing and prints nothing if that's the value returned by an expression. What you're looking to do is to return a bunch of values and not print them, which strikes me as rather odd. If a function returns some stuff, I want to know about it – having the repl lie to users seems like a bad idea. Harlan's suggesting would work though: define a type for this value with the values you don't want to expose to the user as fields and customize its printing so that the fields you don't want to show people aren't printed.

Upvotes: 1

Harlan
Harlan

Reputation: 19361

One thing you could do would be to define a new type for the return value for this function, call it TrimCIResult or something. Then you could define appropriate methods to show that object in the REPL. Or you may be able to generalize that solution with a type hierarchy that could be used for storing the results from and displaying any statistical test.

Upvotes: 2

Related Questions