Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit45d622c

Browse files
PatrickMcDonaldlatkin
authored andcommitted
Implement mapFold and mapFoldBack for Array, List, Seq
commit 5078aaa77b5e5842ab23baed73de42b74d51923eAuthor: latkin <latkin@microsoft.com>Date: Thu Oct 30 16:57:44 2014 -0700 Update tests to verify direction of iterationcommit a83b0b2facdb16b4fe7f1d606c2db1e032bbd188Merge: a1c4a1a eaf92b4Author: latkin <latkin@microsoft.com>Date: Thu Oct 30 16:33:52 2014 -0700 Merge branch 'mapFold' ofhttps://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.fscommit eaf92b4997f8e333e35130b6117aa6d78b9f05afAuthor: Patrick McDonald <paddymcdonald@gmail.com>Date: Tue Sep 16 15:04:35 2014 +0100 Updates to mapFold and mapFoldBack after code reviewcommit bce0f1e696b6b1dc1c6d731dc1cbf6e83675f4a0Author: Patrick McDonald <paddymcdonald@gmail.com>Date: Mon Sep 8 00:02:17 2014 +0100 Implement mapFold and mapFoldBack
1 parent9ceff4c commit45d622c

File tree

13 files changed

+322
-1
lines changed

13 files changed

+322
-1
lines changed

‎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+
memberthis.MapFold()=
153+
// integer array
154+
letfuncInt acc x=if x%2=0then10*x, acc+1else x, acc
155+
letresultInt,resultIntAcc= Array.mapFold funcInt100[|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+
letfuncStr acc(x:string)=match x.Lengthwith0->"empty", acc|_-> x.ToLower(), sprintf"%s%s" acc x
161+
letresultStr,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+
letresultEpt,resultEptAcc= Array.mapFold funcInt100[||]
167+
if resultEpt<>[||]then Assert.Fail()
168+
Assert.AreEqual(100, resultEptAcc)
169+
170+
// null array
171+
letnullArr=null:string[]
172+
CheckThrowsArgumentNullException(fun()-> Array.mapFold funcStr"" nullArr|> ignore)
173+
174+
()
175+
176+
[<Test>]
177+
memberthis.MapFoldBack()=
178+
// integer array
179+
letfuncInt x acc=if acc<105then10*x, acc+2else x, acc
180+
letresultInt,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+
letfuncStr(x:string)acc=match x.Lengthwith0->"empty", acc|_-> x.ToLower(), sprintf"%s%s" acc x
186+
letresultStr,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+
letresultEpt,resultEptAcc= Array.mapFoldBack funcInt[||]100
192+
if resultEpt<>[||]then Assert.Fail()
193+
Assert.AreEqual(100, resultEptAcc)
194+
195+
// null array
196+
letnullArr=null:string[]
197+
CheckThrowsArgumentNullException(fun()-> Array.mapFoldBack funcStr nullArr""|> ignore)
198+
199+
()
200+
151201
[<Test>]
152202
memberthis.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+
memberthis.MapFold()=
198+
// integer List
199+
letfuncInt acc x=if x%2=0then10*x, acc+1else x, acc
200+
letresultInt,resultIntAcc= List.mapFold funcInt100[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+
letfuncInt acc x=if x%2=0then10*x, acc+1else x, acc
206+
letresultInt,resultIntAcc= List.mapFold funcInt100[2]
207+
Assert.AreEqual([20], resultInt)
208+
Assert.AreEqual(101, resultIntAcc)
209+
210+
// string List
211+
letfuncStr acc(x:string)=match x.Lengthwith0->"empty", acc|_-> x.ToLower(), sprintf"%s%s" acc x
212+
letresultStr,resultStrAcc= List.mapFold funcStr""["";"BB";"C";""]
213+
Assert.AreEqual(["empty";"bb";"c";"empty"], resultStr)
214+
Assert.AreEqual("BBC", resultStrAcc)
215+
216+
// empty List
217+
letresultEpt,resultEptAcc= List.mapFold funcInt100[]
218+
Assert.AreEqual([], resultEpt)
219+
Assert.AreEqual(100, resultEptAcc)
220+
221+
()
222+
223+
[<Test>]
224+
memberthis.MapFoldBack()=
225+
// integer List
226+
letfuncInt x acc=if acc<105then10*x, acc+2else x, acc
227+
letresultInt,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+
letresultInt,resultIntAcc= List.mapFoldBack funcInt[1]100
233+
Assert.AreEqual([10], resultInt)
234+
Assert.AreEqual(102, resultIntAcc)
235+
236+
// string List
237+
letfuncStr(x:string)acc=match x.Lengthwith0->"empty", acc|_-> x.ToLower(), sprintf"%s%s" acc x
238+
letresultStr,resultStrAcc= List.mapFoldBack funcStr["";"BB";"C";""]""
239+
Assert.AreEqual(["empty";"bb";"c";"empty"], resultStr)
240+
Assert.AreEqual("CBB", resultStrAcc)
241+
242+
// empty List
243+
letresultEpt,resultEptAcc= List.mapFoldBack funcInt[]100
244+
Assert.AreEqual([], resultEpt)
245+
Assert.AreEqual(100, resultEptAcc)
246+
247+
()
248+
196249
[<Test>]
197250
memberthis.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
memberthis.Map3()=
436435
// Integer seq
@@ -463,6 +462,55 @@ type SeqModule2() =
463462

464463
()
465464

465+
[<Test>]
466+
memberthis.MapFold()=
467+
// integer Seq
468+
letfuncInt acc x=if x%2=0then10*x, acc+1else x, acc
469+
letresultInt,resultIntAcc= Seq.mapFold funcInt100<|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+
letfuncStr acc(x:string)=match x.Lengthwith0->"empty", acc|_-> x.ToLower(), sprintf"%s%s" acc x
475+
letresultStr,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+
letresultEpt,resultEptAcc= Seq.mapFold funcInt100 Seq.empty
481+
VerifySeqsEqual Seq.empty resultEpt
482+
Assert.AreEqual(100, resultEptAcc)
483+
484+
// null Seq
485+
letnullArr=null:seq<string>
486+
CheckThrowsArgumentNullException(fun()-> Seq.mapFold funcStr"" nullArr|> ignore)
487+
488+
()
489+
490+
[<Test>]
491+
memberthis.MapFoldBack()=
492+
// integer Seq
493+
letfuncInt x acc=if acc<105then10*x, acc+2else x, acc
494+
letresultInt,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+
letfuncStr(x:string)acc=match x.Lengthwith0->"empty", acc|_-> x.ToLower(), sprintf"%s%s" acc x
500+
letresultStr,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+
letresultEpt,resultEptAcc= Seq.mapFoldBack funcInt Seq.empty100
506+
VerifySeqsEqual Seq.empty resultEpt
507+
Assert.AreEqual(100, resultEptAcc)
508+
509+
// null Seq
510+
letnullArr=null:seq<string>
511+
CheckThrowsArgumentNullException(fun()-> Seq.mapFoldBack funcStr nullArr""|> ignore)
512+
513+
()
466514

467515
memberprivatethis.MapWithSideEffectsTester(map:(int-> int)->seq<int>->seq<int>)expectExceptions=
468516
leti= ref0

‎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+
letmapFold<'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+
letmapFoldBack<'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
letexists(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
letindexed list= Microsoft.FSharp.Primitives.Basics.List.indexed list
6969

70+
[<CompiledName("MapFold")>]
71+
letmapFold<'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+
letmapFoldBack<'T,'State,'Result>(f:'T->'State->'Result* 'State)list acc=
76+
match listwith
77+
|[]->[], acc
78+
|[h]->leth',s'= f h accin[h'], s'
79+
|_->
80+
letf= OptimizedClosures.FSharpFunc<_,_,_>.Adapt(f)
81+
let recloop res list=
82+
match list, reswith
83+
|[],_-> res
84+
| h::t,(list', acc')->
85+
leth',s'= f.Invoke(h,acc')
86+
loop(h'::list', s') t
87+
loop([], acc)(rev list)
88+
7089
[<CompiledName("Iterate")>]
7190
letiter f list= Microsoft.FSharp.Primitives.Basics.List.iter f list
7291

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp