On Mon, 19 Feb 2007, Sasha Rush wrote:
> But how do I get the same abstraction with scanf?
>
> let id a = a
> module Foo =
> struct
> type t = int
> let read buf = bscanf "%d" id
> end
>
> (*Seemed natural*)
> let some_foo = bscanf "My %a" Foo.read id
Well, it's not really the same thing, as there is no real conversion that
says, "Pass this into another scanner until it achieves success, then take
its results and add them to ours in some meaningful way." Part of this, I
think, is because the typing issues surrounding the "meaningful way" part
of the above seem a bit hairy at first blush.
Anyway, for simple cases, where you just want to add scanning bits on to
the front, you can easily pass the remainder of a buffer on to another
scanner using the %0c conversion. For example:
let id a = a;;
module Foo2 =
struct
type t = int
let read buf = Scanf.bscanf buf "%d" id
end;;
let some_foo buf =
Scanf.bscanf buf "My %0c" (fun _ -> Foo2.read buf);;
# some_foo (Scanf.Scanning.from_string "My 20");;
- : int = 20
let some_foo buf =
Scanf.bscanf buf "My %0c" (fun _ -> 10 + Foo2.read buf);;
# some_foo (Scanf.Scanning.from_string "My 20");;
- : int = 30
But if you want to do anything significantly trickier than that, unless
you want to resort to chopping the input into a set of strings via your
first scanner and then feeding those strings into another set of scanners
inside your supplied function, you might need to go to something like
stream parsers.
Of course, there's always the chance that I'm missing something obvious
here.
William D. Neumann
---
"There's just so many extra children, we could just feed the
children to these tigers. We don't need them, we're not doing
anything with them.
Tigers are noble and sleek; children are loud and messy."
-- Neko Case
Life is unfair. Kill yourself or get over it.
-- Black Box Recorder
.