Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ValueTuple #524

Merged
merged 18 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docsrc/content/abstraction-applicative.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ From F#
- ``Choice<'T,'U>``
- ``KeyValuePair<'Key,'T>``
- ``'Monoid * 'T``
- ``'ValueTuple<Monoid, 'T>``
- ``Task<'T>``
- ``'R->'T``
- ``Expr<'T>``
Expand Down
1 change: 1 addition & 0 deletions docsrc/content/abstraction-functor.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ From F#
- ``KeyValuePair<'Key,'T>``
- ``Map<'Key,'T>``
- ``'Monoid * 'T``
- ``'ValueTuple<Monoid, 'T>``
- ``Task<'T>``
- ``'R->'T``
- ``Expr<'T>``
Expand Down
1 change: 1 addition & 0 deletions docsrc/content/abstraction-monad.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ From F#
- ``Result<'T,'U>``
- ``Choice<'T,'U>``
- ``'Monoid * 'T``
- ``'ValueTuple<Monoid, 'T>``
- ``Task<'T>``
- ``'R->'T``
- ``ResizeArray<'T>``
Expand Down
1 change: 1 addition & 0 deletions docsrc/content/abstraction-semigroup.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ From .Net/F#
- ``Map<'T,'U>``
- ``TimeSpan``
- ``Tuple<*>``
- ``ValueTuple<*>``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is tricky. either we support ValueTuple<n> with n < 8 or we do some kind of reflection trick for those tuples greater than 8.

Copy link
Contributor Author

@rodriguestiago0 rodriguestiago0 Nov 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we support for n >= 0 and n <= 8

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I remove this line?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, in that case just change it to ValueTuple<*> (* up to 8 elements)

- ``'T1* ... *'Tn``
- ``Task<'T>``
- ``'T->'Semigroup``
Expand Down
2 changes: 1 addition & 1 deletion docsrc/content/operators-common.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// This block of code is omitted in the generated HTML documentation. Use
// it to define helpers that you do not want to show in the documentation.
#r @"../../src/FSharpPlus/bin/Release/netstandard2.0/FSharpPlus.dll"
open System
open FSharpPlus

(**
Expand Down Expand Up @@ -137,7 +138,6 @@ let inline tuple6 a b c d e f = a,b,c,d,e,f
let inline tuple7 a b c d e f g = a,b,c,d,e,f,g
let inline tuple8 a b c d e f g h = a,b,c,d,e,f,g,h


(**
## Explicit

Expand Down
9 changes: 9 additions & 0 deletions src/FSharpPlus/Control/Numeric.fs
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,22 @@ type Zero with
type Zero with
static member inline Zero (_: Tuple<'a>, _: Zero) = tuple1 (Zero.Invoke ()) : Tuple<'a>
static member inline Zero (_: Id<'a> , _: Zero) = Id<_> (Zero.Invoke ())
static member inline Zero (_: ValueTuple<'a>, _: Zero) = valueTuple1 (Zero.Invoke ()) : ValueTuple<'a>

type Zero with static member inline Zero (_: 'a*'b , _: Zero) = (Zero.Invoke (), Zero.Invoke () ) : 'a*'b
type Zero with static member inline Zero (_: 'a*'b*'c , _: Zero) = (Zero.Invoke (), Zero.Invoke (), Zero.Invoke () ) : 'a*'b*'c
type Zero with static member inline Zero (_: 'a*'b*'c*'d , _: Zero) = (Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke () ) : 'a*'b*'c*'d
type Zero with static member inline Zero (_: 'a*'b*'c*'d*'e , _: Zero) = (Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke () ) : 'a*'b*'c*'d*'e
type Zero with static member inline Zero (_: 'a*'b*'c*'d*'e*'f , _: Zero) = (Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke () ) : 'a*'b*'c*'d*'e*'f
type Zero with static member inline Zero (_: 'a*'b*'c*'d*'e*'f*'g, _: Zero) = (Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke ()) : 'a*'b*'c*'d*'e*'f*'g

type Zero with static member inline Zero (_: ValueTuple<'a,'b> , _: Zero) = ValueTuple.Create (Zero.Invoke (), Zero.Invoke () ) : ValueTuple<'a,'b>
type Zero with static member inline Zero (_: ValueTuple<'a,'b,'c> , _: Zero) = ValueTuple.Create (Zero.Invoke (), Zero.Invoke (), Zero.Invoke () ) : ValueTuple<'a,'b,'c>
type Zero with static member inline Zero (_: ValueTuple<'a,'b,'c,'d> , _: Zero) = ValueTuple.Create (Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke () ) : ValueTuple<'a,'b,'c,'d>
type Zero with static member inline Zero (_: ValueTuple<'a,'b,'c,'d,'e> , _: Zero) = ValueTuple.Create (Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke () ) : ValueTuple<'a,'b,'c,'d,'e>
type Zero with static member inline Zero (_: ValueTuple<'a,'b,'c,'d,'e,'f> , _: Zero) = ValueTuple.Create (Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke () ) : ValueTuple<'a,'b,'c,'d,'e,'f>
type Zero with static member inline Zero (_: ValueTuple<'a,'b,'c,'d,'e,'f,'g>, _: Zero) = ValueTuple.Create (Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke (), Zero.Invoke ()) : ValueTuple<'a,'b,'c,'d,'e,'f,'g>


type Zero with
#if !FABLE_COMPILER
Expand Down
14 changes: 14 additions & 0 deletions src/FSharpPlus/Extensions/ValueTuple.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace FSharpPlus

/// Additional operations on Tuple (,)
[<RequireQualifiedAccess>]
module ValueTuple2 =
let mapItem1 f (x, y) = (f x, y)
let mapItem2 f (x, y) = (x, f y)

/// Additional operations on Tuple (,,)
[<RequireQualifiedAccess>]
module ValueTuple3 =
let mapItem1 f (x, y, z) = (f x, y, z)
let mapItem2 f (x, y, z) = (x, f y, z)
let mapItem3 f (x, y, z) = (x, y, f z)
1 change: 1 addition & 0 deletions src/FSharpPlus/FSharpPlus.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Compile Include="Extensions/Async.fs" />
<Compile Include="Extensions/Extensions.fs" />
<Compile Include="Extensions/Tuple.fs" />
<Compile Include="Extensions/ValueTuple.fs" />
<Compile Include="Data/NonEmptySeq.fs" />
<Compile Include="Control/Converter.fs" />
<Compile Include="Control/Numeric.fs" />
Expand Down
4 changes: 4 additions & 0 deletions src/FSharpPlus/Internals.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Default1 = class inherit Default2 end
#nowarn "0042" // retype

module internal Prelude =
open System

let inline flip f x y = f y x
let inline const' k _ = k
let inline tupleToOption x = match x with true, value -> Some value | _ -> None
Expand All @@ -36,6 +38,8 @@ module internal Prelude =
System.Tuple<_> x
#endif

let inline valueTuple1<'T1> (t1: 'T1) = ValueTuple.Create t1

[<RequireQualifiedAccess>]
module internal Implicit = let inline Invoke (x: ^t) = ((^R or ^t) : (static member op_Implicit : ^t -> ^R) x) : ^R

Expand Down
41 changes: 41 additions & 0 deletions src/FSharpPlus/Operators.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace FSharpPlus

open System
open FSharpPlus.Control

/// Generic functions and operators
Expand Down Expand Up @@ -114,8 +115,48 @@ module Operators =
/// </summary>
/// <category index="0">Common Combinators</category>
let inline tuple8<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'T8> (t1: 'T1) (t2: 'T2) (t3: 'T3) (t4: 'T4) (t5: 'T5) (t6: 'T6) (t7: 'T7) (t8: 'T8) = t1, t2, t3, t4, t5, t6, t7, t8

/// <summary>
/// ValueTuple two arguments
/// </summary>
/// <category index="0">Common Combinators</category>
let inline valueTuple2<'T1, 'T2> (t1: 'T1) (t2: 'T2) = ValueTuple.Create t1, t2

/// <summary>
/// ValueTuple three arguments
/// </summary>
/// <category index="0">Common Combinators</category>
let inline valueTuple3<'T1, 'T2, 'T3> (t1: 'T1) (t2: 'T2) (t3: 'T3) = ValueTuple.Create t1, t2, t3

/// <summary>
/// ValueTuple four arguments
/// </summary>
/// <category index="0">Common Combinators</category>
let inline valueTuple4<'T1, 'T2, 'T3, 'T4> (t1: 'T1) (t2: 'T2) (t3: 'T3) (t4: 'T4) = ValueTuple.Create t1, t2, t3, t4

/// <summary>
/// ValueTuple five arguments
/// </summary>
/// <category index="0">Common Combinators</category>
let inline valueTuple5<'T1, 'T2, 'T3, 'T4, 'T5> (t1: 'T1) (t2: 'T2) (t3: 'T3) (t4: 'T4) (t5: 'T5) = ValueTuple.Create t1, t2, t3, t4, t5

/// ValueTuple six arguments
/// <category index="0">Common Combinators</category>
let inline valueTuple6<'T1, 'T2, 'T3, 'T4, 'T5, 'T6> (t1: 'T1) (t2: 'T2) (t3: 'T3) (t4: 'T4) (t5: 'T5) (t6: 'T6) = ValueTuple.Create t1, t2, t3, t4, t5, t6

/// <summary>
/// ValueTuple seven arguments
/// </summary>
/// <category index="0">Common Combinators</category>
let inline valueTuple7<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7> (t1: 'T1) (t2: 'T2) (t3: 'T3) (t4: 'T4) (t5: 'T5) (t6: 'T6) (t7: 'T7) = ValueTuple.Create t1, t2, t3, t4, t5, t6, t7

/// <summary>
/// ValueTuple eight arguments
/// </summary>
/// <category index="0">Common Combinators</category>
let inline valueTuple8<'T1, 'T2, 'T3, 'T4, 'T5, 'T6, 'T7, 'T8> (t1: 'T1) (t2: 'T2) (t3: 'T3) (t4: 'T4) (t5: 'T5) (t6: 'T6) (t7: 'T7) (t8: 'T8) = ValueTuple.Create t1, t2, t3, t4, t5, t6, t7, t8


#if !FABLE_COMPILER || FABLE_COMPILER_3

// Functor ----------------------------------------------------------------
Expand Down