小编Dav*_*son的帖子

F# - 工厂方法设计模式

下面是我尝试使用 F# 实现工厂方法设计模式,同时尝试使其更具功能性(即不是直接的 OO 实现)。以下是我想出的:

type ISkateBoard = abstract Model : unit -> string

type SkateBoard = 
| Roskopp 
| Peters
    interface ISkateBoard
        with member this.Model() = 
                match this with 
                | Roskopp-> "Rob Roskopp..."
                | Peters -> "Duane Peters..."

let assemble model : ISkateBoard =
    match model with
    | "Roskopp" -> Roskopp :> ISkateBoard
    | "Peters" -> Peters :> ISkateBoard
    | _ -> failwith "no such skateboard model.."

let SkateBoardFactory assemble model = assemble model

let SantaCruzFactory = …
Run Code Online (Sandbox Code Playgroud)

f# design-patterns

5
推荐指数
2
解决办法
1494
查看次数

F#命令模式

我正在尝试实现命令模式来控制机器人.我正在使用它来探索如何在F#中实现命令模式.以下是我的实施:

type Walle(position, rotate) =     
    let (x:float,y:float) = position
    let rotation = rotate
    member this.Move(distance) =
        let x2 = distance * sin (System.Math.PI/180.0 * rotation)
        let y2 = distance * cos (System.Math.PI/180.0 * rotation)
        let newPosition = (x+x2, y+y2)
        Walle(newPosition, rotation)
    member this.Rotate(angle) = 
        let newRotation = 
            let nr = rotation + angle
            match nr with
            | n when n < 360.0 -> nr
            | _ -> nr - 360.0
        Walle(position, newRotation)

let Move distance = fun (w:Walle) …
Run Code Online (Sandbox Code Playgroud)

f# design-patterns

4
推荐指数
1
解决办法
542
查看次数

F#将sprintf应用于字符串列表

我怎样才能最好地创建一个函数(让我们称之为myPrint),它将sprintf,一个格式字符串和一个字符串列表作为参数,并产生一个结果,使得字符串列表中的每个元素都被应用/折叠到sprintf中?

myPrint (sprintf "one: %s two: %s three: %s") ["first"; "second"; "third"];;
Run Code Online (Sandbox Code Playgroud)

会产生输出

val myPrint : string = "one: first two: second three: third"
Run Code Online (Sandbox Code Playgroud)

recursion f# functional-programming

3
推荐指数
1
解决办法
864
查看次数