F#计算表达式透明状态通过绑定

我有以下代码尝试使用常见的MaybeBuilder从网络流中读取可能不完整的数据(例如图像数据):

let image = maybe {
    let pos = 2 //Initial position skips 2 bytes of packet ID
    let! width, pos = readStreamAsInt 2 pos
    let! height, pos = readStreamAsInt 2 pos
    let! data, pos = readStream (width*height) pos
    advanceInStream pos
    return {width = width; height = height; pixels = data}
}

因此,如果数据尚未到达NetworkStream,readStream [asInt] [numBytes] [offset]函数会返回Some [data]或None。 在读取整个网络数据包时执行advanceInStream函数。

我想知道是否有某种方法来编写一些自定义计算表达式构建器来隐藏pos用户的pos传递,因为它总是相同的 - 我读取一些数据并在流中进行定位,并将其作为最后一个参数传递给下一个读取函数。

PS MaybeBuilder使用:

type MaybeBuilder() =    
    member x.Bind(d,f) = Option.bind f d
    member x.Return d = Some d
    member x.ReturnFrom d = d
    member x.Zero() = None
let maybe = new MaybeBuilder()

PPS

第二个想法是,由于可能的“for”或“while”循环在阅读中,似乎我必须让pos变化。 简单的让! 适用于Pos绑定阴影,但如果您在循环中添加阅读,则无法保持不可变性,对吗? 那么任务就变得微不足道了。


@bytebuster正在提供关于自定义计算表达式的可维护性的好点,但我仍然认为我演示了如何将StateMaybe monad合并为一个。

在“传统”语言中,我们有很好的支持组合值等整数值,但是在开发解析器时遇到问题(二进制流中的生成值本质上是解析)。 对于解析器,我们希望将简单的解析器函数组合成更复杂的解析器函数,但这里的“传统”语言通常缺乏良好的支持。

在函数式语言中,函数与数值一样普通,并且由于值可以组成,所以显然函数也可以是这样。

首先让我们定义一个StreamReader函数。 StreamReader需要StreamPosition (stream + position)并生成更新的StreamPositionStreamReaderResult (读取值或失败)。

type StreamReader<'T> = 
  StreamReader of (StreamPosition -> StreamPosition*StreamReaderResult<'T>)

(这是最重要的一步。)

我们希望能够将简单的StreamReader功能组合成更复杂的功能。 我们想要维护的一个非常重要的属性是,在StreamReader下,组合操作是“关闭”的,这意味着组合的结果是一个新的StreamReader ,它可以StreamReader地构成。

为了读取图像,我们需要读取宽度和高度,计算产品并读取字节。 像这样的东西:

let readImage = 
  reader {
    let! width  = readInt32 
    let! height = readInt32 
    let! bytes  = readBytes (width*height)

    return width, height, bytes
  }

由于组合被关闭, readImage是一个StreamReader<int*int*byte[]>

为了能够像上面那样StreamReader ,我们需要定义一个计算表达式,但在我们能够做到这一点之前,我们需要为StreamReader定义操作ReturnBind 。 事实证明, Yield也是很好的。

module StreamReader =
  let Return v : StreamReader<'T> =
    StreamReader <| fun sp -> 
      sp, (Success v)

  let Bind (StreamReader t) (fu : 'T -> StreamReader<'U>) : StreamReader<'U> =
    StreamReader <| fun sp -> 
      let tsp, tr = t sp
      match tr with
      | Success tv ->
        let (StreamReader u) = fu tv
        u tsp
      | Failure tfs -> tsp, Failure tfs

  let Yield (ft : unit -> StreamReader<'T>) : StreamReader<'T> =
    StreamReader <| fun sp -> 
      let (StreamReader t) = ft ()
      t sp

Return是微不足道的,因为StreamReader应该返回给定的值并且不更新StreamPosition

Bind更具挑战性,但描述了如何将两个StreamReader函数合并为一个新函数。 Bind运行第一个StreamReader函数并检查结果,如果失败则返回失败,否则使用StreamReader结果计算第二个StreamReader并在更新流位置上运行该结果。

Yield仅创建StreamReader函数并运行它。 Yield构建计算表达式时所采用F#。

最后我们来创建计算表达式构建器

type StreamReaderBuilder() =
  member x.Return v   = StreamReader.Return v
  member x.Bind(t,fu) = StreamReader.Bind t fu
  member x.Yield(ft)  = StreamReader.Yield ft

let reader = StreamReaderBuilder ()

现在我们构建了用于组合StreamReader函数的基本框架。 另外,我们需要定义StreamReader的基本功能。

完整的例子:

open System
open System.IO

// The result of a stream reader operation is either
//  Success of value
//  Failure of list of failures
type StreamReaderResult<'T> =
  | Success of 'T
  | Failure of (string*StreamPosition) list

and StreamPosition =
  {
    Stream    : byte[]
    Position  : int
  }

  member x.Remaining = max 0 (x.Stream.Length - x.Position)

  member x.ReadBytes (size : int) : StreamPosition*StreamReaderResult<byte[]> =
    if x.Remaining < size then
      x, Failure ["EOS", x]
    else
      let nsp = StreamPosition.New x.Stream (x.Position + size)
      nsp, Success (x.Stream.[x.Position..(x.Position + size - 1)])

  member x.Read (converter : byte[]*int -> 'T) : StreamPosition*StreamReaderResult<'T> =
    let size = sizeof<'T>
    if x.Remaining < size then
      x, Failure ["EOS", x]
    else
      let nsp = StreamPosition.New x.Stream (x.Position + size)
      nsp, Success (converter (x.Stream, x.Position))

  static member New s p = {Stream = s; Position = p;}

// Defining the StreamReader<'T> function is the most important decision
//   In this case a stream reader is a function that takes a StreamPosition 
//   and produces a (potentially) new StreamPosition and a StreamReadeResult
type StreamReader<'T> = StreamReader of (StreamPosition -> StreamPosition*StreamReaderResult<'T>)

// Defining the StreamReader CE
module StreamReader =
  let Return v : StreamReader<'T> =
    StreamReader <| fun sp -> 
      sp, (Success v)

  let Bind (StreamReader t) (fu : 'T -> StreamReader<'U>) : StreamReader<'U> =
    StreamReader <| fun sp -> 
      let tsp, tr = t sp
      match tr with
      | Success tv ->
        let (StreamReader u) = fu tv
        u tsp
      | Failure tfs -> tsp, Failure tfs

  let Yield (ft : unit -> StreamReader<'T>) : StreamReader<'T> =
    StreamReader <| fun sp -> 
      let (StreamReader t) = ft ()
      t sp

type StreamReaderBuilder() =
  member x.Return v   = StreamReader.Return v
  member x.Bind(t,fu) = StreamReader.Bind t fu
  member x.Yield(ft)  = StreamReader.Yield ft

let reader = StreamReaderBuilder ()

let read (StreamReader sr) (bytes : byte[]) (pos : int) : StreamReaderResult<'T> =
  let sp    = StreamPosition.New bytes pos
  let _, sr = sr sp
  sr

// Defining various stream reader functions
let readValue (converter : byte[]*int -> 'T) : StreamReader<'T> =
  StreamReader <| fun sp -> sp.Read converter

let readInt32 = readValue BitConverter.ToInt32
let readInt16 = readValue BitConverter.ToInt16
let readBytes size : StreamReader<byte[]> = 
  StreamReader <| fun sp -> 
    sp.ReadBytes size

let readImage = 
  reader {
    let! width  = readInt32 
    let! height = readInt32 
    let! bytes  = readBytes (width*height)

    return width, height, bytes
  }

[<EntryPoint>]
let main argv = 
  // Sample byte stream
  let bytes   = [|2;0;0;0;3;0;0;0;1;2;3;4;5;6|] |> Array.map byte
  let result  = read readImage bytes 0

  printfn "%A" result

  0
链接地址: http://www.djcxy.com/p/24419.html

上一篇: F# computation expression transparent state passing with Bind

下一篇: F# "Stateful" Computation Expression