Skip to content

Commit 2efb355

Browse files
authored
Merge pull request #922 from 1eyewonder/seq-optimizations
Minor Seq Optimizations
2 parents 1ce0fae + 6b8d5c9 commit 2efb355

File tree

7 files changed

+105
-89
lines changed

7 files changed

+105
-89
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
]
1010
},
1111
"fsharp-analyzers": {
12-
"version": "0.24.0",
12+
"version": "0.26.0",
1313
"commands": [
1414
"fsharp-analyzers"
1515
]

Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
2828
<PackageVersion Include="Ionide.KeepAChangelog.Tasks" Version="0.1.8" />
2929
<PackageVersion Include="FSharp.Analyzers.Build" Version="0.3.0" />
30-
<PackageVersion Include="G-Research.FSharp.Analyzers" Version="0.8.0" />
31-
<PackageVersion Include="Ionide.Analyzers" Version="0.8.0" />
30+
<PackageVersion Include="G-Research.FSharp.Analyzers" Version="0.10.0" />
31+
<PackageVersion Include="Ionide.Analyzers" Version="0.11.0" />
3232
</ItemGroup>
3333
</Project>

src/Common/StringParsing.fs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ module String =
108108
let removeSpaces (lines: string list) =
109109
let spaces =
110110
lines
111-
|> Seq.filter (String.IsNullOrWhiteSpace >> not)
112-
|> Seq.map (fun line -> line |> Seq.takeWhile Char.IsWhiteSpace |> Seq.length)
111+
|> Seq.choose (fun line ->
112+
if String.IsNullOrWhiteSpace line |> not then
113+
line |> Seq.takeWhile Char.IsWhiteSpace |> Seq.length |> Some
114+
else
115+
None)
113116
|> fun xs -> if Seq.isEmpty xs then 0 else Seq.min xs
114117

115118
lines

src/FSharp.Formatting.ApiDocs/GenerateModel.fs

Lines changed: 78 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,8 +1282,9 @@ module internal TypeFormatter =
12821282
n
12831283

12841284
curriedArgs
1285-
|> List.map (List.map (fun x -> formatArgNameAndType (counter ()) x |> fst))
1286-
|> List.map (fun argTuple ->
1285+
|> List.map (fun args ->
1286+
let argTuple = args |> List.map (formatArgNameAndType (counter ()) >> fst)
1287+
12871288
match argTuple with
12881289
| [] -> !! "()"
12891290
| [ argName ] when argName = "()" -> !! "()"
@@ -2217,10 +2218,9 @@ module internal SymbolReader =
22172218
}
22182219

22192220
/// Returns whether the link is not included in the document defined links
2220-
let linkNotDefined (doc: LiterateDocument) (link: string) =
2221+
let linkDefined (doc: LiterateDocument) (link: string) =
22212222
[ link; link.Replace("\r\n", ""); link.Replace("\r\n", " "); link.Replace("\n", ""); link.Replace("\n", " ") ]
2222-
|> Seq.map (fun key -> not (doc.DefinedLinks.ContainsKey(key)))
2223-
|> Seq.reduce (fun a c -> a && c)
2223+
|> List.exists (fun key -> doc.DefinedLinks.ContainsKey(key))
22242224

22252225
/// Returns a tuple of the undefined link and its Cref if it exists
22262226
let getTypeLink (ctx: ReadingContext) undefinedLink =
@@ -2264,8 +2264,11 @@ module internal SymbolReader =
22642264
do
22652265
replacedParagraphs
22662266
|> Seq.collect collectParagraphIndirectLinks
2267-
|> Seq.filter (linkNotDefined doc)
2268-
|> Seq.map (getTypeLink ctx)
2267+
|> Seq.choose (fun line ->
2268+
if linkDefined doc line then
2269+
None
2270+
else
2271+
getTypeLink ctx line |> Some)
22692272
|> Seq.iter (addLinkToType doc)
22702273

22712274
doc.With(paragraphs = replacedParagraphs)
@@ -2275,15 +2278,12 @@ module internal SymbolReader =
22752278

22762279
let text =
22772280
lines
2278-
|> List.filter (
2279-
findCommand
2280-
>> (function
2281+
|> List.choose (fun line ->
2282+
match findCommand line with
22812283
| Some(k, v) ->
22822284
cmds.[k] <- v
2283-
false
2284-
| _ -> true)
2285-
)
2286-
|> List.map fst
2285+
None
2286+
| _ -> fst line |> Some)
22872287
|> String.concat "\n"
22882288

22892289
let doc =
@@ -2421,8 +2421,7 @@ module internal SymbolReader =
24212421

24222422
let readChildren ctx (entities: FSharpEntity seq) reader cond =
24232423
entities
2424-
|> Seq.filter (fun v -> checkAccess ctx v.Accessibility)
2425-
|> Seq.filter cond
2424+
|> Seq.filter (fun v -> checkAccess ctx v.Accessibility && cond v)
24262425
|> Seq.sortBy (fun (c: FSharpEntity) -> c.DisplayName)
24272426
|> Seq.choose (reader ctx)
24282427
|> List.ofSeq
@@ -2448,23 +2447,28 @@ module internal SymbolReader =
24482447

24492448
let readAllMembers ctx entityUrl kind (members: FSharpMemberOrFunctionOrValue seq) =
24502449
members
2451-
|> Seq.filter (fun v -> checkAccess ctx v.Accessibility)
2452-
|> Seq.filter (fun v ->
2453-
not v.IsCompilerGenerated
2454-
&& not v.IsPropertyGetterMethod
2455-
&& not v.IsPropertySetterMethod
2456-
&& not v.IsEventAddMethod
2457-
&& not v.IsEventRemoveMethod)
2458-
|> Seq.choose (tryReadMember ctx entityUrl kind)
2450+
|> Seq.choose (fun v ->
2451+
if
2452+
checkAccess ctx v.Accessibility
2453+
&& not v.IsCompilerGenerated
2454+
&& not v.IsPropertyGetterMethod
2455+
&& not v.IsPropertySetterMethod
2456+
&& not v.IsEventAddMethod
2457+
&& not v.IsEventRemoveMethod
2458+
then
2459+
tryReadMember ctx entityUrl kind v
2460+
else
2461+
None)
24592462
|> List.ofSeq
24602463
|> collectNamespaceDocs
24612464

24622465
let readMembers ctx entityUrl kind (entity: FSharpEntity) cond =
24632466
entity.MembersFunctionsAndValues
2464-
|> Seq.filter (fun v -> checkAccess ctx v.Accessibility)
2465-
|> Seq.filter (fun v -> not v.IsCompilerGenerated)
2466-
|> Seq.filter cond
2467-
|> Seq.choose (tryReadMember ctx entityUrl kind)
2467+
|> Seq.choose (fun v ->
2468+
if checkAccess ctx v.Accessibility && not v.IsCompilerGenerated && cond v then
2469+
tryReadMember ctx entityUrl kind v
2470+
else
2471+
None)
24682472
|> List.ofSeq
24692473
|> collectNamespaceDocs
24702474

@@ -2485,47 +2489,51 @@ module internal SymbolReader =
24852489
let readUnionCases ctx entityUrl (typ: FSharpEntity) =
24862490
typ.UnionCases
24872491
|> List.ofSeq
2488-
|> List.filter (fun v -> checkAccess ctx v.Accessibility)
24892492
|> List.choose (fun case ->
2490-
readCommentsInto case ctx case.XmlDocSig (fun cat catidx exclude _ comment ->
2491-
let details = readUnionCase ctx typ case
2492-
2493-
ApiDocMember(
2494-
case.Name,
2495-
readAttributes case.Attributes,
2496-
entityUrl,
2497-
ApiDocMemberKind.UnionCase,
2498-
cat,
2499-
catidx,
2500-
exclude,
2501-
details,
2502-
comment,
2503-
case,
2504-
ctx.WarnOnMissingDocs
2505-
)))
2493+
if checkAccess ctx case.Accessibility |> not then
2494+
None
2495+
else
2496+
readCommentsInto case ctx case.XmlDocSig (fun cat catidx exclude _ comment ->
2497+
let details = readUnionCase ctx typ case
2498+
2499+
ApiDocMember(
2500+
case.Name,
2501+
readAttributes case.Attributes,
2502+
entityUrl,
2503+
ApiDocMemberKind.UnionCase,
2504+
cat,
2505+
catidx,
2506+
exclude,
2507+
details,
2508+
comment,
2509+
case,
2510+
ctx.WarnOnMissingDocs
2511+
)))
25062512
|> collectNamespaceDocs
25072513

25082514
let readRecordFields ctx entityUrl (typ: FSharpEntity) =
25092515
typ.FSharpFields
25102516
|> List.ofSeq
2511-
|> List.filter (fun field -> not field.IsCompilerGenerated)
25122517
|> List.choose (fun field ->
2513-
readCommentsInto field ctx field.XmlDocSig (fun cat catidx exclude _ comment ->
2514-
let details = readFSharpField ctx field
2515-
2516-
ApiDocMember(
2517-
field.Name,
2518-
readAttributes (Seq.append field.FieldAttributes field.PropertyAttributes),
2519-
entityUrl,
2520-
ApiDocMemberKind.RecordField,
2521-
cat,
2522-
catidx,
2523-
exclude,
2524-
details,
2525-
comment,
2526-
field,
2527-
ctx.WarnOnMissingDocs
2528-
)))
2518+
if field.IsCompilerGenerated then
2519+
None
2520+
else
2521+
readCommentsInto field ctx field.XmlDocSig (fun cat catidx exclude _ comment ->
2522+
let details = readFSharpField ctx field
2523+
2524+
ApiDocMember(
2525+
field.Name,
2526+
readAttributes (Seq.append field.FieldAttributes field.PropertyAttributes),
2527+
entityUrl,
2528+
ApiDocMemberKind.RecordField,
2529+
cat,
2530+
catidx,
2531+
exclude,
2532+
details,
2533+
comment,
2534+
field,
2535+
ctx.WarnOnMissingDocs
2536+
)))
25292537
|> collectNamespaceDocs
25302538

25312539
let readStaticParams ctx entityUrl (typ: FSharpEntity) =
@@ -2585,11 +2593,12 @@ module internal SymbolReader =
25852593
if isNull nameAttr then
25862594
None
25872595
else
2588-
Some(nameAttr.Value, p.Value))
2589-
|> Seq.iter (fun (name, xmlDoc) ->
2590-
let xmlDocSig = getFSharpStaticParamXmlSig typ name
2596+
let xmlDocSig = getFSharpStaticParamXmlSig typ nameAttr.Value
25912597

2592-
registerXmlDoc ctx xmlDocSig (Security.SecurityElement.Escape xmlDoc) |> ignore)
2598+
registerXmlDoc ctx xmlDocSig (Security.SecurityElement.Escape p.Value)
2599+
|> ignore
2600+
|> Some)
2601+
|> ignore
25932602

25942603
let rec readType (ctx: ReadingContext) (typ: FSharpEntity) =
25952604
if typ.IsProvided && typ.XmlDoc <> FSharpXmlDoc.None then
@@ -2617,17 +2626,15 @@ module internal SymbolReader =
26172626

26182627
let ivals, svals =
26192628
getMembers typ
2620-
|> List.ofSeq
2621-
|> List.filter (fun v ->
2629+
|> Seq.filter (fun v ->
26222630
checkAccess ctx v.Accessibility
26232631
&& not v.IsCompilerGenerated
2624-
&& not v.IsOverrideOrExplicitInterfaceImplementation)
2625-
|> List.filter (fun v ->
2626-
not v.IsCompilerGenerated
2632+
&& not v.IsOverrideOrExplicitInterfaceImplementation
26272633
&& not v.IsEventAddMethod
26282634
&& not v.IsEventRemoveMethod
26292635
&& not v.IsPropertyGetterMethod
26302636
&& not v.IsPropertySetterMethod)
2637+
|> List.ofSeq
26312638
|> List.partition (fun v -> v.IsInstanceMember)
26322639

26332640
let cvals, svals = svals |> List.partition (fun v -> v.CompiledName = ".ctor")

src/FSharp.Formatting.Common/Templating.fs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ type FrontMatterFile =
4848
// Allow empty lines in frontmatter
4949
let isBlankLine = String.IsNullOrWhiteSpace line
5050
isBlankLine || line.Contains(":"))
51-
|> Seq.filter (String.IsNullOrWhiteSpace >> not)
52-
|> Seq.map (fun line ->
53-
let parts = line.Split(":")
54-
parts.[0].ToLowerInvariant(), parts.[1])
51+
|> Seq.choose (fun line ->
52+
if String.IsNullOrWhiteSpace line |> not then
53+
let parts = line.Split(":") |> Array.toList
54+
55+
match parts with
56+
| first :: second :: _ -> Some(first.ToLowerInvariant(), second)
57+
| _ -> None
58+
else
59+
None)
5560
|> Map.ofSeq
5661

5762
match

src/FSharp.Formatting.Common/YaafFSharpScripting.fs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ module internal CompilerServiceExtensions =
8383

8484
options.OtherOptions
8585
|> Array.filter (fun path -> path.StartsWith("-r:", StringComparison.Ordinal))
86-
|> Array.filter (fun path -> path.StartsWith("-r:", StringComparison.Ordinal))
8786
//|> Seq.choose (fun path -> if path.StartsWith "-r:" then path.Substring 3 |> Some else None)
8887
//|> Seq.map (fun path -> path.Replace("\\\\", "\\"))
8988
|> Array.toList)
@@ -180,11 +179,11 @@ module internal CompilerServiceExtensions =
180179
|| libDirs
181180
|> List.exists (fun lib ->
182181
Directory.EnumerateFiles(lib)
183-
|> Seq.filter (fun file -> Path.GetExtension file =? ".dll")
184182
|> Seq.filter (fun file ->
185183
// If we find a FSharp.Core in a lib path, we check if is suited for us...
186-
Path.GetFileNameWithoutExtension file <>? "FSharp.Core"
187-
|| (tryCheckFsCore file |> Option.isSome))
184+
Path.GetExtension file =? ".dll"
185+
&& (Path.GetFileNameWithoutExtension file <>? "FSharp.Core"
186+
|| (tryCheckFsCore file |> Option.isSome)))
188187
|> Seq.toList
189188
|> isAssembly asm)
190189

@@ -279,10 +278,9 @@ module internal CompilerServiceExtensions =
279278

280279
let findReferences libDir =
281280
Directory.EnumerateFiles(libDir, "*.dll")
282-
|> Seq.map Path.GetFullPath
283281
// Filter files already referenced directly
284282
|> Seq.filter (fun file ->
285-
let fileName = Path.GetFileName file
283+
let fileName = Path.GetFullPath file |> Path.GetFileName
286284

287285
dllFiles
288286
|> List.exists (fun (dllFile: string) -> Path.GetFileName dllFile =? fileName)

src/fsdocs-tool/BuildCommand.fs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,12 @@ type internal DocContent
9595
(subFolderFullPath = rootOutputFolderFullPath)
9696

9797
let allCultures =
98-
System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
99-
|> Array.map (fun x -> x.TwoLetterISOLanguageName)
100-
|> Array.filter (fun x -> x.Length = 2)
98+
CultureInfo.GetCultures(CultureTypes.AllCultures)
99+
|> Array.choose (fun x ->
100+
if x.TwoLetterISOLanguageName.Length <> 2 then
101+
None
102+
else
103+
Some x.TwoLetterISOLanguageName)
101104
|> Array.distinct
102105

103106
let makeMarkdownLinkResolver

0 commit comments

Comments
 (0)