繁体   English   中英

在 f# 中发送记录时出现 Seq.map 错误

[英]Seq.map error when sending a record in f#

我正在尝试读取一个文件,employees.txt 并输出一个 payroll.txt 文件。 员工文件包含以下文本: H|1111|Jane Doe|8.50|40.0 S|2222|John Doe|0.10|1500.00 我正在尝试读取此文件并输出相同的员工,但已经评估了薪水。 问题是我不太明白File.ReadLines "employees.txt" |> Seq.map inputline_to_employee |> Seq.map employee_to_outputline背后的逻辑,代码如下

open System 
open System.IO 

type Employee =
    | HourlyEmployee of 
        id: string * name: string * pay_rate: float * hours_worked: float
    | SalesEmployee of 
        id: string * name: string * comm_rate: float * sales_amount: float

// inputline_to_student : string -> Student
// Returns a student record for the given input line.
let inputline_to_employee (line : string) =
   let fields = line.Split '|'

   [
       if (fields.[0] = "H") then HourlyEmployee(fields.[1], (fields.[2] + " " + fields.[3]), float fields.[4], float fields.[5])
       else SalesEmployee(fields.[1], (fields.[2] + " " + fields.[3]), float fields.[4], float fields.[5])
   ]

let payroll = function
    | HourlyEmployee (_, _, pay, hrs) -> if (hrs <= 40.) then pay * hrs
                                            else pay * 40. + (hrs * 1.5 * pay)
    | SalesEmployee (_, _, comm, amt) -> comm * amt

// employee_to_outputline : Employee -> string
// Returns the output line for the given employee.
let employee_to_outputline emp =
   let pay = payroll emp 
   match emp with
   | HourlyEmployee (id, name, _, _) ->
        sprintf "%s|%s|%.1f" id name pay
   | SalesEmployee (id, name, _, _) ->
       sprintf "%s|%s|%.1f" id name pay

// main: string [] -> int
// Entry point of the program.
[<EntryPoint>]
let main _ =
   let mutable exit_code = 0
   try
       let wages = 
           File.ReadLines "employees.txt"
               |> Seq.map inputline_to_employee 
               |> Seq.map employee_to_outputline

       File.WriteAllLines ("payroll.txt", wages)

       Console.WriteLine "All employees were read and evaluations were written."
   with
       :? FileNotFoundException as e ->
           Console.Error.WriteLine ("Error: {0}", e.Message)
           exit_code <- 1
   exit_code 

我遇到的问题是这个在此处输入图片说明

业务逻辑有效,因为我使用一组员工运行它,但由于某种原因,我无法读取文件。 我知道一些关于我传递给函数的 Seq.map 的信息。 但是我对这种语言比较陌生。

inputline_to_employee 正在通过 [] 返回一个列表。 它应该返回单个员工而不是只有一个条目的列表。

    let inputline_to_employee (line : string) =
    let fields = line.Split '|'
       if (fields.[0] = "H") then HourlyEmployee(fields.[1], (fields.[2] + " " + fields.[3]), float fields.[4], float fields.[5])
       else SalesEmployee(fields.[1], (fields.[2] + " " + fields.[3]), float fields.[4], float fields.[5])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM