1
+ namespace FsPoem
2
+
3
+ module Poetry =
4
+ open System.IO
5
+
6
+ let lines ( s : string ) = s.Split [| '\n' |] |> List.ofArray
7
+
8
+ let unlines ( sl : string list ) =
9
+ sl |> List.fold ( fun a s -> a + s + " \n " ) " "
10
+
11
+ let sortTextLines s = unlines ( List.sort ( lines s))
12
+ let sortTextLines ' s = s |> lines |> List.sort |> unlines
13
+ let sortTextLines '' = lines >> List.sort >> unlines
14
+
15
+ let sortLines s = s |> lines |> List.sort |> unlines
16
+ let revLines s = s |> lines |> List.rev |> unlines
17
+ let twoLines s = s |> lines |> List.take 2 |> unlines
18
+
19
+ let byLines f s = s |> lines |> f |> unlines
20
+
21
+ let sortLines ' = byLines List.sort
22
+ let revLines ' = byLines List.rev
23
+ let twoLines ' = byLines ( List.take 2 )
24
+
25
+ let indent s = " " + s
26
+ let indent ' = (+) " "
27
+
28
+ let indentEachLine = byLines ( List.map indent)
29
+
30
+ let eachLine f s = s |> lines |> List.map f |> unlines
31
+ let eachLine ' f = lines >> List.map f >> unlines
32
+ let eachLine '' f = byLines ( List.map f)
33
+
34
+ let indentEachLine ' = eachLine indent
35
+
36
+ let private yell ( s : string ) = s.ToUpper () + " !!! "
37
+
38
+ let yellEachLine = eachLine yell
39
+
40
+ let words ( s : string ) = s.Split [| ' ' |] |> List.ofArray
41
+
42
+ let unwords = List.fold (+) " "
43
+
44
+ let eachWord f = words >> List.map f >> unwords
45
+
46
+ let yellEachWord = eachWord yell
47
+
48
+ let eachWordOnEachLine f = eachLine ( eachWord f)
49
+
50
+ let yellEachWordOnEachLine = eachWordOnEachLine yell
51
+
52
+ let poem = File.ReadAllText " siphonaptera.txt"
53
+ let sortedPoem = sortLines poem
54
+
55
+ let run () =
56
+ printfn " %s " ( revLines poem)
57
+ printfn " %s " ( poem |> revLines |> twoLines)
58
+ printfn " %s " ( poem |> indentEachLine)
59
+ printfn " %s " ( poem |> yellEachLine)
60
+ printfn " %s " ( poem |> yellEachWord)
61
+ printfn " %s " ( poem |> yellEachWordOnEachLine |> indentEachLine)
0 commit comments