"ocaml_beginners"::[] verbose_mode

View: New views
8 Messages — Rating Filter:   Alert me  

"ocaml_beginners"::[] verbose_mode

by kamaradclimber :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

hi,
i'd like having different mode for outpout:
the O-mode : all messages are printed on screen
the 1-mode: only warning messsages
the 2-mode: none

so i need to create a function which, according to the verbose_mode
value, print or not my message
but my first try didn't succeed:
let verbose_mode = 1 // 2//0

let vprint level=if verbose_mode <= level then Printf.printf else ( );;
in order to use:
vprint 1 (*it's a warning*) "take care : the age of mamut is above %i
years" age_of_mamut;;

it could'nt work obvously because  of types
so i tried to use instead of ( ) a function which take the same
arguments than Printf.printf and that's not easy because format type
seems to be weird

how can i solve this issue ?



Re: "ocaml_beginners"::[] verbose_mode

by Richard Jones-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Jul 28, 2008 at 03:24:22PM -0000, kamaradclimber wrote:
> let verbose_mode = 1 // 2//0

I'm assuming you'll want to adjust 'verbose_mode' during the lifetime
of the program, so better to make it a reference:

  let verbose_mode = ref 1

> let vprint level=if verbose_mode <= level then Printf.printf else ( );;

As you rightly found out, this doesn't have a valid type.  If you look
in the 'Printf' module (/usr/lib/ocaml/printf.mli) you'll see that
there is a function 'ksprintf' which is basically designed for this
purpose, so in this version we instead make a function 'f' which will
either print the string or discard it:

  let myprint level =
    let f = if !verbose_mode <= level then print_string else fun _ -> () in
    Printf.ksprintf f ;;

This version works, but it wastes time actually creating the
intermediate string even if you never print it.  To get around that, I
believe that you will need to use a macro (but I'm not entirely sure
-- someone else may jump in and point out a way to do it in regular
OCaml).

Rich.

--
Richard Jones
Red Hat

Re: "ocaml_beginners"::[] verbose_mode

by Virgile Prevosto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

Le lun 28 jui 2008 15:24:22 CEST,
"kamaradclimber" <kamaradclimbe.r@...> a écrit :

> let vprint level=if verbose_mode <= level then Printf.printf else ( );;
> in order to use:
> vprint 1 (*it's a warning*) "take care : the age of mamut is above %i
> years" age_of_mamut;;
>
> it could'nt work obvously because  of types
> so i tried to use instead of ( ) a function which take the same
> arguments than Printf.printf and that's not easy because format type
> seems to be weird

You're looking for the function ifprintf of the Printf module, which
takes the same arguments as printf(1), but does not output anything:

let vprint level =
  if verbose_mode <= level then
    Printf.printf
  else
    Printf.ifprintf stdout

should do what you're expecting.
--
E tutto per oggi, a la prossima volta.
Virgile
(1) modulo an additional one, whose purpose is apparently to be able
to use ifprintf as a replacement for both printf and sprintf as well as
the corresponding functions in Format.


Re: "ocaml_beginners"::[] verbose_mode

by Mathias Kende :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Le lundi 28 juillet 2008 à 16:41 +0100, Richard Jones a écrit :

> let myprint level =
> let f = if !verbose_mode <= level then print_string else fun _ -> ()
> in
> Printf.ksprintf f ;;
>
> This version works, but it wastes time actually creating the
> intermediate string even if you never print it. To get around that, I
> believe that you will need to use a macro (but I'm not entirely sure
> -- someone else may jump in and point out a way to do it in regular
> OCaml).

You could use the ifprintf function from Printf which just eats its
argument and prints nothing:

let myprint level =
        if !verbose_level < level then
                Printf.printf
        else
                Printf.ifprintf stdout

Mathias


Re: "ocaml_beginners"::[] verbose_mode

by Richard Jones-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Jul 28, 2008 at 06:02:31PM +0200, Mathias Kende wrote:

> Le lundi 28 juillet 2008 à 16:41 +0100, Richard Jones a écrit :
> > let myprint level =
> > let f = if !verbose_mode <= level then print_string else fun _ -> ()
> > in
> > Printf.ksprintf f ;;
> >
> > This version works, but it wastes time actually creating the
> > intermediate string even if you never print it. To get around that, I
> > believe that you will need to use a macro (but I'm not entirely sure
> > -- someone else may jump in and point out a way to do it in regular
> > OCaml).
>
> You could use the ifprintf function from Printf which just eats its
> argument and prints nothing:
>
> let myprint level =
> if !verbose_level < level then
> Printf.printf
> else
> Printf.ifprintf stdout

Yes yes, of course this is better :-)

Rich.

--
Richard Jones
Red Hat

Re: "ocaml_beginners"::[] verbose_mode

by Florent Monnier :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> This version works, but it wastes time actually creating the
> intermediate string even if you never print it.  To get around that, I
> believe that you will need to use a macro (but I'm not entirely sure
> -- someone else may jump in and point out a way to do it in regular
> OCaml).

Here is a hack, which seems to work,
but I wouldn't recommend anyone to use it ;-)

# let nothing fmt =
    let last = String.length fmt in
    let rec fold i curry =
      if i >= last then curry else
      match fmt.[i] with
      | '%' when (String.unsafe_get fmt (i+1)) = '%' ->
          fold (i+2) curry
      | '%' ->
          fold (i+1) (fun _ -> Obj.magic curry)
      | _ ->
          fold (i+1) curry
    in
    fold 0 (Obj.magic ())
  ;;
val nothing : string -> 'a -> 'b = <fun>

# let print_debug = function
  | 1 -> Printf.printf
  | _ -> Obj.magic nothing
  ;;
val print_debug : int -> ('a, out_channel, unit) format -> 'a = <fun>

# print_debug 1 "warn: %d %d %d\n" 1 2 3 ;;
warn: 1 2 3
- : unit = ()

# print_debug 0 "warn: %d %d %d\n" 1 2 3 ;;
- : unit = ()

# print_debug 1 "warn: %d %% %d %% %d\n" 1 2 3 ;;
warn: 1 % 2 % 3
- : unit = ()

# print_debug 0 "warn: %d %% %d %% %d\n" 1 2 3 ;;
- : unit = ()

--
Florent

------------------------------------

Archives up to December 31, 2007 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/ocaml_beginners/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/ocaml_beginners/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:ocaml_beginners-digest@...
    mailto:ocaml_beginners-fullfeatured@...

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@...

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/


Re: "ocaml_beginners"::[] verbose_mode

by Virgile Prevosto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Le mar 29 jui 2008 08:25:57 CEST,
Florent Monnier <fmonnier@...> a écrit :

> Here is a hack, which seems to work,
> but I wouldn't recommend anyone to use it ;-)

Well, it lacks at least cases for %a (which implies two further
arguments and not only one) and %!. And I thought that uses of the Obj
module were banned from the beginner's list ;-)
ifprintf is IMHO the way to go.
--
E tutto per oggi, a la prossima volta.
Virgile


Re: "ocaml_beginners"::[] verbose_mode

by kamaradclimber :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

i 've tried the method with ifprintf, my problem is solved
thx all

On Tue, Jul 29, 2008 at 09:02, Virgile Prevosto <virgile.prevosto@...>wrote:

> Le mar 29 jui 2008 08:25:57 CEST,
> Florent Monnier <fmonnier@...> a écrit :
>
> > Here is a hack, which seems to work,
> > but I wouldn't recommend anyone to use it ;-)
>
> Well, it lacks at least cases for %a (which implies two further
> arguments and not only one) and %!. And I thought that uses of the Obj
> module were banned from the beginner's list ;-)
> ifprintf is IMHO the way to go.
> --
> E tutto per oggi, a la prossima volta.
> Virgile
>
>
> ------------------------------------
>
> Archives up to December 31, 2007 are also downloadable at
> http://www.connettivo.net/cntprojects/ocaml_beginners/
> The archives of the very official ocaml list (the seniors' one) can be
> found at http://caml.inria.fr
> Attachments are banned and you're asked to be polite, avoid flames
> etc.Yahoo! Groups Links
>
>
>
>


[Non-text portions of this message have been removed]

LightInTheBox - Buy quality products at wholesale price!