Skip to content

Implemented functions for FlatList module to correspond to Array module #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "shell",
"args": [
"build",
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<Description>F# bindings for System.Collections.Immutable</Description>
<Copyright>Copyright © XperiAndri 2016</Copyright>
<AssemblyTitle>FSharp.Collections.Immutable</AssemblyTitle>
<Company>XperiAndri</Company>
<ProductName>FSharp.Collections.Immutable</ProductName>
<Version>2.0.0</Version>
<Authors>XperiAndri;EventHelix;vilinski;anthony-mi;dim-37</Authors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageId>FSharp.Collections.Immutable</PackageId>
<PackageTags>System;Immutable;Collections;FSharp;F#</PackageTags>
<RepositoryType>git</RepositoryType>
<DebugType>embedded</DebugType>
<RepositoryUrl>https://github.com/fsprojects/FSharp.Collections.Immutable/</RepositoryUrl>
<PackageProjectUrl>https://github.com/fsprojects/FSharp.Collections.Immutable/</PackageProjectUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
</PropertyGroup>

<ItemGroup>
<Compile Include="immutable-collection-util.fs" />
<Compile Include="flat-list.fs" />
<Compile Include="stack.fs" />
<Compile Include="immutable-list.fs" />
<Compile Include="queue.fs" />
<Compile Include="indexed-seq.fs" />
<Compile Include="maps.fs" />
<Compile Include="sets.fs" />
<Compile Include="seq.fs" />
</ItemGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<Description>F# bindings for System.Collections.Immutable</Description>
<Copyright>Copyright © XperiAndri 2016</Copyright>
<AssemblyTitle>FSharp.Collections.Immutable</AssemblyTitle>
<Company>XperiAndri</Company>
<ProductName>FSharp.Collections.Immutable</ProductName>
<Version>2.0.0</Version>
<Authors>XperiAndri;EventHelix;vilinski;anthony-mi;dim-37</Authors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageId>FSharp.Collections.Immutable</PackageId>
<PackageTags>System;Immutable;Collections;FSharp;F#</PackageTags>
<RepositoryType>git</RepositoryType>
<DebugType>embedded</DebugType>
<RepositoryUrl>https://github.com/fsprojects/FSharp.Collections.Immutable/</RepositoryUrl>
<PackageProjectUrl>https://github.com/fsprojects/FSharp.Collections.Immutable/</PackageProjectUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
</PropertyGroup>
<ItemGroup>
<Compile Include="functional-utils.fs" />
<Compile Include="immutable-collection-util.fs" />
<Compile Include="flat-list.fs" />
<Compile Include="stack.fs" />
<Compile Include="immutable-list.fs" />
<Compile Include="queue.fs" />
<Compile Include="indexed-seq.fs" />
<Compile Include="maps.fs" />
<Compile Include="sets.fs" />
<Compile Include="seq.fs" />
</ItemGroup>
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
</Project>
136 changes: 126 additions & 10 deletions src/FSharp.Collections.Immutable/flat-list.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ module FlatList =
let inline internal checkNotDefault argName (list : FlatList<'T>) =
if list.IsDefault then invalidArg argName "Uninstantiated ImmutableArray/FlatList"
let inline internal check (list : FlatList<'T>) = checkNotDefault (nameof list) list
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/dotnet/runtime/blob/54c717a4ed822f46a23893479b8d4398596c041d/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs#L409

Получается самый быстрый вариант – это дёрнуть IsEmpty и выбросить
https://github.com/dotnet/runtime/blob/54c717a4ed822f46a23893479b8d4398596c041d/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs#L157

И получится то же, что они делают. Тогда не нужно использовать их LINQ обёртку

let inline internal checkEmpty (list : FlatList<_>) = check list; if list.Length = 0 then invalidArg (nameof list) "Source is empty" else ()
let inline internal raiseOrReturn list = check list; list
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут наверное надо переименовать будет на checkAndReturn чтобы было в едином стиле


////////// Creating //////////

let inline empty<'T> : FlatList<_> = FlatListFactory.Create<'T>()
let inline singleton<'T> (item : 'T) : FlatList<'T> = FlatListFactory.Create<'T> (item)
let copy (list:FlatList<_>) = FlatListFactory.CreateRange list

let inline ofSeq source = FlatListFactory.CreateRange source
let inline ofArray (source : _ array) = FlatListFactory.CreateRange source
let inline ofList (source: _ list) = FlatListFactory.CreateRange source

let inline toSeq (flatList: FlatList<_>) = flatList :> seq<_>
let inline toArray (list : FlatList<_>) = check list; Seq.toArray list
let inline toList list = check list; Seq.toList list

////////// Building //////////

Expand Down Expand Up @@ -131,6 +136,8 @@ module FlatList =
let sortWith comparer list = sortWithComparer (ComparisonIdentity.FromFunction comparer) list
let sort list = check list; list.Sort()

let get (list:FlatList<_>) index = list.[index]

////////// Loop-based //////////

let inline private builderWithLengthOf list = builderWith <| length list
Expand All @@ -142,6 +149,8 @@ module FlatList =
builder.Add <| initializer i
moveFromBuilder builder

let zeroCreate<'a> count = init count (fun _ -> Unchecked.defaultof<'a>)

let rec private concatAddLengths (arrs: FlatList<FlatList<_>>) i acc =
if i >= length arrs then acc
else concatAddLengths arrs (i+1) (acc + arrs.[i].Length)
Expand Down Expand Up @@ -198,17 +207,16 @@ module FlatList =
for i = 0 to len - 1 do
f.Invoke(list1.[i], list2.[i])

let distinctBy projection (list: FlatList<'T>) =
let builder: FlatList<'T>.Builder = builderWith <| length list
let set = System.Collections.Generic.HashSet<'Key>(HashIdentity.Structural)
let mutable outputIndex = 0
let distinct (list: FlatList<'T>) = list |> System.Collections.Generic.HashSet |> ofSeq

let distinctBy projection (list:FlatList<'a>) =
let builder = builderWithLengthOf list
let set = System.Collections.Generic.HashSet<'key>(HashIdentity.Structural)

for i = 0 to length list - 1 do
let item = list.[i]
for item in list do
if set.Add <| projection item then
outputIndex <- outputIndex + 1
Builder.add item builder

builder.Add item

ofBuilder builder

let map2 mapping list1 list2 =
Expand Down Expand Up @@ -400,20 +408,93 @@ module FlatList =
if predicate list.[i] then Some list.[i] else loop (i+1)
loop <| length list - 1

let findIndex predicate = raiseOrReturn >> Seq.findIndex predicate

let findIndexBack predicate list =
check list
let rec loop i =
if i < 0 then indexNotFound() else
if predicate list.[i] then i else loop (i - 1)
loop <| length list - 1

let tryFindIndex predicate = raiseOrReturn >> Seq.tryFindIndex predicate

let tryFindIndexBack predicate list =
check list
let rec loop i =
if i < 0 then None else
if predicate list.[i] then Some i else loop (i - 1)
loop <| length list - 1
// TODO: windowed

let fold folder (state: 'state) = raiseOrReturn >> Seq.fold folder state

let scan folder (state: 'state) = raiseOrReturn >> Seq.scan folder state >> ofSeq

let fold2 folder (state: 'state) (left:FlatList<'a>) (right:FlatList<'b>) =
check left; check right
Seq.fold2 folder state left right

let foldBack2 folder (left:FlatList<'a>) (right:FlatList<'b>) (state:'state) =
check left; check right
Seq.foldBack2 folder left right state

let foldBack folder (list:FlatList<'a>) (state: 'state) =
check list
Seq.foldBack folder list state

let scanBack folder (list:FlatList<'a>) (state:'state) =
check list
Seq.scanBack folder list state |> ofSeq

let unfold (generator: 'state -> ('a * 'state) option) state =
Seq.unfold generator state |> ofSeq

let reduce reduction = raiseOrReturn >> Seq.reduce reduction

let reduceBack reduction = raiseOrReturn >> Seq.reduceBack reduction

let mapFold mapping (state:'State) (list:FlatList<'T>) =
check list
let (items, s) = Seq.mapFold mapping state list
ofSeq items, s

let mapFoldBack mapping (list:FlatList<'T>) (state:'State) =
check list
let (i, s) = Seq.mapFoldBack mapping list state
ofSeq i, s

let zip (left:FlatList<_>) (right:FlatList<_>) =
check left; check right
Seq.zip left right |> ofSeq

let zip3 (left:FlatList<_>) (middle:FlatList<_>) (right:FlatList<_>) =
check left; check middle; check right
Seq.zip3 left middle right |> ofSeq

let unzip list =
let left = builderWithLengthOf list
let right = builderWithLengthOf list
for item in list do
left.Add <| fst item
right.Add <| snd item
left, right

let unzip3 list =
let left = builderWithLengthOf list
let right = builderWithLengthOf list
let middle = builderWithLengthOf list
for item in list do
left.Add <| fst3 item
middle.Add <| snd3 item
right.Add <| thd3 item
left, middle, right

let windowed windowSize = raiseOrReturn >> Seq.windowed windowSize >> Seq.map ofSeq >> ofSeq

let fill target targetIndex count value =
indexed target
|> map (fun (i, a) -> if targetIndex <= i && i < targetIndex + count then value else a)
|> ofSeq

////////// Based on other operations //////////

Expand Down Expand Up @@ -469,6 +550,41 @@ module FlatList =
f builder
moveFromBuilder builder

let inline sum ( list:FlatList< ^T > when ^T : (static member (+) : ^T * ^T -> ^T) and ^T : (static member Zero : ^T) ) =
list |> raiseOrReturn |> reduce (+)

let inline sumBy projection ( list:FlatList< ^T > when ^T : (static member (+) : ^T * ^T -> ^T) and ^T : (static member Zero : ^T) ) =
list |> raiseOrReturn |> map projection |> reduce (+)

let inline average ( list:FlatList< ^T > when ^T : (static member (+) : ^T * ^T -> ^T) and ^T : (static member DivideByInt : ^T*int -> ^T) and ^T : (static member Zero : ^T) ) =
list |> raiseOrReturn |> applyOverFuncs LanguagePrimitives.DivideByInt sum length

let inline averageBy projection ( list:FlatList< ^T > when ^T : (static member (+) : ^T * ^T -> ^T) and ^T : (static member DivideByInt : ^T*int -> ^T) and ^T : (static member Zero : ^T) ) =
list |> raiseOrReturn |> applyOverFuncs LanguagePrimitives.DivideByInt (map projection >> sum) length

let maxBy projection (list:FlatList<'a> when 'a : comparison) = list |> raiseOrReturn |> map projection |> reduce max
let minBy projection (list:FlatList<'a> when 'a : comparison) = list |> raiseOrReturn |> map projection |> reduce min
let max (list:FlatList<'a> when 'a : comparison) = list |> raiseOrReturn |> reduce max
let min (list:FlatList<'a> when 'a : comparison) = list |> raiseOrReturn |> reduce min

let sortBy projection = sortWith (applyOverArgs LanguagePrimitives.GenericComparison projection)
let sortDescending (list:FlatList<'a>) = sortWith (flip LanguagePrimitives.GenericComparison) list
let sortByDescending projection = sortWith (flip (applyOverArgs LanguagePrimitives.GenericComparison projection))

let compareWith comparer (left:FlatList<'a>) (right:FlatList<'b>) = zip left right |> skipWhile ((uncurry comparer) >> ((=) 0)) |> head |> (uncurry comparer)

let tryExactlyOne = Seq.tryExactlyOne
let exactlyOne = Seq.exactlyOne

let rev = Seq.rev >> ofSeq
let transpose = Seq.transpose >> Seq.map ofSeq >> ofSeq
let permute indexMap = Seq.permute indexMap >> ofSeq
let pairwise = Seq.pairwise >> ofSeq
let except itemsToExclude = Seq.except itemsToExclude >> ofSeq
let splitInto count = Seq.splitInto count >> Seq.map ofSeq >> ofSeq
let chunkBySize chunkSize = Seq.chunkBySize chunkSize >> Seq.map ofSeq >> ofSeq
let allPairs left = Seq.allPairs left >> ofSeq

//////////

module ImmutableArray = FlatList
17 changes: 17 additions & 0 deletions src/FSharp.Collections.Immutable/functional-utils.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#if INTERACTIVE
namespace global
#else
namespace FSharp.Collections.Immutable
#endif

[<AutoOpen>]
module internal FunctionalUtils =
let inline flip f a b = f b a
let inline uncurry f (a, b) = f a b

let inline applyOverFuncs f g h x = f (g x) (h x)
let inline applyOverArgs f g x y = f (g x) (g y)

let inline fst3 (a, _, _) = a
let inline snd3 (_, a, _) = a
let inline thd3 (_, _, a) = a