[英]Seq.map error when sending a record in f#
I'm trying to read a File, employees.txt and output a payroll.txt File.我正在尝试读取一个文件,employees.txt 并输出一个 payroll.txt 文件。 The employee file has the following text: H|1111|Jane Doe|8.50|40.0 S|2222|John Doe|0.10|1500.00
.员工文件包含以下文本: H|1111|Jane Doe|8.50|40.0 S|2222|John Doe|0.10|1500.00
。 I'm trying to read this file and output the same employees but with salary evaluate already.我正在尝试读取此文件并输出相同的员工,但已经评估了薪水。 The thing is that I don't understand very well the logic behind File.ReadLines "employees.txt" |> Seq.map inputline_to_employee |> Seq.map employee_to_outputline Here's the code问题是我不太明白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
The issue that I'm having is this我遇到的问题是这个
The business logic works because I ran it using an array of Employees, but for some reason I can't get this reading the file instead.业务逻辑有效,因为我使用一组员工运行它,但由于某种原因,我无法读取文件。 I know is something about the Seq.map that I'm passing to the functions.我知道一些关于我传递给函数的 Seq.map 的信息。 BUt I'm relatively new to this language.但是我对这种语言比较陌生。
inputline_to_employee is returning a list via []. inputline_to_employee 正在通过 [] 返回一个列表。 It should instead return the single employee rather than a list with only one entry.它应该返回单个员工而不是只有一个条目的列表。
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.