Simplee.IO
1.0.8
See the version list below for details.
dotnet add package Simplee.IO --version 1.0.8
NuGet\Install-Package Simplee.IO -Version 1.0.8
<PackageReference Include="Simplee.IO" Version="1.0.8" />
paket add Simplee.IO --version 1.0.8
#r "nuget: Simplee.IO, 1.0.8"
// Install Simplee.IO as a Cake Addin #addin nuget:?package=Simplee.IO&version=1.0.8 // Install Simplee.IO as a Cake Tool #tool nuget:?package=Simplee.IO&version=1.0.8
Koio |> Simple.IO
Library Simplee.IO v1.0.8 implements common IO functionality used by all other Simplee projects. The namespace of the library is Simplee.IO.
Here is the main functionality exposed:
- io monad and stdio operations
- retry pattern
1. IO Monad
The library exposes a monad for IO. The main functions you can use are iorun which executes an IO flow, iobind which binds an IO monad to a given function. The library exposes a computation buildel, io, which can be used for imperative programing style.
let flow = io {
let! _ = putStrLn "Introduce line 1:"
let! cs1 = getLine
let! _ = putStrLn "Introduce line 2:"
let! cs2 = getLine
let! _ = putStrLn "You typed:"
return! putStrLn cs1
return! putStrLn cs2
}
flow |> iorun
2. stdio Operations
As you could see in the above example, the library exposes several convenience methods that allows you to interact with the stdio. All these functions are build on top of the IO monad.
For writing to the stdio, you can use: putChar, putStr, putStrLn. To get the input from stdio, you can use: getChar, getLine, getContent (this last function reads the stdio till the end).
let lines (s:string) = s.Split([|stdout.NewLine|], StringSplitOptions.None) |> Seq.ofArray
let length xs = Seq.length xs
let flow1 = io {
let! _ = putStrLn "Introduce line 1:"
let! cs1 = getLine
let! _ = putStrLn "Introduce line 2:"
let! cs2 = getLine
let! _ = putStrLn "You typed:"
return! putStrLn cs1
return! putStrLn cs2
}
let flow2 = io {
let! _ = putStrLn "Test contents:"
let! cs = getContents
return! putStr cs
}
let flow3 = io {
let! cs = getContents
return! cs |> lines |> length |> print
}
3. Retry Pattern
The library exposes a retry mechanism, implemented by the retry function.
The function retry receives as input arguments a function, k, which controls the number of attempts. This function should return true if we need to keep trying, or false if we want to give up retrying. The second argument of the retry function, f, is the function that will be executed in the retry loop.
In the example below, the function f fails firs time and succeeds on the second attempt. The retry is configured using keeptry function to make 3 attempts.
let mutable attempt = 0
let f () =
if attempt < 1 then
attempt <- attempt + 1
"My IO Error here" |> Error
else
10 |> Ok
let keeptry _ a = a < 3
(f |> retry keeptry) ()
Since the pattern calls the same function f multiple times, expecting different results each time, we consider the retry pattern part of the IO namespace.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 is compatible. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
-
.NETCoreApp 2.0
- FSharp.Core (>= 4.3.4)
- Simplee.Common (>= 1.0.11)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Added the retry pattern.