Skip to content

Commit 45d622c

Browse files
PatrickMcDonaldlatkin
authored andcommitted
Implement mapFold and mapFoldBack for Array, List, Seq
commit 5078aaa77b5e5842ab23baed73de42b74d51923e Author: latkin <latkin@microsoft.com> Date: Thu Oct 30 16:57:44 2014 -0700 Update tests to verify direction of iteration commit a83b0b2facdb16b4fe7f1d606c2db1e032bbd188 Merge: a1c4a1a eaf92b4 Author: latkin <latkin@microsoft.com> Date: Thu Oct 30 16:33:52 2014 -0700 Merge branch 'mapFold' of https://git01.codeplex.com/forks/patrickmcdonald/visualfsharp into PR Conflicts: src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule2.fs src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ListModule2.fs src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SeqModule2.fs src/fsharp/FSharp.Core.Unittests/SurfaceArea.4.0.fs src/fsharp/FSharp.Core.Unittests/SurfaceArea.Portable.fs src/fsharp/FSharp.Core/list.fs src/fsharp/FSharp.Core/local.fs src/fsharp/FSharp.Core/local.fsi src/fsharp/FSharp.Core/seq.fs commit eaf92b4997f8e333e35130b6117aa6d78b9f05af Author: Patrick McDonald <paddymcdonald@gmail.com> Date: Tue Sep 16 15:04:35 2014 +0100 Updates to mapFold and mapFoldBack after code review commit bce0f1e696b6b1dc1c6d731dc1cbf6e83675f4a0 Author: Patrick McDonald <paddymcdonald@gmail.com> Date: Mon Sep 8 00:02:17 2014 +0100 Implement mapFold and mapFoldBack
1 parent 9ceff4c commit 45d622c

13 files changed

Lines changed: 322 additions & 1 deletion

File tree

src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ArrayModule2.fs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,56 @@ type ArrayModule2() =
148148

149149
()
150150

151+
[<Test>]
152+
member this.MapFold() =
153+
// integer array
154+
let funcInt acc x = if x % 2 = 0 then 10*x, acc + 1 else x, acc
155+
let resultInt,resultIntAcc = Array.mapFold funcInt 100 [| 1..10 |]
156+
if resultInt <> [| 1;20;3;40;5;60;7;80;9;100 |] then Assert.Fail()
157+
Assert.AreEqual(105, resultIntAcc)
158+
159+
// string array
160+
let funcStr acc (x:string) = match x.Length with 0 -> "empty", acc | _ -> x.ToLower(), sprintf "%s%s" acc x
161+
let resultStr,resultStrAcc = Array.mapFold funcStr "" [| "";"BB";"C";"" |]
162+
if resultStr <> [| "empty";"bb";"c";"empty" |] then Assert.Fail()
163+
Assert.AreEqual("BBC", resultStrAcc)
164+
165+
// empty array
166+
let resultEpt,resultEptAcc = Array.mapFold funcInt 100 [| |]
167+
if resultEpt <> [| |] then Assert.Fail()
168+
Assert.AreEqual(100, resultEptAcc)
169+
170+
// null array
171+
let nullArr = null:string[]
172+
CheckThrowsArgumentNullException (fun () -> Array.mapFold funcStr "" nullArr |> ignore)
173+
174+
()
175+
176+
[<Test>]
177+
member this.MapFoldBack() =
178+
// integer array
179+
let funcInt x acc = if acc < 105 then 10*x, acc + 2 else x, acc
180+
let resultInt,resultIntAcc = Array.mapFoldBack funcInt [| 1..10 |] 100
181+
if resultInt <> [| 1;2;3;4;5;6;7;80;90;100 |] then Assert.Fail()
182+
Assert.AreEqual(106, resultIntAcc)
183+
184+
// string array
185+
let funcStr (x:string) acc = match x.Length with 0 -> "empty", acc | _ -> x.ToLower(), sprintf "%s%s" acc x
186+
let resultStr,resultStrAcc = Array.mapFoldBack funcStr [| "";"BB";"C";"" |] ""
187+
if resultStr <> [| "empty";"bb";"c";"empty" |] then Assert.Fail()
188+
Assert.AreEqual("CBB", resultStrAcc)
189+
190+
// empty array
191+
let resultEpt,resultEptAcc = Array.mapFoldBack funcInt [| |] 100
192+
if resultEpt <> [| |] then Assert.Fail()
193+
Assert.AreEqual(100, resultEptAcc)
194+
195+
// null array
196+
let nullArr = null:string[]
197+
CheckThrowsArgumentNullException (fun () -> Array.mapFoldBack funcStr nullArr "" |> ignore)
198+
199+
()
200+
151201
[<Test>]
152202
member this.Mapi() =
153203
// integer array

src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/ListModule2.fs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,59 @@ type ListModule02() =
193193

194194
()
195195

196+
[<Test>]
197+
member this.MapFold() =
198+
// integer List
199+
let funcInt acc x = if x % 2 = 0 then 10*x, acc + 1 else x, acc
200+
let resultInt,resultIntAcc = List.mapFold funcInt 100 [ 1..10 ]
201+
Assert.AreEqual([1;20;3;40;5;60;7;80;9;100], resultInt)
202+
Assert.AreEqual(105, resultIntAcc)
203+
204+
// integer List single item
205+
let funcInt acc x = if x % 2 = 0 then 10*x, acc + 1 else x, acc
206+
let resultInt,resultIntAcc = List.mapFold funcInt 100 [ 2 ]
207+
Assert.AreEqual([20], resultInt)
208+
Assert.AreEqual(101, resultIntAcc)
209+
210+
// string List
211+
let funcStr acc (x:string) = match x.Length with 0 -> "empty", acc | _ -> x.ToLower(), sprintf "%s%s" acc x
212+
let resultStr,resultStrAcc = List.mapFold funcStr "" ["";"BB";"C";""]
213+
Assert.AreEqual(["empty";"bb";"c";"empty"], resultStr)
214+
Assert.AreEqual("BBC", resultStrAcc)
215+
216+
// empty List
217+
let resultEpt,resultEptAcc = List.mapFold funcInt 100 []
218+
Assert.AreEqual([], resultEpt)
219+
Assert.AreEqual(100, resultEptAcc)
220+
221+
()
222+
223+
[<Test>]
224+
member this.MapFoldBack() =
225+
// integer List
226+
let funcInt x acc = if acc < 105 then 10*x, acc + 2 else x, acc
227+
let resultInt,resultIntAcc = List.mapFoldBack funcInt [ 1..10 ] 100
228+
Assert.AreEqual([1;2;3;4;5;6;7;80;90;100], resultInt)
229+
Assert.AreEqual(106, resultIntAcc)
230+
231+
// integer List single item
232+
let resultInt,resultIntAcc = List.mapFoldBack funcInt [1] 100
233+
Assert.AreEqual([10], resultInt)
234+
Assert.AreEqual(102, resultIntAcc)
235+
236+
// string List
237+
let funcStr (x:string) acc = match x.Length with 0 -> "empty", acc | _ -> x.ToLower(), sprintf "%s%s" acc x
238+
let resultStr,resultStrAcc = List.mapFoldBack funcStr ["";"BB";"C";""] ""
239+
Assert.AreEqual(["empty";"bb";"c";"empty"], resultStr)
240+
Assert.AreEqual("CBB", resultStrAcc)
241+
242+
// empty List
243+
let resultEpt,resultEptAcc = List.mapFoldBack funcInt [] 100
244+
Assert.AreEqual([], resultEpt)
245+
Assert.AreEqual(100, resultEptAcc)
246+
247+
()
248+
196249
[<Test>]
197250
member this.Max() =
198251
// integer List

src/fsharp/FSharp.Core.Unittests/FSharp.Core/Microsoft.FSharp.Collections/SeqModule2.fs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,6 @@ type SeqModule2() =
430430

431431
()
432432

433-
434433
[<Test>]
435434
member this.Map3() =
436435
// Integer seq
@@ -463,6 +462,55 @@ type SeqModule2() =
463462

464463
()
465464

465+
[<Test>]
466+
member this.MapFold() =
467+
// integer Seq
468+
let funcInt acc x = if x % 2 = 0 then 10*x, acc + 1 else x, acc
469+
let resultInt,resultIntAcc = Seq.mapFold funcInt 100 <| seq { 1..10 }
470+
VerifySeqsEqual (seq [ 1;20;3;40;5;60;7;80;9;100 ]) resultInt
471+
Assert.AreEqual(105, resultIntAcc)
472+
473+
// string Seq
474+
let funcStr acc (x:string) = match x.Length with 0 -> "empty", acc | _ -> x.ToLower(), sprintf "%s%s" acc x
475+
let resultStr,resultStrAcc = Seq.mapFold funcStr "" <| seq [ "";"BB";"C";"" ]
476+
VerifySeqsEqual (seq [ "empty";"bb";"c";"empty" ]) resultStr
477+
Assert.AreEqual("BBC", resultStrAcc)
478+
479+
// empty Seq
480+
let resultEpt,resultEptAcc = Seq.mapFold funcInt 100 Seq.empty
481+
VerifySeqsEqual Seq.empty resultEpt
482+
Assert.AreEqual(100, resultEptAcc)
483+
484+
// null Seq
485+
let nullArr = null:seq<string>
486+
CheckThrowsArgumentNullException (fun () -> Seq.mapFold funcStr "" nullArr |> ignore)
487+
488+
()
489+
490+
[<Test>]
491+
member this.MapFoldBack() =
492+
// integer Seq
493+
let funcInt x acc = if acc < 105 then 10*x, acc + 2 else x, acc
494+
let resultInt,resultIntAcc = Seq.mapFoldBack funcInt (seq { 1..10 }) 100
495+
VerifySeqsEqual (seq [ 1;2;3;4;5;6;7;80;90;100 ]) resultInt
496+
Assert.AreEqual(106, resultIntAcc)
497+
498+
// string Seq
499+
let funcStr (x:string) acc = match x.Length with 0 -> "empty", acc | _ -> x.ToLower(), sprintf "%s%s" acc x
500+
let resultStr,resultStrAcc = Seq.mapFoldBack funcStr (seq [ "";"BB";"C";"" ]) ""
501+
VerifySeqsEqual (seq [ "empty";"bb";"c";"empty" ]) resultStr
502+
Assert.AreEqual("CBB", resultStrAcc)
503+
504+
// empty Seq
505+
let resultEpt,resultEptAcc = Seq.mapFoldBack funcInt Seq.empty 100
506+
VerifySeqsEqual Seq.empty resultEpt
507+
Assert.AreEqual(100, resultEptAcc)
508+
509+
// null Seq
510+
let nullArr = null:seq<string>
511+
CheckThrowsArgumentNullException (fun () -> Seq.mapFoldBack funcStr nullArr "" |> ignore)
512+
513+
()
466514

467515
member private this.MapWithSideEffectsTester (map : (int -> int) -> seq<int> -> seq<int>) expectExceptions =
468516
let i = ref 0

src/fsharp/FSharp.Core.Unittests/SurfaceArea.4.0.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1,T2][] Zip[T1,T2](T1[
123123
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1[],T2[]] Unzip[T1,T2](System.Tuple`2[T1,T2][])
124124
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[TKey,System.Int32][] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
125125
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[TKey,T[]][] GroupBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
126+
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[TResult[],TState] MapFoldBack[T,TState,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,System.Tuple`2[TResult,TState]]], T[], TState)
127+
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[TResult[],TState] MapFold[T,TState,TResult](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Tuple`2[TResult,TState]]], TState, T[])
126128
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T[],T[]] Partition[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
127129
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T[],T[]] SplitAt[T](Int32, T[])
128130
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`3[T1,T2,T3][] Zip3[T1,T2,T3](T1[], T2[], T3[])
@@ -346,6 +348,8 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T]
346348
Microsoft.FSharp.Collections.ListModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](Microsoft.FSharp.Collections.FSharpList`1[T])
347349
Microsoft.FSharp.Collections.ListModule: System.String ToString()
348350
Microsoft.FSharp.Collections.ListModule: System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[T1],Microsoft.FSharp.Collections.FSharpList`1[T2]] Unzip[T1,T2](Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T1,T2]])
351+
Microsoft.FSharp.Collections.ListModule: System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[TResult],TState] MapFoldBack[T,TState,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,System.Tuple`2[TResult,TState]]], Microsoft.FSharp.Collections.FSharpList`1[T], TState)
352+
Microsoft.FSharp.Collections.ListModule: System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[TResult],TState] MapFold[T,TState,TResult](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Tuple`2[TResult,TState]]], TState, Microsoft.FSharp.Collections.FSharpList`1[T])
349353
Microsoft.FSharp.Collections.ListModule: System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[T],Microsoft.FSharp.Collections.FSharpList`1[T]] Partition[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
350354
Microsoft.FSharp.Collections.ListModule: System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[T],Microsoft.FSharp.Collections.FSharpList`1[T]] SplitAt[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
351355
Microsoft.FSharp.Collections.ListModule: System.Tuple`3[Microsoft.FSharp.Collections.FSharpList`1[T1],Microsoft.FSharp.Collections.FSharpList`1[T2],Microsoft.FSharp.Collections.FSharpList`1[T3]] Unzip3[T1,T2,T3](Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[T1,T2,T3]])
@@ -471,6 +475,8 @@ Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1
471475
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Unfold[TState,T](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpOption`1[System.Tuple`2[T,TState]]], TState)
472476
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], System.Collections.Generic.IEnumerable`1[T])
473477
Microsoft.FSharp.Collections.SeqModule: System.String ToString()
478+
Microsoft.FSharp.Collections.SeqModule: System.Tuple`2[System.Collections.Generic.IEnumerable`1[TResult],TState] MapFoldBack[T,TState,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,System.Tuple`2[TResult,TState]]], System.Collections.Generic.IEnumerable`1[T], TState)
479+
Microsoft.FSharp.Collections.SeqModule: System.Tuple`2[System.Collections.Generic.IEnumerable`1[TResult],TState] MapFold[T,TState,TResult](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Tuple`2[TResult,TState]]], TState, System.Collections.Generic.IEnumerable`1[T])
474480
Microsoft.FSharp.Collections.SeqModule: System.Type GetType()
475481
Microsoft.FSharp.Collections.SeqModule: T Average[T](System.Collections.Generic.IEnumerable`1[T])
476482
Microsoft.FSharp.Collections.SeqModule: T ExactlyOne[T](System.Collections.Generic.IEnumerable`1[T])

src/fsharp/FSharp.Core.Unittests/SurfaceArea.Portable.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1,T2][] Zip[T1,T2](T1[
117117
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1[],T2[]] Unzip[T1,T2](System.Tuple`2[T1,T2][])
118118
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[TKey,System.Int32][] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
119119
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[TKey,T[]][] GroupBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
120+
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[TResult[],TState] MapFoldBack[T,TState,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,System.Tuple`2[TResult,TState]]], T[], TState)
121+
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[TResult[],TState] MapFold[T,TState,TResult](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Tuple`2[TResult,TState]]], TState, T[])
120122
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T[],T[]] Partition[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
121123
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T[],T[]] SplitAt[T](Int32, T[])
122124
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`3[T1,T2,T3][] Zip3[T1,T2,T3](T1[], T2[], T3[])
@@ -340,6 +342,8 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[T]
340342
Microsoft.FSharp.Collections.ListModule: System.Collections.Generic.IEnumerable`1[T] ToSeq[T](Microsoft.FSharp.Collections.FSharpList`1[T])
341343
Microsoft.FSharp.Collections.ListModule: System.String ToString()
342344
Microsoft.FSharp.Collections.ListModule: System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[T1],Microsoft.FSharp.Collections.FSharpList`1[T2]] Unzip[T1,T2](Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T1,T2]])
345+
Microsoft.FSharp.Collections.ListModule: System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[TResult],TState] MapFoldBack[T,TState,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,System.Tuple`2[TResult,TState]]], Microsoft.FSharp.Collections.FSharpList`1[T], TState)
346+
Microsoft.FSharp.Collections.ListModule: System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[TResult],TState] MapFold[T,TState,TResult](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Tuple`2[TResult,TState]]], TState, Microsoft.FSharp.Collections.FSharpList`1[T])
343347
Microsoft.FSharp.Collections.ListModule: System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[T],Microsoft.FSharp.Collections.FSharpList`1[T]] Partition[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
344348
Microsoft.FSharp.Collections.ListModule: System.Tuple`2[Microsoft.FSharp.Collections.FSharpList`1[T],Microsoft.FSharp.Collections.FSharpList`1[T]] SplitAt[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
345349
Microsoft.FSharp.Collections.ListModule: System.Tuple`3[Microsoft.FSharp.Collections.FSharpList`1[T1],Microsoft.FSharp.Collections.FSharpList`1[T2],Microsoft.FSharp.Collections.FSharpList`1[T3]] Unzip3[T1,T2,T3](Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`3[T1,T2,T3]])
@@ -465,6 +469,8 @@ Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1
465469
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Unfold[TState,T](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpOption`1[System.Tuple`2[T,TState]]], TState)
466470
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[T] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], System.Collections.Generic.IEnumerable`1[T])
467471
Microsoft.FSharp.Collections.SeqModule: System.String ToString()
472+
Microsoft.FSharp.Collections.SeqModule: System.Tuple`2[System.Collections.Generic.IEnumerable`1[TResult],TState] MapFoldBack[T,TState,TResult](Microsoft.FSharp.Core.FSharpFunc`2[T,Microsoft.FSharp.Core.FSharpFunc`2[TState,System.Tuple`2[TResult,TState]]], System.Collections.Generic.IEnumerable`1[T], TState)
473+
Microsoft.FSharp.Collections.SeqModule: System.Tuple`2[System.Collections.Generic.IEnumerable`1[TResult],TState] MapFold[T,TState,TResult](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpFunc`2[T,System.Tuple`2[TResult,TState]]], TState, System.Collections.Generic.IEnumerable`1[T])
468474
Microsoft.FSharp.Collections.SeqModule: System.Type GetType()
469475
Microsoft.FSharp.Collections.SeqModule: T Average[T](System.Collections.Generic.IEnumerable`1[T])
470476
Microsoft.FSharp.Collections.SeqModule: T ExactlyOne[T](System.Collections.Generic.IEnumerable`1[T])

src/fsharp/FSharp.Core/array.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,16 @@ namespace Microsoft.FSharp.Collections
372372
res.[i] <- f.Invoke(i,array.[i])
373373
res
374374

375+
[<CompiledName("MapFold")>]
376+
let mapFold<'T,'State,'Result> (f : 'State -> 'T -> 'Result * 'State) acc array =
377+
checkNonNull "array" array
378+
Microsoft.FSharp.Primitives.Basics.Array.mapFold f acc array
379+
380+
[<CompiledName("MapFoldBack")>]
381+
let mapFoldBack<'T,'State,'Result> (f : 'T -> 'State -> 'Result * 'State) array acc =
382+
checkNonNull "array" array
383+
Microsoft.FSharp.Primitives.Basics.Array.mapFoldBack f array acc
384+
375385
[<CompiledName("Exists")>]
376386
let exists (f: 'T -> bool) (array:'T[]) =
377387
checkNonNull "array" array

src/fsharp/FSharp.Core/array.fsi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,26 @@ namespace Microsoft.FSharp.Collections
496496
[<CompiledName("Map2")>]
497497
val map2: mapping:('T1 -> 'T2 -> 'U) -> array1:'T1[] -> array2:'T2[] -> 'U[]
498498

499+
/// <summary>Combines map and fold. Builds a new array whose elements are the results of applying the given function
500+
/// to each of the elements of the input array. The function is also used to accumulate a final value.</summary>
501+
/// <param name="mapping">The function to transform elements from the input array and accumulate the final value.</param>
502+
/// <param name="state">The initial state.</param>
503+
/// <param name="array">The input array.</param>
504+
/// <exception cref="System.ArgumentNullException">Thrown when the input array is null.</exception>
505+
/// <returns>The array of transformed elements, and the final accumulated value.</returns>
506+
[<CompiledName("MapFold")>]
507+
val mapFold<'T,'State,'Result> : mapping:('State -> 'T -> 'Result * 'State) -> state:'State -> array:'T[] -> 'Result[] * 'State
508+
509+
/// <summary>Combines map and foldBack. Builds a new array whose elements are the results of applying the given function
510+
/// to each of the elements of the input array. The function is also used to accumulate a final value.</summary>
511+
/// <param name="mapping">The function to transform elements from the input array and accumulate the final value.</param>
512+
/// <param name="array">The input array.</param>
513+
/// <param name="state">The initial state.</param>
514+
/// <exception cref="System.ArgumentNullException">Thrown when the input array is null.</exception>
515+
/// <returns>The array of transformed elements, and the final accumulated value.</returns>
516+
[<CompiledName("MapFoldBack")>]
517+
val mapFoldBack<'T,'State,'Result> : mapping:('T -> 'State -> 'Result * 'State) -> array:'T[] -> state:'State -> 'Result[] * 'State
518+
499519
/// <summary>Builds a new collection whose elements are the results of applying the given function
500520
/// to the corresponding triples from the three collections. The three input
501521
/// arrays must have the same length, otherwise an <c>ArgumentException</c> is

src/fsharp/FSharp.Core/list.fs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ namespace Microsoft.FSharp.Collections
6767
[<CompiledName("Indexed")>]
6868
let indexed list = Microsoft.FSharp.Primitives.Basics.List.indexed list
6969

70+
[<CompiledName("MapFold")>]
71+
let mapFold<'T,'State,'Result> (f:'State -> 'T -> 'Result * 'State) acc list =
72+
Microsoft.FSharp.Primitives.Basics.List.mapFold f acc list
73+
74+
[<CompiledName("MapFoldBack")>]
75+
let mapFoldBack<'T,'State,'Result> (f:'T -> 'State -> 'Result * 'State) list acc =
76+
match list with
77+
| [] -> [], acc
78+
| [h] -> let h',s' = f h acc in [h'], s'
79+
| _ ->
80+
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
81+
let rec loop res list =
82+
match list, res with
83+
| [], _ -> res
84+
| h::t, (list', acc') ->
85+
let h',s' = f.Invoke(h,acc')
86+
loop (h'::list', s') t
87+
loop ([], acc) (rev list)
88+
7089
[<CompiledName("Iterate")>]
7190
let iter f list = Microsoft.FSharp.Primitives.Basics.List.iter f list
7291

0 commit comments

Comments
 (0)