Skip to content

Commit 4e6ddc3

Browse files
author
Jonas Juselius
committed
Poetry night!
1 parent 781675d commit 4e6ddc3

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed

src/FsPoem/FsPoem.fsproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<?xml version="1.0" encoding="utf-8"?>
21
<Project Sdk="Microsoft.NET.Sdk">
32
<PropertyGroup>
43
<OutputType>Exe</OutputType>
54
<TargetFramework>netcoreapp2.1</TargetFramework>
65
</PropertyGroup>
76
<ItemGroup>
7+
<Compile Include="Poem.fs" />
88
<Compile Include="Program.fs" />
99
</ItemGroup>
1010
<Import Project="..\..\.paket\Paket.Restore.targets" />

src/FsPoem/Poem.fs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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)

src/FsPoem/Program.fs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
// Learn more about F# at http://fsharp.org
1+
namespace FsPoem
22

3-
open System
3+
module Main =
4+
open Poetry
45

5-
[<EntryPoint>]
6-
let main argv =
7-
printfn "Hello World from F#!"
8-
0 // return an integer exit code
6+
[<EntryPoint>]
7+
let main argv =
8+
printfn "Hello World from F#!"
9+
Poetry.run ()
10+
0

src/FsPoem/siphonaptera.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Big fleas have little fleas upon their backs to bite 'em,
2+
And little fleas have lesser fleas, and so, ad infinitum.
3+
And the great fleas, themselves, in turn, have greater fleas to go on;
4+
While these again have greater still, and greater still, and so on.

0 commit comments

Comments
 (0)