F#惯用Seq / List展望未来并进行修改?

时间:2015-01-29 04:19:07

标签: f#

在F#中有一种惯用的方法是在列表/ seq /数组中向前看并使用在处理当前项目时学到的信息吗?在我的场景中,还需要改变(或以其他方式存储它已更改的事实)前面的项目,以便依次正确处理它。我正在实施一些相当愚蠢的业务规则,这样的模式或技术会很有用。

现在我正在使用累加器来存储信息,然后在处理每个数据时改变数组的项目。如下面的简化示例所示,这感觉有点笨拙。我正在解决的问题的实际业务规则更复杂,所以如果有更好的方法,我宁可不跋涉这条路。基本上,我希望摆脱graceMonths类型中的Acc,而是通过向前看列表/ seq /数组来解决这些月份。

模拟示例:当工人每个月达到所需的生产水平时,他们会获得某种类型的奖励。如果他们未能达到理想水平,他们可以通过超过接下来几个月的水平来弥补它。同样,他们可以将过剩的生产储存在未来的月份,以供不足之处使用。以下脚本显示了一个示例。

type CalendarMonth = 
    { year : int
      month : int }

type InMonth = 
    { month : CalendarMonth
      prodCount : int }

type OutMonth = 
    { month : CalendarMonth
      prodCount : int
      borrowedFrom : InMonth list
      metProd : bool }

type OutMonthAcc = 
    { outMonth : OutMonth
      notUsed : InMonth list }

type IndexOutMonth = 
    { index : int
      outMonth : OutMonth }

type Acc = 
    { index : int
      graceMonths : IndexOutMonth list
      bankedProd : InMonth list
      arrRef : OutMonth array }

type GraceAcc = 
    { processed : IndexOutMonth list
      notUsed : InMonth list }

let createMonth y m c = 
    { InMonth.month = 
          { year = y
            month = m }
      prodCount = c }

let toOutPutMonth (x : InMonth) = 
    { month = x.month
      prodCount = x.prodCount
      borrowedFrom = []
      metProd = false }

let toSimple (x : OutMonth) = sprintf "year: %i, month: %i, metProd: %b" x.month.year x.month.month x.metProd

let solveWithBanked desiredProd bank m = 
    let useProd (acc : OutMonthAcc) inMonth = 
        let m = acc.outMonth
        if m.metProd then 
            { acc with notUsed = inMonth :: acc.notUsed }
        else 
            let borrowed = m.borrowedFrom |> List.sumBy (fun x -> x.prodCount)
            let needed = desiredProd - (m.prodCount + borrowed)
            match inMonth.prodCount with
            | x when x < needed -> 
                { outMonth = { m with borrowedFrom = inMonth :: m.borrowedFrom }
                  notUsed = acc.notUsed }
            | x when x > needed -> 
                let newInMonth = { inMonth with prodCount = inMonth.prodCount - needed }

                let newOutMonth = 
                    { m with borrowedFrom = newInMonth :: m.borrowedFrom
                             metProd = true }
                { outMonth = newOutMonth
                  notUsed = newInMonth :: acc.notUsed }
            | _ -> 
                { outMonth = 
                      { m with borrowedFrom = inMonth :: m.borrowedFrom
                               metProd = true }
                  notUsed = acc.notUsed }
    bank |> List.fold useProd { outMonth = m
                                notUsed = [] }

let solveGrace desiredProd bank (graceLst : IndexOutMonth list) = 
    let useBank acc iOutMonth = 
        let result = iOutMonth.outMonth |> solveWithBanked desiredProd acc.notUsed
        if result.outMonth.metProd then 
            let iMonth = 
                { index = iOutMonth.index
                  outMonth = result.outMonth }
            { processed = iMonth :: acc.processed
              notUsed = result.notUsed }
        else { acc with processed = iOutMonth :: acc.processed }
    graceLst
    |> List.sortBy (fun x -> x.index)
    |> List.fold useBank { processed = []
                           notUsed = bank }

let solve desiredProd acc m = 
    match m.prodCount < desiredProd with
    | true -> // less
        let result = m |> solveWithBanked desiredProd acc.bankedProd
        if result.outMonth.metProd then 
            acc.arrRef.[acc.index] <- result.outMonth
            { acc with index = acc.index + 1
                       bankedProd = result.notUsed }
        else 
            let iMonth = 
                { IndexOutMonth.index = acc.index
                  outMonth = m }
            { acc with index = acc.index + 1
                       graceMonths = iMonth :: acc.graceMonths }
    | false -> // greater
        let newM = 
            { index = acc.index
              outMonth = { m with metProd = true } }

        let newIn = 
            { InMonth.month = m.month
              prodCount = m.prodCount - desiredProd }

        let result = acc.graceMonths |> solveGrace desiredProd (newIn :: acc.bankedProd)
        let solved, unsolved = result.processed |> List.partition (fun x -> x.outMonth.metProd)
        newM :: solved |> List.iter (fun x -> acc.arrRef.[x.index] <- x.outMonth)
        { acc with index = acc.index + 1
                   graceMonths = unsolved
                   bankedProd = result.notUsed }

let jan = createMonth 2013 01 4
let feb = createMonth 2013 02 4
let mar = createMonth 2013 03 6
let apr = createMonth 2013 04 7
let may = createMonth 2013 05 4
let jun = createMonth 2013 06 4

let arr = 
    jan :: feb :: mar :: apr :: may :: jun :: []
    |> Array.ofList
    |> Array.map toOutPutMonth

arr |> Array.fold (solve 5) { index = 0
                              graceMonths = []
                              bankedProd = []
                              arrRef = arr }

let result = 
    arr
    |> Array.map toSimple
    |> List.ofArray

result的值应显示除6月以外的所有月份生产。这是F#中的正确方法还是有更好的方法?

1 个答案:

答案 0 :(得分:1)

这是我在这里尝试的方法:

  • 预先计算每个月的预期金额与实际金额之间的差异
  • 将这几个月分为三组 - 低于配额,高于配额的组和准确配额的组,
  • 尝试平衡配额以下的配额与配额以上的配额。

对于我来说,前两点似乎是不言自明的,至于第三点,这里是余额函数的草案和示例用法:

let (|Lt|Eq|Gt|) (a, b) =
    if a = b 
        then Eq
        elif a > b then Gt else Lt

let rec balance below above balanced = 
    match below, above with
    | (x, required)::xs, (y, available)::ys ->
        match required, available with
        | Lt -> balance xs ((y, available - required) :: ys) (x::balanced)
        | Eq -> balance xs ys (x::y::balanced)
        | Gt -> balance ((x, required - available) :: xs) ys (y::balanced)
    | _, _ -> 
        below, above, balanced

balance [("a", 4); ("b", 1)] [ ("c", 2); ("d", 2) ] [ "e" ]
balance [("a", 1); ("b", 1)] [ ("c", 2); ("d", 2) ] [ "e" ]

基本上你是并行地走两个列表,从一个列表中“取”并向另一个列表“添加”,直到你用完两个列表。剩下的是做平衡的最佳尝试。

通常,您希望在编写F#代码时使用像List模块这样的集合API,但是记住当您的用例似乎不适合现有方案时,您总是可以回退到“原始”递归。 / p>