From a5636e6331052aa77fd91e17afb82db4e3eb99eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luz?= Date: Tue, 7 Mar 2023 14:28:57 -0300 Subject: [PATCH 1/5] Created Documentation section and included examples in a project for maintance purpose --- .../Maybe.Documentation.csproj | 14 ++++ Maybe.Documentation/MaybeExtensions.cs | 80 +++++++++++++++++++ Maybe.Documentation/Program.cs | 16 ++++ Maybe.sln | 12 ++- README.md | 80 ++++++++++++++++++- 5 files changed, 198 insertions(+), 4 deletions(-) create mode 100644 Maybe.Documentation/Maybe.Documentation.csproj create mode 100644 Maybe.Documentation/MaybeExtensions.cs create mode 100644 Maybe.Documentation/Program.cs diff --git a/Maybe.Documentation/Maybe.Documentation.csproj b/Maybe.Documentation/Maybe.Documentation.csproj new file mode 100644 index 0000000..0d83803 --- /dev/null +++ b/Maybe.Documentation/Maybe.Documentation.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/Maybe.Documentation/MaybeExtensions.cs b/Maybe.Documentation/MaybeExtensions.cs new file mode 100644 index 0000000..5f83338 --- /dev/null +++ b/Maybe.Documentation/MaybeExtensions.cs @@ -0,0 +1,80 @@ +using ZBRA.Maybe; + +namespace Maybe.Documentation +{ + public class MaybeExtensions + { + public static void OrEmptyExample_WithValue() + { + Console.WriteLine("OrEmpty example with values"); + var maybe = "some value".ToMaybe(); + var v = maybe.OrEmpty(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: some value + } + + public static void OrEmptyExample_WithoutValue() + { + Console.WriteLine("OrEmpty example without values"); + var maybe = Maybe.Nothing; + var v = maybe.OrEmpty(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: + } + + public static void OrNullExample_WithValue() + { + Console.WriteLine("OrNull example with values"); + var maybe = "some value".ToMaybe(); + var v = maybe.OrNull(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: some value + } + + public static void OrNullExample_WithoutValue() + { + Console.WriteLine("OrNull example without values"); + var maybe = Maybe.Nothing; + var v = maybe.OrNull(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: + } + + + public static void OrTrueExample_WithValue() + { + Console.WriteLine("OrTrue example with values"); + var maybe = false.ToMaybe(); + var v = maybe.OrTrue(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: false + } + + public static void OrTrueExample_WithoutValue() + { + Console.WriteLine("OrTrue example without values"); + var maybe = Maybe.Nothing; + var v = maybe.OrTrue(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: true + } + + public static void OrFalseExample_WithValue() + { + Console.WriteLine("OrFalse example with values"); + var maybe = false.ToMaybe(); + var v = maybe.OrFalse(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: False + } + + public static void OrFalseExample_WithoutValue() + { + Console.WriteLine("OrFalse example without values"); + var maybe = Maybe.Nothing; + var v = maybe.OrFalse(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: False + } + } +} diff --git a/Maybe.Documentation/Program.cs b/Maybe.Documentation/Program.cs new file mode 100644 index 0000000..33e12b7 --- /dev/null +++ b/Maybe.Documentation/Program.cs @@ -0,0 +1,16 @@ +using Maybe.Documentation; + +//OrEmpty +MaybeExtensions.OrEmptyExample_WithValue(); +MaybeExtensions.OrEmptyExample_WithoutValue(); +//OrNull +MaybeExtensions.OrNullExample_WithValue(); +MaybeExtensions.OrNullExample_WithoutValue(); +//OrTrue +MaybeExtensions.OrTrueExample_WithValue(); +MaybeExtensions.OrTrueExample_WithoutValue(); +//OrFalse +MaybeExtensions.OrFalseExample_WithValue(); +MaybeExtensions.OrFalseExample_WithoutValue(); + +Console.ReadLine(); \ No newline at end of file diff --git a/Maybe.sln b/Maybe.sln index 3a39b32..e4e7cf2 100644 --- a/Maybe.sln +++ b/Maybe.sln @@ -1,11 +1,13 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30717.126 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33213.308 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Maybe.Test", "Maybe.Test\Maybe.Test.csproj", "{F5DD5CA7-C20A-4E6A-A06A-1C4118D10945}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Maybe", "Maybe\Maybe.csproj", "{15B03988-B848-41CC-9B68-1BA4AB16A78F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Maybe", "Maybe\Maybe.csproj", "{15B03988-B848-41CC-9B68-1BA4AB16A78F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Maybe.Documentation", "Maybe.Documentation\Maybe.Documentation.csproj", "{AAB025E0-8341-4A7B-9B65-115430DED230}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {15B03988-B848-41CC-9B68-1BA4AB16A78F}.Debug|Any CPU.Build.0 = Debug|Any CPU {15B03988-B848-41CC-9B68-1BA4AB16A78F}.Release|Any CPU.ActiveCfg = Release|Any CPU {15B03988-B848-41CC-9B68-1BA4AB16A78F}.Release|Any CPU.Build.0 = Release|Any CPU + {AAB025E0-8341-4A7B-9B65-115430DED230}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AAB025E0-8341-4A7B-9B65-115430DED230}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AAB025E0-8341-4A7B-9B65-115430DED230}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AAB025E0-8341-4A7B-9B65-115430DED230}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/README.md b/README.md index a2864ee..70550df 100644 --- a/README.md +++ b/README.md @@ -73,4 +73,82 @@ if (a < b) { // Nothing will always be less than any int value } -``` \ No newline at end of file +``` + +# Documentation + +## MaybeExtensions.OrEmpty +Allow to get the encapsulated value or an empty string. + +Returns the maybe value if HasValue is true, otherwise `string.Empty`. + +### Examples +``` +var maybe = "some value".ToMaybe(); +var v = maybe.OrEmpty(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: some value +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.OrEmpty(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: +``` + +## MaybeExtensions.OrNull +Allow to get the encapsulated value or null. + +Returns the maybe value if HasValue is true, otherwise `null`. + +### Examples +``` +var maybe = "some value".ToMaybe(); +var v = maybe.OrNull(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: some value +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.OrNull(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: +``` + +## MaybeExtensions.OrTrue +Allow to get the boolean maybe encapsulated value or True. + +Returns the maybe value if HasValue is true, otherwise `true`. + +### Examples +``` +var maybe = false.ToMaybe(); +var v = maybe.OrTrue(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: False +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.OrTrue(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: True +``` + +## MaybeExtensions.OrFalse +Allow to get the boolean maybe encapsulated value or False. + +Returns the maybe value if HasValue is true, otherwise `false`. + +### Examples +``` +var maybe = false.ToMaybe(); +var v = maybe.OrFalse(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: False +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.OrFalse(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: False +``` From a824151e8bb6ee7118d4971c1e42ccc696b94c99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luz?= Date: Mon, 13 Mar 2023 12:39:42 -0300 Subject: [PATCH 2/5] Included ToNullable at documentation and included its tests.. Imported all Method extensions to a Raw section in the md. --- Maybe.Documentation/MaybeExtensions.cs | 18 +++ Maybe.Documentation/Program.cs | 5 + README.md | 174 ++++++++++++++++++++++++- 3 files changed, 196 insertions(+), 1 deletion(-) diff --git a/Maybe.Documentation/MaybeExtensions.cs b/Maybe.Documentation/MaybeExtensions.cs index 5f83338..1092aea 100644 --- a/Maybe.Documentation/MaybeExtensions.cs +++ b/Maybe.Documentation/MaybeExtensions.cs @@ -76,5 +76,23 @@ public static void OrFalseExample_WithoutValue() Console.WriteLine($"Print Value: {v}"); // Print Value: False } + + internal static void ToNullableExample_WithValue() + { + Console.WriteLine("ToNullable example with value"); + var maybe = 1234.ToMaybe(); + var v = maybe.ToNullable(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: 1234 + } + + internal static void ToNullableExample_WithoutValue() + { + Console.WriteLine("ToNullable example without value"); + var maybe = Maybe.Nothing; + var v = maybe.ToNullable(); + Console.WriteLine($"Print Value: {v}"); + // Print Value: + } } } diff --git a/Maybe.Documentation/Program.cs b/Maybe.Documentation/Program.cs index 33e12b7..16ab2d9 100644 --- a/Maybe.Documentation/Program.cs +++ b/Maybe.Documentation/Program.cs @@ -13,4 +13,9 @@ MaybeExtensions.OrFalseExample_WithValue(); MaybeExtensions.OrFalseExample_WithoutValue(); + +//ToNullable +MaybeExtensions.ToNullableExample_WithValue(); +MaybeExtensions.ToNullableExample_WithoutValue(); + Console.ReadLine(); \ No newline at end of file diff --git a/README.md b/README.md index 70550df..74ae586 100644 --- a/README.md +++ b/README.md @@ -78,10 +78,17 @@ if (a < b) # Documentation ## MaybeExtensions.OrEmpty + +### Remarks Allow to get the encapsulated value or an empty string. Returns the maybe value if HasValue is true, otherwise `string.Empty`. +### Method definition +``` +public static string OrEmpty(this Maybe subject); +``` + ### Examples ``` var maybe = "some value".ToMaybe(); @@ -97,10 +104,16 @@ Console.WriteLine($"Print Value: {v}"); ``` ## MaybeExtensions.OrNull +### Remarks Allow to get the encapsulated value or null. Returns the maybe value if HasValue is true, otherwise `null`. +### Method definition +``` +public static T OrNull(this Maybe subject) where T : class; +``` + ### Examples ``` var maybe = "some value".ToMaybe(); @@ -116,10 +129,14 @@ Console.WriteLine($"Print Value: {v}"); ``` ## MaybeExtensions.OrTrue +### Remarks Allow to get the boolean maybe encapsulated value or True. Returns the maybe value if HasValue is true, otherwise `true`. - +### Method definition +``` +public static bool OrTrue(this Maybe subject); +``` ### Examples ``` var maybe = false.ToMaybe(); @@ -135,10 +152,15 @@ Console.WriteLine($"Print Value: {v}"); ``` ## MaybeExtensions.OrFalse +### Remarks Allow to get the boolean maybe encapsulated value or False. Returns the maybe value if HasValue is true, otherwise `false`. +### Method defintion +``` +public static bool OrFalse(this Maybe subject); +``` ### Examples ``` var maybe = false.ToMaybe(); @@ -152,3 +174,153 @@ var v = maybe.OrFalse(); Console.WriteLine($"Print Value: {v}"); // Print Value: False ``` + + +## MaybeExtensions.ToNullable +### Remarks +Convert a Maybe to Nullable. + +If the Maybe.HasValue is true, return its `value` otherwise a `null`. + +### Method definitions +``` +public static T? ToNullable(this Maybe value) where T : struct +``` +### Examples +``` +var maybe = 1234.ToMaybe(); +var v = maybe.ToNullable(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: 1234 +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.ToNullable(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: +``` + +## MaybeExtensions.ToMaybe +### Remarks +/// Converts the value to Maybe<S>. +/// Maybe<>.Nothing if value.HasValue is false, +/// otherwise new Maybe<>(value.Value) +/// The value to be converted. +### Method definitions +``` +``` +### Examples +``` +``` +``` +``` + +# RAW DATA + +## MaybeExtensions.ToMaybe +overloads + +/// Converts the value to Maybe<S>. +/// Maybe<>.Nothing if value.HasValue is false, +/// otherwise new Maybe<>(value.Value) +/// The value to be converted. + +Maybe<S> ToMaybe<S>(this S? value) where S : struct + +/// +/// This methods prevents stacking maybes, it just returns value +/// +/// +/// value +/// +/// The value to be converted. + +Maybe ToMaybe(this Maybe value) + +/// +/// Converts the value to Maybe<>. +/// +/// +/// Maybe<>.Nothing if value is null, +/// otherwise new Maybe<>(value) +/// +/// The value to be converted. +Maybe ToMaybe(this T value) + + +## MaybeExtensions.Select + +/// +/// Projects the value according to the selector. +/// Analogous to Linq's Select. +/// +/// +/// Maybe<>.Nothing if subject.HasValue is false, +/// otherwise returns an instance of Maybe<> with the projected value +/// +/// The subject that will be projected. +/// The selector to be applied. +Maybe Select(this Maybe subject, Func selector) + +/// +/// Projects the value according to the selector. +/// Analogous to Linq's Select. +/// +/// +/// Maybe<>.Nothing if subject.HasValue is false, +/// otherwise returns an instance of Maybe<> with the projected value +/// +/// The subject that will be projected. +/// The selector to be applied. +Maybe Select(this Maybe subject, Func selector) + +## MaybeExtensions.SelectMany +/// +/// Projects the value according to the selector and flatten it. +/// Analogous to Linq's SelectMany. +/// +/// +/// Maybe<>.Nothing if subject.HasValue is false, +/// otherwise returns an instance of Maybe<> with the projected value +/// +/// The subject that will be projected. +/// The selector to be applied. +Maybe SelectMany(this Maybe subject, Func> selector) + + +## MaybeExtensions.Zip +### Overloads + +/// +/// Zips two maybes together. Analogous to Linq's Zip. +/// +/// +/// The zipped maybe +/// +/// The subject that will be projected. +/// The other maybe to be zipped. +/// The transformer function to be applied. +Maybe Zip(this Maybe subject, Maybe other, Func transformer) + +/// +/// Zips two maybes together. Analogous to Linq's Zip. +/// +/// +/// The zipped maybe +/// +/// The subject that will be projected. +/// The other maybe to be zipped. +/// The transformer function to be applied. +Maybe Zip(this Maybe subject, Maybe other, Func> transformer) + +## MaybeExtensions.ZipAndConsume +/// +/// Zips and consumes two maybes. +/// +/// The subject that will be projected. +/// The other maybe to be zipped. +/// The action to be applied to both maybes. +void ZipAndConsume(this Maybe subject, Maybe other, Action consumer) + +# End of Methods Extensions + From 140c8ac38092deaca4076efe5b1e116a9ccb11c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luz?= Date: Mon, 13 Mar 2023 12:41:33 -0300 Subject: [PATCH 3/5] Small fix --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 74ae586..1e01902 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,7 @@ Convert a Maybe to Nullable. If the Maybe.HasValue is true, return its `value` otherwise a `null`. -### Method definitions +### Method definition ``` public static T? ToNullable(this Maybe value) where T : struct ``` @@ -206,7 +206,7 @@ Console.WriteLine($"Print Value: {v}"); /// Maybe<>.Nothing if value.HasValue is false, /// otherwise new Maybe<>(value.Value) /// The value to be converted. -### Method definitions +### Method definition ``` ``` ### Examples From 035dbd63fe6636f23927ba3015ab797047b427f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luz?= Date: Tue, 14 Mar 2023 11:12:42 -0300 Subject: [PATCH 4/5] Included ToMaybe example and documentation --- Maybe.Documentation/MaybeExtensions.cs | 47 +++++++++++++++ Maybe.Documentation/Program.cs | 6 ++ README.md | 79 +++++++++++++++++++++----- 3 files changed, 118 insertions(+), 14 deletions(-) diff --git a/Maybe.Documentation/MaybeExtensions.cs b/Maybe.Documentation/MaybeExtensions.cs index 1092aea..169d7c0 100644 --- a/Maybe.Documentation/MaybeExtensions.cs +++ b/Maybe.Documentation/MaybeExtensions.cs @@ -94,5 +94,52 @@ internal static void ToNullableExample_WithoutValue() Console.WriteLine($"Print Value: {v}"); // Print Value: } + + internal static void ToMaybeExample_WithObjectValue() + { + Console.WriteLine("ToMaybe example with object"); + var myObject = new { PropertyA = "Value Of Property A", PropertyB = 66 }; + var maybe = myObject.ToMaybe(); + Console.WriteLine($"Print Value: {maybe}"); + // Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } + Console.WriteLine($"Maybe PropertyA value: {maybe.Select(o => o.PropertyA)}"); + // Maybe PropertyA value: Value Of Property A + Console.WriteLine($"Maybe PropertyB value: {maybe.Select(o => o.PropertyB)}"); + // Maybe PropertyB value: 66 + } + + internal static void ToMaybeExample_WithStructValue() + { + Console.WriteLine("ToMaybe example with struct"); + var myInt = 66; + var maybe = myInt.ToMaybe(); + Console.WriteLine($"Print Value: {maybe}"); + // Print Value: 66 + Console.WriteLine($"Print Maybe value: {maybe.Select(i => i)}"); + //Print Maybe value: 66 + } + + internal static void ToMaybeExample_WithMaybeValue() + { + Console.WriteLine("ToMaybe example with a Maybe of an object"); + var myObject = new { PropertyA = "Value Of Property A", PropertyB = 66 }; + var maybe = myObject.ToMaybe(); + var secondMaybe = maybe.ToMaybe(); + Console.WriteLine($"First Maybe Print Value: {maybe}"); + // First Maybe Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } + Console.WriteLine($"Second Maybe Print Value: {secondMaybe}"); + // Second Maybe Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } + } + + internal static void ToMaybeExample_WithMaybeNothingValue() + { + Console.WriteLine("ToMaybe example with a Maybe of an object"); + var maybe = Maybe.Nothing; + var secondMaybe = maybe.ToMaybe(); + Console.WriteLine($"First Maybe Print Value: {maybe}"); + // First Maybe Print Value: + Console.WriteLine($"Second Maybe Print Value: {secondMaybe}"); + // Second Maybe Print Value: + } } } diff --git a/Maybe.Documentation/Program.cs b/Maybe.Documentation/Program.cs index 16ab2d9..febdf1e 100644 --- a/Maybe.Documentation/Program.cs +++ b/Maybe.Documentation/Program.cs @@ -18,4 +18,10 @@ MaybeExtensions.ToNullableExample_WithValue(); MaybeExtensions.ToNullableExample_WithoutValue(); +//ToMaybe +MaybeExtensions.ToMaybeExample_WithObjectValue(); +MaybeExtensions.ToMaybeExample_WithStructValue(); +MaybeExtensions.ToMaybeExample_WithMaybeValue(); +MaybeExtensions.ToMaybeExample_WithMaybeNothingValue(); + Console.ReadLine(); \ No newline at end of file diff --git a/README.md b/README.md index 1e01902..3168a91 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,71 @@ if (a < b) # Documentation + +## MaybeExtensions.ToMaybe +### Remarks +Converts the value to a Maybe. + +If the value is null, so it returns a Maybe.Nothing for that specific type. +For structs it always returns the Maybe. + +### Method definition +``` +public static Maybe ToMaybe(this T value) +``` +### Overloads +Overload to convert structs. +``` +public static Maybe ToMaybe(this S? value) where S : struct +``` + +This overload will always return the original Maybe, preveting the user to create a Maybe of a Maybe. +``` +public static Maybe ToMaybe(this Maybe value) +``` + +### Examples +Transform an object to a maybe and access its properties +``` +var myObject = new { PropertyA = "Value Of Property A", PropertyB = 66 }; +var maybe = myObject.ToMaybe(); +Console.WriteLine($"Print Value: {maybe}"); +// Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } +Console.WriteLine($"Maybe PropertyA value: {maybe.Select(o => o.PropertyA)}"); +// Maybe PropertyA value: Value Of Property A +Console.WriteLine($"Maybe PropertyB value: {maybe.Select(o => o.PropertyB)}"); +// Maybe PropertyB value: 66 +``` +Transform an int to a maybe and access it value. +``` +var myInt = 66; +var maybe = myInt.ToMaybe(); +Console.WriteLine($"Print Value: {maybe}"); +// Print Value: 66 +Console.WriteLine($"Print Maybe value: {maybe.Select(i => i)}"); +//Print Maybe value: 66 +``` +Try to transform a Maybe object to another Maybe. +``` +var myObject = new { PropertyA = "Value Of Property A", PropertyB = 66 }; +var maybe = myObject.ToMaybe(); +var secondMaybe = maybe.ToMaybe(); +Console.WriteLine($"First Maybe Print Value: {maybe}"); +// First Maybe Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } +Console.WriteLine($"Second Maybe Print Value: {secondMaybe}"); +// Second Maybe Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } + +``` +Try to transform a empty Maybe to another Maybe +``` +var maybe = Maybe.Nothing; +var secondMaybe = maybe.ToMaybe(); +Console.WriteLine($"First Maybe Print Value: {maybe}"); +// First Maybe Print Value: +Console.WriteLine($"Second Maybe Print Value: {secondMaybe}"); +// Second Maybe Print Value: +``` + ## MaybeExtensions.OrEmpty ### Remarks @@ -200,20 +265,6 @@ Console.WriteLine($"Print Value: {v}"); // Print Value: ``` -## MaybeExtensions.ToMaybe -### Remarks -/// Converts the value to Maybe<S>. -/// Maybe<>.Nothing if value.HasValue is false, -/// otherwise new Maybe<>(value.Value) -/// The value to be converted. -### Method definition -``` -``` -### Examples -``` -``` -``` -``` # RAW DATA From 8de6afe68c40b433f12c546f8cdcf6f97ce3e9bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luz?= Date: Tue, 21 Mar 2023 11:21:20 -0300 Subject: [PATCH 5/5] Moved Maybe Extension documentation to a proper file and make a link to it at README file --- README.md | 301 +--------------------------------------- docs/maybe-extension.md | 188 +++++++++++++++++++++++++ 2 files changed, 190 insertions(+), 299 deletions(-) create mode 100644 docs/maybe-extension.md diff --git a/README.md b/README.md index 3168a91..8717c16 100644 --- a/README.md +++ b/README.md @@ -75,303 +75,6 @@ if (a < b) } ``` -# Documentation - - -## MaybeExtensions.ToMaybe -### Remarks -Converts the value to a Maybe. - -If the value is null, so it returns a Maybe.Nothing for that specific type. -For structs it always returns the Maybe. - -### Method definition -``` -public static Maybe ToMaybe(this T value) -``` -### Overloads -Overload to convert structs. -``` -public static Maybe ToMaybe(this S? value) where S : struct -``` - -This overload will always return the original Maybe, preveting the user to create a Maybe of a Maybe. -``` -public static Maybe ToMaybe(this Maybe value) -``` - -### Examples -Transform an object to a maybe and access its properties -``` -var myObject = new { PropertyA = "Value Of Property A", PropertyB = 66 }; -var maybe = myObject.ToMaybe(); -Console.WriteLine($"Print Value: {maybe}"); -// Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } -Console.WriteLine($"Maybe PropertyA value: {maybe.Select(o => o.PropertyA)}"); -// Maybe PropertyA value: Value Of Property A -Console.WriteLine($"Maybe PropertyB value: {maybe.Select(o => o.PropertyB)}"); -// Maybe PropertyB value: 66 -``` -Transform an int to a maybe and access it value. -``` -var myInt = 66; -var maybe = myInt.ToMaybe(); -Console.WriteLine($"Print Value: {maybe}"); -// Print Value: 66 -Console.WriteLine($"Print Maybe value: {maybe.Select(i => i)}"); -//Print Maybe value: 66 -``` -Try to transform a Maybe object to another Maybe. -``` -var myObject = new { PropertyA = "Value Of Property A", PropertyB = 66 }; -var maybe = myObject.ToMaybe(); -var secondMaybe = maybe.ToMaybe(); -Console.WriteLine($"First Maybe Print Value: {maybe}"); -// First Maybe Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } -Console.WriteLine($"Second Maybe Print Value: {secondMaybe}"); -// Second Maybe Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } - -``` -Try to transform a empty Maybe to another Maybe -``` -var maybe = Maybe.Nothing; -var secondMaybe = maybe.ToMaybe(); -Console.WriteLine($"First Maybe Print Value: {maybe}"); -// First Maybe Print Value: -Console.WriteLine($"Second Maybe Print Value: {secondMaybe}"); -// Second Maybe Print Value: -``` - -## MaybeExtensions.OrEmpty - -### Remarks -Allow to get the encapsulated value or an empty string. - -Returns the maybe value if HasValue is true, otherwise `string.Empty`. - -### Method definition -``` -public static string OrEmpty(this Maybe subject); -``` - -### Examples -``` -var maybe = "some value".ToMaybe(); -var v = maybe.OrEmpty(); -Console.WriteLine($"Print Value: {v}"); -// Print Value: some value -``` -``` -var maybe = Maybe.Nothing; -var v = maybe.OrEmpty(); -Console.WriteLine($"Print Value: {v}"); -// Print Value: -``` - -## MaybeExtensions.OrNull -### Remarks -Allow to get the encapsulated value or null. - -Returns the maybe value if HasValue is true, otherwise `null`. - -### Method definition -``` -public static T OrNull(this Maybe subject) where T : class; -``` - -### Examples -``` -var maybe = "some value".ToMaybe(); -var v = maybe.OrNull(); -Console.WriteLine($"Print Value: {v}"); -// Print Value: some value -``` -``` -var maybe = Maybe.Nothing; -var v = maybe.OrNull(); -Console.WriteLine($"Print Value: {v}"); -// Print Value: -``` - -## MaybeExtensions.OrTrue -### Remarks -Allow to get the boolean maybe encapsulated value or True. - -Returns the maybe value if HasValue is true, otherwise `true`. -### Method definition -``` -public static bool OrTrue(this Maybe subject); -``` -### Examples -``` -var maybe = false.ToMaybe(); -var v = maybe.OrTrue(); -Console.WriteLine($"Print Value: {v}"); -// Print Value: False -``` -``` -var maybe = Maybe.Nothing; -var v = maybe.OrTrue(); -Console.WriteLine($"Print Value: {v}"); -// Print Value: True -``` - -## MaybeExtensions.OrFalse -### Remarks -Allow to get the boolean maybe encapsulated value or False. - -Returns the maybe value if HasValue is true, otherwise `false`. - -### Method defintion -``` -public static bool OrFalse(this Maybe subject); -``` -### Examples -``` -var maybe = false.ToMaybe(); -var v = maybe.OrFalse(); -Console.WriteLine($"Print Value: {v}"); -// Print Value: False -``` -``` -var maybe = Maybe.Nothing; -var v = maybe.OrFalse(); -Console.WriteLine($"Print Value: {v}"); -// Print Value: False -``` - - -## MaybeExtensions.ToNullable -### Remarks -Convert a Maybe to Nullable. - -If the Maybe.HasValue is true, return its `value` otherwise a `null`. - -### Method definition -``` -public static T? ToNullable(this Maybe value) where T : struct -``` -### Examples -``` -var maybe = 1234.ToMaybe(); -var v = maybe.ToNullable(); -Console.WriteLine($"Print Value: {v}"); -// Print Value: 1234 -``` -``` -var maybe = Maybe.Nothing; -var v = maybe.ToNullable(); -Console.WriteLine($"Print Value: {v}"); -// Print Value: -``` - - -# RAW DATA - -## MaybeExtensions.ToMaybe -overloads - -/// Converts the value to Maybe<S>. -/// Maybe<>.Nothing if value.HasValue is false, -/// otherwise new Maybe<>(value.Value) -/// The value to be converted. - -Maybe<S> ToMaybe<S>(this S? value) where S : struct - -/// -/// This methods prevents stacking maybes, it just returns value -/// -/// -/// value -/// -/// The value to be converted. - -Maybe ToMaybe(this Maybe value) - -/// -/// Converts the value to Maybe<>. -/// -/// -/// Maybe<>.Nothing if value is null, -/// otherwise new Maybe<>(value) -/// -/// The value to be converted. -Maybe ToMaybe(this T value) - - -## MaybeExtensions.Select - -/// -/// Projects the value according to the selector. -/// Analogous to Linq's Select. -/// -/// -/// Maybe<>.Nothing if subject.HasValue is false, -/// otherwise returns an instance of Maybe<> with the projected value -/// -/// The subject that will be projected. -/// The selector to be applied. -Maybe Select(this Maybe subject, Func selector) - -/// -/// Projects the value according to the selector. -/// Analogous to Linq's Select. -/// -/// -/// Maybe<>.Nothing if subject.HasValue is false, -/// otherwise returns an instance of Maybe<> with the projected value -/// -/// The subject that will be projected. -/// The selector to be applied. -Maybe Select(this Maybe subject, Func selector) - -## MaybeExtensions.SelectMany -/// -/// Projects the value according to the selector and flatten it. -/// Analogous to Linq's SelectMany. -/// -/// -/// Maybe<>.Nothing if subject.HasValue is false, -/// otherwise returns an instance of Maybe<> with the projected value -/// -/// The subject that will be projected. -/// The selector to be applied. -Maybe SelectMany(this Maybe subject, Func> selector) - - -## MaybeExtensions.Zip -### Overloads - -/// -/// Zips two maybes together. Analogous to Linq's Zip. -/// -/// -/// The zipped maybe -/// -/// The subject that will be projected. -/// The other maybe to be zipped. -/// The transformer function to be applied. -Maybe Zip(this Maybe subject, Maybe other, Func transformer) - -/// -/// Zips two maybes together. Analogous to Linq's Zip. -/// -/// -/// The zipped maybe -/// -/// The subject that will be projected. -/// The other maybe to be zipped. -/// The transformer function to be applied. -Maybe Zip(this Maybe subject, Maybe other, Func> transformer) - -## MaybeExtensions.ZipAndConsume -/// -/// Zips and consumes two maybes. -/// -/// The subject that will be projected. -/// The other maybe to be zipped. -/// The action to be applied to both maybes. -void ZipAndConsume(this Maybe subject, Maybe other, Action consumer) - -# End of Methods Extensions +# See more details +[Maybe Extension](docs/maybe-extension.md) \ No newline at end of file diff --git a/docs/maybe-extension.md b/docs/maybe-extension.md new file mode 100644 index 0000000..9c9033c --- /dev/null +++ b/docs/maybe-extension.md @@ -0,0 +1,188 @@ +# Maybe Extension Documentation + +## MaybeExtensions.ToMaybe +### Remarks +Converts the value to a Maybe. + +If the value is null, so it returns a Maybe.Nothing for that specific type. +For structs it always returns the Maybe. + +### Method definition +``` +public static Maybe ToMaybe(this T value) +``` +### Overloads +Overload to convert structs. +``` +public static Maybe ToMaybe(this S? value) where S : struct +``` + +This overload will always return the original Maybe, preveting the user to create a Maybe of a Maybe. +``` +public static Maybe ToMaybe(this Maybe value) +``` + +### Examples +Transform an object to a maybe and access its properties +``` +var myObject = new { PropertyA = "Value Of Property A", PropertyB = 66 }; +var maybe = myObject.ToMaybe(); +Console.WriteLine($"Print Value: {maybe}"); +// Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } +Console.WriteLine($"Maybe PropertyA value: {maybe.Select(o => o.PropertyA)}"); +// Maybe PropertyA value: Value Of Property A +Console.WriteLine($"Maybe PropertyB value: {maybe.Select(o => o.PropertyB)}"); +// Maybe PropertyB value: 66 +``` +Transform an int to a maybe and access it value. +``` +var myInt = 66; +var maybe = myInt.ToMaybe(); +Console.WriteLine($"Print Value: {maybe}"); +// Print Value: 66 +Console.WriteLine($"Print Maybe value: {maybe.Select(i => i)}"); +//Print Maybe value: 66 +``` +Try to transform a Maybe object to another Maybe. +``` +var myObject = new { PropertyA = "Value Of Property A", PropertyB = 66 }; +var maybe = myObject.ToMaybe(); +var secondMaybe = maybe.ToMaybe(); +Console.WriteLine($"First Maybe Print Value: {maybe}"); +// First Maybe Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } +Console.WriteLine($"Second Maybe Print Value: {secondMaybe}"); +// Second Maybe Print Value: { PropertyA = Value Of Property A, PropertyB = 66 } + +``` +Try to transform a empty Maybe to another Maybe +``` +var maybe = Maybe.Nothing; +var secondMaybe = maybe.ToMaybe(); +Console.WriteLine($"First Maybe Print Value: {maybe}"); +// First Maybe Print Value: +Console.WriteLine($"Second Maybe Print Value: {secondMaybe}"); +// Second Maybe Print Value: +``` + +## MaybeExtensions.OrEmpty + +### Remarks +Allow to get the encapsulated value or an empty string. + +Returns the maybe value if HasValue is true, otherwise `string.Empty`. + +### Method definition +``` +public static string OrEmpty(this Maybe subject); +``` + +### Examples +``` +var maybe = "some value".ToMaybe(); +var v = maybe.OrEmpty(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: some value +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.OrEmpty(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: +``` + +## MaybeExtensions.OrNull +### Remarks +Allow to get the encapsulated value or null. + +Returns the maybe value if HasValue is true, otherwise `null`. + +### Method definition +``` +public static T OrNull(this Maybe subject) where T : class; +``` + +### Examples +``` +var maybe = "some value".ToMaybe(); +var v = maybe.OrNull(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: some value +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.OrNull(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: +``` + +## MaybeExtensions.OrTrue +### Remarks +Allow to get the boolean maybe encapsulated value or True. + +Returns the maybe value if HasValue is true, otherwise `true`. +### Method definition +``` +public static bool OrTrue(this Maybe subject); +``` +### Examples +``` +var maybe = false.ToMaybe(); +var v = maybe.OrTrue(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: False +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.OrTrue(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: True +``` + +## MaybeExtensions.OrFalse +### Remarks +Allow to get the boolean maybe encapsulated value or False. + +Returns the maybe value if HasValue is true, otherwise `false`. + +### Method defintion +``` +public static bool OrFalse(this Maybe subject); +``` +### Examples +``` +var maybe = false.ToMaybe(); +var v = maybe.OrFalse(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: False +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.OrFalse(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: False +``` + + +## MaybeExtensions.ToNullable +### Remarks +Convert a Maybe to Nullable. + +If the Maybe.HasValue is true, return its `value` otherwise a `null`. + +### Method definition +``` +public static T? ToNullable(this Maybe value) where T : struct +``` +### Examples +``` +var maybe = 1234.ToMaybe(); +var v = maybe.ToNullable(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: 1234 +``` +``` +var maybe = Maybe.Nothing; +var v = maybe.ToNullable(); +Console.WriteLine($"Print Value: {v}"); +// Print Value: +``` \ No newline at end of file