-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAboutPipelining.fs
More file actions
66 lines (49 loc) · 1.92 KB
/
AboutPipelining.fs
File metadata and controls
66 lines (49 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
namespace FSharpKoans
open FSharpKoans.Core
//----------------------------------------------------------------
// About Pipelining
//
// The forward pipe operator is one of the most commonly used
// symbols in F# programming. You can use it to combine operations
// on lists and other data structures in a readable way.
//----------------------------------------------------------------
[<Koan(Sort = 10)>]
module ``about pipelining`` =
let square x =
x * x
let isEven x =
x % 2 = 0
[<Koan>]
let SquareEvenNumbersWithSeparateStatements() =
(* One way to combine the operations is by using separate statements.
However, this can be clumsy since you have to name each result. *)
let numbers = [0..5]
let evens = List.filter isEven numbers
let result = List.map square evens
AssertEquality result __
[<Koan>]
let SquareEvenNumbersWithParens() =
(* You can avoid this problem by using parens to pass the result of one
function to another. This can be difficult to read since you have to
start from the innermost function and work your way out. *)
let numbers = [0..5]
let result = List.map square (List.filter isEven numbers)
AssertEquality result __
[<Koan>]
let SquareEvenNumbersWithPipelineOperator() =
(* In F#, you can use the pipeline operator to get the benefit of the
parens style with the readability of the statement style. *)
let result =
[0..5]
|> List.filter isEven
|> List.map square
AssertEquality result __
[<Koan>]
let HowThePipeOperatorIsDefined() =
let (|>) x f =
f x
let result =
[0..5]
|> List.filter isEven
|> List.map square
AssertEquality result __