Skip to content

Commit

Permalink
Merge pull request #274 from darkdrag00nv2/filter_array
Browse files Browse the repository at this point in the history
Document `filter` function in array types
  • Loading branch information
nialexsan authored Oct 25, 2023
2 parents 1338d75 + 04a55c3 commit c8cddcf
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions docs/cadence/language/values-and-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: fun(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: fun(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.
Expand Down

0 comments on commit c8cddcf

Please sign in to comment.