Overview

Common Fβ™― collections

Module
Type
-
BCL Equivalent
Immutable
Structural comparison

Array

'T array

≑

Array<T>

❌

βœ…

List

'T list

≃

ImmutableList<T>

βœ…

βœ…

Seq

seq<'T>

≑

IEnumerable<T>

βœ…

βœ…

Set

Set<'T>

≃

ImmutableHashSet<T>

βœ…

βœ…

Map

Map<'K, 'V>

≃

ImmutableDictionary<K,V>

βœ…

βœ…

❌

dict

≑

IDictionary<K,V>

β˜‘οΈ ❗

❌

❌

readOnlyDict

≑

IReadOnlyDictionary<K,V>

β˜‘οΈ

❌

❌

ResizeArray

≑

List<T>

❌

❌

Functions consistency πŸ‘

Common to all 5 modules:

  • empty/isEmpty, exists/forall

  • find/tryFind, pick/tryPick, contains (containsKey for Map)

  • map/iter, filter, fold

Common to Array, List, Seq:

  • append/concat, choose, collect

  • item, head, last

  • take, skip

  • ... a hundred functions altogether!

Syntax consistency πŸ‘

Type
Construction
Range
ComprehensionπŸ“

Array

[∣ 1; 2 ∣]

[∣ 1..5 ∣]

βœ…

List

[ 1; 2 ]

[ 1..5 ]

βœ…

Seq

seq { 1; 2 }

seq { 1..5 }

βœ…

Set

set [ 1; 2 ]

set [ 1..5 ]

βœ…

Syntax trap ⚠️

Square brackets [] are used both for:

  • Value: instance of a list [ 1; 2 ] (of type int list)

  • Type: array int [], e.g. of [| 1; 2 |]

☝ Recommendations

  • Distinguish between type vs value ❗

  • Write int array rather than int[]

Comprehension

  • Purpose: syntactic sugar to construct collection

    • Handy, succinct, powerful

    • Syntax includes for loops, if condition

  • Same principle as generators in Cβ™―, JS

    • yield keyword but often optional (since Fβ™― 4.7)

    • yield! keyword (pronounce "yield bang") ≑ yield* in JS

    • Works for all collections πŸ‘

Examples:

Flattening:

Last updated

Was this helpful?