F# Training
F# Training
F# Training
  • Presentation
  • Fundamentals
    • Introduction
    • Syntax
      • Bases
      • Functions
      • Rules
      • Exceptions
    • First concepts
    • πŸ”Quiz
  • Functions
    • Signature
    • Concepts
    • Syntax
    • Standard functions
    • Operators
    • Addendum
    • πŸ”Quiz
    • πŸ“œSummary
  • Types
    • Overview
    • Tuples
    • Records
    • Unions
    • Enums
    • Anonymous records
    • Value types
    • πŸ“œRecap
    • Addendum
  • Monadic types
    • Intro
    • Option type
    • Result type
    • Smart constructor
    • πŸš€Computation expression (CE)
    • πŸš€CE theoretical basements
    • πŸ“œRecap
  • Pattern matching
    • Patterns
    • Match expression
    • Active patterns
    • πŸš€Fold function
    • πŸ“œRecap
    • πŸ•ΉοΈExercises
  • Collections
    • Overview
    • Types
    • Common functions
    • Dedicated functions
    • πŸ”Quiz
    • πŸ“œRecap
  • Asynchronous programming
    • Asynchronous workflow
    • Interop with .NET TPL
    • πŸ“œRecap
  • Module and Namespace
    • Overview
    • Namespace
    • Module
    • πŸ”Quiz
    • πŸ“œRecap
  • Object-oriented
    • Introduction
    • Members
    • Type extensions
    • Class, Struct
    • Interface
    • Object expression
    • Recommendations
Powered by GitBook
On this page
  • Common Fβ™― collections
  • Functions consistency πŸ‘
  • Syntax consistency πŸ‘
  • Syntax trap ⚠️
  • Comprehension

Was this helpful?

Edit on GitHub
  1. Collections

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:

// Multi-line (recommended)
let squares =
    seq { for i in 1 .. 10 do
        yield i * i // πŸ’‘ 'yield' can be omitted most of the time πŸ‘
    }

// Single line
let squares = seq { for i in 1 .. 10 -> i * i }

// Can contain 'if'
let halfEvens =
    [ for i in [1..10] do
        if (i % 2) = 0 then i / 2 ]  // [1; 2; 3; 4; 5]

// Nested 'for'
let pairs =
    [ for i in [1..3] do
      for j in [1..3] do
        i, j ]              // [(1, 1); (1; 2); (1; 3); (2, 1); ... (3, 3)]

Flattening:

// Multiple items
let twoToNine =
    [ for i in [1; 4; 7] do
        if i > 1 then i
        i + 1
        i + 2 ]  // [2; 3; 4; 5; 6; 7; 8; 9]

// With 'yield! collections'
let oneToSix =
    [ for i in [1; 3; 5] do
        yield! [i; i+1] ]
PreviousExercisesNextTypes

Last updated 1 month ago

Was this helpful?