mirror of
https://github.com/tiennm99/serena.git
synced 2026-10-16 08:15:31 +00:00
add F# support
This commit is contained in:
1 parent
77213e18d0
commit
1fe767278e
14 files changed
+1189
No files matched your search
@@ -0,0 +1,5 @@
|
||||
bin/
|
||||
obj/
|
||||
*.user
|
||||
.vscode/
|
||||
.ionide/
|
||||
@@ -0,0 +1,32 @@
|
||||
module Calculator
|
||||
|
||||
/// Simple calculator functions
|
||||
let add a b = a + b
|
||||
|
||||
let subtract a b = a - b
|
||||
|
||||
let multiply a b = a * b
|
||||
|
||||
let divide a b =
|
||||
if b = 0 then
|
||||
failwith "Cannot divide by zero"
|
||||
else
|
||||
(float a) / (float b)
|
||||
|
||||
/// More complex operations
|
||||
let square x = x * x
|
||||
|
||||
let factorial n =
|
||||
if n <= 0 then 1
|
||||
else
|
||||
let rec factorialHelper acc n =
|
||||
if n <= 1 then acc
|
||||
else factorialHelper (acc * n) (n - 1)
|
||||
factorialHelper 1 n
|
||||
|
||||
/// Calculator type with instance methods
|
||||
type CalculatorClass() =
|
||||
member this.Add(a, b) = add a b
|
||||
member this.Subtract(a, b) = subtract a b
|
||||
member this.Multiply(a, b) = multiply a b
|
||||
member this.Divide(a, b) = divide a b
|
||||
@@ -0,0 +1,44 @@
|
||||
namespace Models
|
||||
|
||||
/// Person record type
|
||||
type Person = {
|
||||
Name: string
|
||||
Age: int
|
||||
Email: string option
|
||||
}
|
||||
|
||||
module PersonModule =
|
||||
/// Create a new person
|
||||
let createPerson name age email =
|
||||
{ Name = name; Age = age; Email = email }
|
||||
|
||||
/// Check if person is an adult
|
||||
let isAdult person =
|
||||
person.Age >= 18
|
||||
|
||||
/// Get display name
|
||||
let getDisplayName person =
|
||||
match person.Email with
|
||||
| Some email -> $"{person.Name} ({email})"
|
||||
| None -> person.Name
|
||||
|
||||
/// Update person age
|
||||
let updateAge newAge person =
|
||||
{ person with Age = newAge }
|
||||
|
||||
/// Address type
|
||||
type Address = {
|
||||
Street: string
|
||||
City: string
|
||||
ZipCode: string
|
||||
Country: string
|
||||
}
|
||||
|
||||
/// Employee type that extends Person concept
|
||||
type Employee = {
|
||||
Person: Person
|
||||
EmployeeId: int
|
||||
Department: string
|
||||
Salary: decimal
|
||||
Address: Address option
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
module Program
|
||||
|
||||
open Calculator
|
||||
open Models
|
||||
|
||||
[<EntryPoint>]
|
||||
let main argv =
|
||||
printfn "Hello, F# World!"
|
||||
|
||||
// Test calculator functions
|
||||
let result1 = add 5 3
|
||||
printfn "5 + 3 = %d" result1
|
||||
|
||||
let result2 = subtract 10 4
|
||||
printfn "10 - 4 = %d" result2
|
||||
|
||||
let result3 = multiply 6 7
|
||||
printfn "6 * 7 = %d" result3
|
||||
|
||||
let result4 = divide 15 3
|
||||
printfn "15 / 3 = %.2f" result4
|
||||
|
||||
// Test calculator class
|
||||
let calc = CalculatorClass()
|
||||
let classResult = calc.Add(20, 5)
|
||||
printfn "Calculator class: 20 + 5 = %d" classResult
|
||||
|
||||
// Test person module
|
||||
let person = PersonModule.createPerson "Alice Smith" 25 (Some "alice@example.com")
|
||||
printfn "Person: %s" (PersonModule.getDisplayName person)
|
||||
printfn "Is adult: %b" (PersonModule.isAdult person)
|
||||
|
||||
// Test factorial
|
||||
let fact5 = factorial 5
|
||||
printfn "5! = %d" fact5
|
||||
|
||||
0 // return success
|
||||
@@ -0,0 +1,21 @@
|
||||
# F# Test Project
|
||||
|
||||
This is a test F# project for testing Serena's F# language support.
|
||||
|
||||
## Project Structure
|
||||
|
||||
- `Program.fs` - Main program entry point
|
||||
- `Calculator.fs` - Calculator functions and types
|
||||
- `Models/Person.fs` - Person and Employee data models
|
||||
|
||||
## Build
|
||||
|
||||
```bash
|
||||
dotnet build
|
||||
```
|
||||
|
||||
## Run
|
||||
|
||||
```bash
|
||||
dotnet run
|
||||
```
|
||||
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Calculator.fs" />
|
||||
<Compile Include="Models/Person.fs" />
|
||||
<Compile Include="Program.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in new issue
Block a user