Skip to content

Commit

Permalink
feat: implement map, iter and length
Browse files Browse the repository at this point in the history
  • Loading branch information
xperiandri committed Nov 7, 2024
1 parent 1e92f99 commit a673d90
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 86 deletions.
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageVersion Include="NuGet.Common" Version="6.11.1" />
<PackageVersion Include="NuGet.Protocol" Version="6.11.1" />
<PackageVersion Include="Octokit" Version="13.0.1" />
<PackageVersion Include="R3" Version="1.2.9" />
<PackageVersion Include="Unquote" Version="7.0.0" />
</ItemGroup>
</Project>
</Project>
3 changes: 2 additions & 1 deletion src/FSharp.Control.R3/FSharp.Control.R3.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="Library.fs" />
<Compile Include="Observable.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="R3" />
</ItemGroup>
</Project>
54 changes: 0 additions & 54 deletions src/FSharp.Control.R3/Library.fs

This file was deleted.

28 changes: 28 additions & 0 deletions src/FSharp.Control.R3/Observable.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module FSharp.Control.Observable

open R3
open System.Threading
open System.Threading.Tasks

let asyncMap (f : 't -> Async<'r>) source =
let selector x ct = ValueTask<'r> (Async.StartImmediateAsTask (f x, ct))
ObservableExtensions.SelectAwait (source, selector)

let mapAsync (f : CancellationToken -> 't -> Task<'r>) source =
let selector x ct = ValueTask<'r> (f ct x)
ObservableExtensions.SelectAwait (source, selector)

let inline map (f : 't -> 'r) source = ObservableExtensions.Select (source, f)

let length source = async {
let! ct = Async.CancellationToken
return!
ObservableExtensions.CountAsync (source, ct)
|> Async.AwaitTask
}

let inline iter (action : 't -> unit) source = ObservableExtensions.ForEachAsync (source, action)

let asyncIter (action : 't -> Async<unit>) source = source |> asyncMap action |> length |> Async.Ignore

let iterAsync (action : CancellationToken -> 't -> Task<unit>) source = source |> mapAsync action |> length |> Async.Ignore
32 changes: 2 additions & 30 deletions tests/FSharp.Control.R3.Tests/Tests.fs
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
namespace FSharp.Control.R3.Tests

open System
open FSharp.Control.R3
open FSharp.Control.R3.Say
open FSharp.Control
open Microsoft.VisualStudio.TestTools.UnitTesting
open Swensen.Unquote

[<TestClass>]
type SayTests () =

member _.``Add two integers`` () =
let subject = Say.add 1 2
test <@ subject = 3 @>
//Assert.AreEqual (subject, 3, message = "Addition works")

member _.``Say nothing`` () =
let subject = Say.nothing ()
Assert.AreEqual (subject, (), "Not an absolute unit")

member _.``Say hello all`` () =
let person = {
Name = "Jean-Luc Picard"
FavoriteNumber = 4
FavoriteColor = Red
DateOfBirth = DateTimeOffset.Parse ("July 13, 2305")
}

let subject = Say.helloPerson person

test <@ subject = "Hello Jean-Luc Picard. You were born on 2305/07/13 and your favorite number is 4. You like Red." @>

//Assert.AreEqual<String> (
// subject,
// "Hello Jean-Luc Picard. You were born on 2305/07/13 and your favorite number is 4. You like Red.",
// message = "You didn't say hello"
//)
type SayTests () = class end

0 comments on commit a673d90

Please sign in to comment.