From 5d0f7d94afe5d278a4edc728c9a38234e073d87b Mon Sep 17 00:00:00 2001 From: darkdrag00n Date: Thu, 14 Sep 2023 23:49:46 +0530 Subject: [PATCH 1/4] Document filter function in array types --- docs/cadence/language/values-and-types.mdx | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/docs/cadence/language/values-and-types.mdx b/docs/cadence/language/values-and-types.mdx index 5d80c99122..4216e76104 100644 --- a/docs/cadence/language/values-and-types.mdx +++ b/docs/cadence/language/values-and-types.mdx @@ -1315,6 +1315,69 @@ are available for both variable-sized and fixed-sized or variable-sized arrays. let invalidMapFunctionExample = fixedSizedExample.map(functionAcceptingBool) ``` +- + ```cadence + access(all) + fun filter(_ f: (T: Bool)): [T] + ``` + + Returns a new array whose elements are filtered by applying the filter function on each element + of the original array. + Available if `T` is not resource-kinded. + + ```cadence + let example = [1, 2, 3] + let trueForEven = + fun (_ x: Int): Bool { + return x % 2 == 0 + } + + let filteredExample: [Int] = example.filter(trueForEven) + // `filteredExample` is `[2]` + // `example` still remains as `[1, 2, 3]` + + // Invalid: Filter using a function which accepts a different type. + // This results in a type error, as the array contains `Int` values while function accepts + // `Int64`. + let functionAcceptingInt64 = + fun (_ x: Int64): Bool { + return x % 2 == 0 + } + let invalidFilterFunctionExample = example.filter(functionAcceptingInt64) + ``` + + `filter` function is also available for fixed-sized arrays: + + ```cadence + access(all) + fun filter(_ f: (T: Bool)): [T] + ``` + + Returns a new **variable-sized** array whose elements are filtered by applying the filter function on each element + of the original array. + Available if `T` is not resource-kinded. + + ```cadence + let fixedSizedExample: [String; 3] = ["AB", "XYZYX", "PQR"] + let lengthOfStringGreaterThanTwo = + fun (_ x: String): Bool { + return x.length > 2 + } + + let fixedArrayFilteredExample = fixedSizedExample.filter(lengthOfStringGreaterThanTwo) + // `fixedArrayFilteredExample` is `["XYZYX", "PQR"]` + // `fixedSizedExample` still remains as ["AB", "XYZYX", "PQR"] + + // Invalid: Filter using a function which accepts a different type. + // This results in a type error, as the array contains `String` values while function accepts + // `Bool`. + let functionAcceptingBool = + fun (_ x: Bool): Bool { + return True + } + let invalidFilterFunctionExample = fixedSizedExample.filter(functionAcceptingBool) + ``` + #### Variable-size Array Functions The following functions can only be used on variable-sized arrays. From 49ced90bc0d58dec1a8fdea400b21f551797d89a Mon Sep 17 00:00:00 2001 From: darkdrag00nv2 <122124396+darkdrag00nv2@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:11:15 +0530 Subject: [PATCH 2/4] Update docs/cadence/language/values-and-types.mdx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Müller --- docs/cadence/language/values-and-types.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cadence/language/values-and-types.mdx b/docs/cadence/language/values-and-types.mdx index 4216e76104..9ec5892a80 100644 --- a/docs/cadence/language/values-and-types.mdx +++ b/docs/cadence/language/values-and-types.mdx @@ -1318,7 +1318,7 @@ are available for both variable-sized and fixed-sized or variable-sized arrays. - ```cadence access(all) - fun filter(_ f: (T: Bool)): [T] + fun filter(_ f: fun(T): Bool): [T] ``` Returns a new array whose elements are filtered by applying the filter function on each element From 80f581563ef31b938ca6278b82307508cc778d8d Mon Sep 17 00:00:00 2001 From: darkdrag00nv2 <122124396+darkdrag00nv2@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:11:22 +0530 Subject: [PATCH 3/4] Update docs/cadence/language/values-and-types.mdx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Müller --- docs/cadence/language/values-and-types.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/cadence/language/values-and-types.mdx b/docs/cadence/language/values-and-types.mdx index 9ec5892a80..268c0fc72f 100644 --- a/docs/cadence/language/values-and-types.mdx +++ b/docs/cadence/language/values-and-types.mdx @@ -1350,7 +1350,7 @@ are available for both variable-sized and fixed-sized or variable-sized arrays. ```cadence access(all) - fun filter(_ f: (T: Bool)): [T] + fun filter(_ f: fun(T): Bool): [T] ``` Returns a new **variable-sized** array whose elements are filtered by applying the filter function on each element From 04a55c3250abecdb725735b23edbc0609db95ff4 Mon Sep 17 00:00:00 2001 From: darkdrag00n Date: Sat, 7 Oct 2023 22:13:14 +0530 Subject: [PATCH 4/4] fix signature of map function --- docs/cadence/language/values-and-types.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/cadence/language/values-and-types.mdx b/docs/cadence/language/values-and-types.mdx index 268c0fc72f..df74f86b12 100644 --- a/docs/cadence/language/values-and-types.mdx +++ b/docs/cadence/language/values-and-types.mdx @@ -1255,7 +1255,7 @@ are available for both variable-sized and fixed-sized or variable-sized arrays. - ```cadence access(all) - fun map(_ f: (T: U)): [U] + fun map(_ f: fun(T): U): [U] ``` Returns a new array whose elements are produced by applying the mapper function on each element @@ -1287,7 +1287,7 @@ are available for both variable-sized and fixed-sized or variable-sized arrays. ```cadence access(all) - fun map(_ f: (T: U)): [U; N] + fun map(_ f: fun(T): U): [U; N] ``` Returns a new fixed-sized array whose elements are produced by applying the mapper function on