-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_Functions.fs
94 lines (65 loc) · 2.33 KB
/
02_Functions.fs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
module fsharp_demo.Functions
open System
// funkce
module m1 =
let add x y = x + y
// volání funkce bez ()
let res = add 5 6
// funkce bez parametrů / bez návratové hodnoty používá unit ()
let hello () = printfn "hello!"
// lambda funkce
let add' = fun x y -> x + y
let add'' =
let f x y = x + y
f
// higher-order funkce
module m2 =
let nums = [1;2;3]
let square x = x*x
let squared_nums = List.map square nums
// parciální aplikace / currying - kuk na signaturu
let add x y = x + y
let add20 x = add 20 x
let add20' = add 20
let res = add20 10
// operátory |> a >>
module m3 =
let f() =
let sin, cos, tan = Math.Sin, Math.Cos, Math.Tan
// :(
let calc x = tan (cos (sin x))
let pipe x f = f x
let sin x = pipe x sin
let cos x = pipe x cos
let tan x = pipe x tan
// moc jsme si nepomohli
let calc x = pipe (pipe (pipe x sin) cos) tan
// ale binární operátory jsou infixové! zkusme to tedy přepsat
let (|>) x f = f x
// takže...
let sin x = (|>) x sin
// ...se dá ekvivaltentně zapsat jako
let sin x = x |> sin
// tedy původní zápis můžeme upravit na
let calc x = (|>) ((|>) ((|>) x sin) cos) tan
let calc x = (|>) ((|>) (x |> sin) cos) tan
let calc x = (|>) ((x |> sin) |> cos) tan
let calc x = ((x |> sin) |> cos) |> tan
// po odstranění závorek už to vypadá hezky!
let calc x = x |> sin |> cos |> tan
// dobrá zpráva - |> je zabudovaný operátor!
// :)))
// >> composition operátor
// let (>>) f1 f2 = fun x -> x |> f1 |> f2
let calc = sin >> cos >> tan
()
module m4 =
open Microsoft.Extensions.FileSystemGlobbing
// vlastní operátory
let (@) x y = System.IO.Path.Combine(x, y)
let path = Environment.CurrentDirectory @ "foo.txt"
// disclaimer - nezkoušeno
let (!!) (glob:string) = Matcher().AddInclude(glob)
let (++) (files:Matcher) glob = files.AddInclude(glob)
let (--) (files:Matcher) glob = files.AddExclude(glob)
let files = !! "**/*.fs" ++ "**/*.fsx" -- "**/packages/**"