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

Commit1d6fff2

Browse files
PatrickMcDonaldlatkin
authored andcommitted
Implement Array.unfold, List.unfold
commit 19e2c16c4c554c17e4ba730f6f0552091502be48Merge:5f20697 222f5adAuthor: latkin <latkin@microsoft.com>Date: Tue Nov 4 14:32:01 2014 -0800 Merge branch 'unfold' 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/SurfaceArea.4.0.fs src/fsharp/FSharp.Core.Unittests/SurfaceArea.Portable.fs src/fsharp/FSharp.Core/array.fsi src/fsharp/FSharp.Core/list.fs src/fsharp/FSharp.Core/list.fsi src/fsharp/FSharp.Core/local.fscommit 222f5adfefdc379880266b3522c5268a457e5dc3Author: Patrick McDonald <paddymcdonald@gmail.com>Date: Tue Sep 9 23:11:27 2014 +0100 Refactor Array.unfoldcommit 3f5db8bb97fc488bbb58036c427d88bf532b16f6Author: Patrick McDonald <paddymcdonald@gmail.com>Date: Tue Sep 9 18:43:00 2014 +0100 Implement List.unfold and Array.unfold
1 parent5f20697 commit1d6fff2

File tree

10 files changed

+88
-1
lines changed

10 files changed

+88
-1
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,22 @@ type ArrayModule2() =
948948

949949
()
950950

951+
[<Test>]
952+
memberthis.Unfold()=
953+
// integer Seq
954+
letresultInt= Array.unfold(fun x->if x<20then Some(x+1,x*2)else None)1
955+
Assert.AreEqual([|2;3;5;9;17|], resultInt)
956+
957+
// string Seq
958+
letresultStr= Array.unfold(fun(x:string)->if x.Contains("unfold")then Some("a","b")else None)"unfold"
959+
Assert.AreEqual([|"a"|], resultStr)
960+
961+
// empty seq
962+
letresultEpt= Array.unfold(fun _-> None)1
963+
Assert.AreEqual([||], resultEpt)
964+
965+
()
966+
951967
[<Test>]
952968
memberthis.Unzip()=
953969
// integer array

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,23 @@ type ListModule02() =
813813

814814
()
815815

816+
[<Test>]
817+
memberthis.Unfold()=
818+
// integer Seq
819+
letresultInt= List.unfold(fun x->if x<20then Some(x+1,x*2)else None)1
820+
Assert.AreEqual([2;3;5;9;17], resultInt)
821+
822+
// string Seq
823+
letresultStr= List.unfold(fun(x:string)->if x.Contains("unfold")then Some("a","b")else None)"unfold"
824+
Assert.AreEqual(["a"], resultStr)
825+
826+
// empty seq
827+
//let resultEpt = List.unfold (fun _ -> Option<string>.None) 1
828+
letresultEpt= List.unfold(fun _-> None)1
829+
Assert.AreEqual([], resultEpt)
830+
831+
()
832+
816833
[<Test>]
817834
memberthis.Unzip()=
818835
// integer List

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] Tail[T](T[])
186186
Microsoft.FSharp.Collections.ArrayModule: T[] TakeWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
187187
Microsoft.FSharp.Collections.ArrayModule: T[] Take[T](Int32, T[])
188188
Microsoft.FSharp.Collections.ArrayModule: T[] Truncate[T](Int32, T[])
189+
Microsoft.FSharp.Collections.ArrayModule: T[] Unfold[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpOption`1[System.Tuple`2[T,TState]]], TState)
189190
Microsoft.FSharp.Collections.ArrayModule: T[] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
190191
Microsoft.FSharp.Collections.ArrayModule: T[] ZeroCreate[T](Int32)
191192
Microsoft.FSharp.Collections.ArrayModule: Void CopyTo[T](T[], Int32, T[], Int32, Int32)
@@ -338,6 +339,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
338339
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] TakeWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
339340
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Take[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
340341
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Truncate[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
342+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Unfold[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpOption`1[System.Tuple`2[T,TState]]], TState)
341343
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
342344
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[System.Int32] TryFindIndexBack[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
343345
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[System.Int32] TryFindIndex[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ Microsoft.FSharp.Collections.ArrayModule: T[] TakeWhile[T](Microsoft.FSharp.Core
180180
Microsoft.FSharp.Collections.ArrayModule: T[] Tail[T](T[])
181181
Microsoft.FSharp.Collections.ArrayModule: T[] Take[T](Int32, T[])
182182
Microsoft.FSharp.Collections.ArrayModule: T[] Truncate[T](Int32, T[])
183+
Microsoft.FSharp.Collections.ArrayModule: T[] Unfold[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpOption`1[System.Tuple`2[T,TState]]], TState)
183184
Microsoft.FSharp.Collections.ArrayModule: T[] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], T[])
184185
Microsoft.FSharp.Collections.ArrayModule: T[] ZeroCreate[T](Int32)
185186
Microsoft.FSharp.Collections.ArrayModule: Void CopyTo[T](T[], Int32, T[], Int32, Int32)
@@ -332,6 +333,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
332333
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] TakeWhile[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
333334
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Take[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
334335
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Truncate[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
336+
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Unfold[T,TState](Microsoft.FSharp.Core.FSharpFunc`2[TState,Microsoft.FSharp.Core.FSharpOption`1[System.Tuple`2[T,TState]]], TState)
335337
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[T] Where[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
336338
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[System.Int32] TryFindIndexBack[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])
337339
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Core.FSharpOption`1[System.Int32] TryFindIndex[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Collections.FSharpList`1[T])

‎src/fsharp/FSharp.Core/array.fs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,18 @@ namespace Microsoft.FSharp.Collections
593593
res.[i]<-(array1.[i],array2.[i],array3.[i])
594594
res
595595

596+
[<CompiledName("Unfold")>]
597+
letunfold<'T,'State>(f:'State->('T*'State)option)(s:'State)=
598+
letres= ResizeArray<_>()
599+
let recloop state=
600+
match f statewith
601+
| None->()
602+
| Some(x,s')->
603+
res.Add(x)
604+
loop s'
605+
loop s
606+
res.ToArray()
607+
596608
[<CompiledName("Unzip")>]
597609
letunzip(array:_[])=
598610
checkNonNull"array" array

‎src/fsharp/FSharp.Core/array.fsi‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,15 @@ namespace Microsoft.FSharp.Collections
914914
[<CompiledName("TryFindIndexBack")>]
915915
val tryFindIndexBack:predicate:('T->bool)->array:'T[]->int option
916916

917+
///<summary>Returns an array that contains the elements generated by the given computation.
918+
///The given initial <c>state</c> argument is passed to the element generator.</summary>
919+
///<param name="generator">A function that takes in the current state and returns an option tuple of the next
920+
///element of the array and the next state value.</param>
921+
///<param name="state">The initial state value.</param>
922+
///<returns>The result array.</returns>
923+
[<CompiledName("Unfold")>]
924+
val unfold<'T,'State>:generator:('State->('T* 'State)option)->state:'State-> 'T[]
925+
917926
///<summary>Splits an array of pairs into two arrays.</summary>
918927
///<param name="array">The input array.</param>
919928
///<returns>The two arrays.</returns>

‎src/fsharp/FSharp.Core/list.fs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,4 +572,7 @@ namespace Microsoft.FSharp.Collections
572572
|_-> invalidArg"source"(SR.GetString(SR.inputSequenceTooLong))
573573

574574
[<CompiledName("Truncate")>]
575-
lettruncate count list= Microsoft.FSharp.Primitives.Basics.List.truncate count list
575+
lettruncate count list= Microsoft.FSharp.Primitives.Basics.List.truncate count list
576+
577+
[<CompiledName("Unfold")>]
578+
letunfold<'T,'State>(f:'State->('T*'State)option)(s:'State)= Microsoft.FSharp.Primitives.Basics.List.unfold f s

‎src/fsharp/FSharp.Core/list.fsi‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,15 @@ namespace Microsoft.FSharp.Collections
790790
[<CompiledName("TryFindIndexBack")>]
791791
val tryFindIndexBack:predicate:('T->bool)->list:'T list->int option
792792

793+
///<summary>Returns a list that contains the elements generated by the given computation.
794+
///The given initial <c>state</c> argument is passed to the element generator.</summary>
795+
///<param name="generator">A function that takes in the current state and returns an option tuple of the next
796+
///element of the list and the next state value.</param>
797+
///<param name="state">The initial state value.</param>
798+
///<returns>The result list.</returns>
799+
[<CompiledName("Unfold")>]
800+
val unfold<'T,'State>:generator:('State->('T* 'State)option)->state:'State-> 'T list
801+
793802
///<summary>Splits a list of pairs into two lists.</summary>
794803
///<param name="list">The input list.</param>
795804
///<returns>Two lists of split elements.</returns>

‎src/fsharp/FSharp.Core/local.fs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,22 @@ module internal List =
426426
letcons= freshConsNoTail h
427427
truncateToFreshConsTail cons(count-1) t
428428
cons
429+
430+
let recunfoldToFreshConsTail cons f s=
431+
match f swith
432+
| None-> setFreshConsTail cons[]
433+
| Some(x,s')->
434+
letcons2= freshConsNoTail x
435+
setFreshConsTail cons cons2
436+
unfoldToFreshConsTail cons2 f s'
437+
438+
letunfold(f:'State->('T* 'State)option)(s:'State)=
439+
match f swith
440+
| None->[]
441+
| Some(x,s')->
442+
letcons= freshConsNoTail x
443+
unfoldToFreshConsTail cons f s'
444+
cons
429445

430446
// optimized mutation-based implementation. This code is only valid in fslib, where mutation of private
431447
// tail cons cells is permitted in carefully written library code.

‎src/fsharp/FSharp.Core/local.fsi‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module internal List =
2424
val rev: 'T list-> 'T list
2525
val concat: seq<'T list>-> 'T list
2626
val iteri: action:(int-> 'T-> unit)-> 'T list-> unit
27+
val unfold:('State->('T* 'State) option)-> 'State-> 'T list
2728
val unzip:('T1* 'T2) list-> 'T1 list* 'T2 list
2829
val unzip3:('T1* 'T2* 'T3) list-> 'T1 list* 'T2 list* 'T3 list
2930
val zip: 'T1 list-> 'T2 list->('T1* 'T2) list

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp