Skip to content

Commit c60ccdc

Browse files
committed
Added F# Seq type
1 parent d651968 commit c60ccdc

File tree

18 files changed

+1225
-167
lines changed

18 files changed

+1225
-167
lines changed

src/Fable.Transforms/Fable2Babel.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ module Util =
982982

983983
let enumerator2iterator com ctx =
984984
let enumerator = Expression.callExpression(get None (Expression.identifier("this")) "GetEnumerator", [||])
985-
BlockStatement([| Statement.returnStatement(libCall com ctx None "Seq" "toIterator" [|enumerator|])|])
985+
BlockStatement([| Statement.returnStatement(libCall com ctx None "Util" "toIterator" [|enumerator|])|])
986986

987987
let extractBaseExprFromBaseCall (com: IBabelCompiler) (ctx: Context) (baseType: Fable.DeclaredType option) baseCall =
988988
match baseCall, baseType with

src/Fable.Transforms/Replacements.fs

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,12 +1372,12 @@ let operators (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr o
13721372
| _ -> args
13731373
let modul, meth, args =
13741374
match genArg with
1375-
| Char -> "Seq", "rangeChar", args
1376-
| Builtin BclInt64 -> "Seq", "rangeLong", (addStep args) @ [makeBoolConst false]
1377-
| Builtin BclUInt64 -> "Seq", "rangeLong", (addStep args) @ [makeBoolConst true]
1378-
| Builtin BclDecimal -> "Seq", "rangeDecimal", addStep args
1379-
| Builtin BclBigInt -> "BigInt", "range", addStep args
1380-
| _ -> "Seq", "rangeNumber", addStep args
1375+
| Char -> "Range", "rangeChar", args
1376+
| Builtin BclInt64 -> "Range", "rangeInt64", addStep args
1377+
| Builtin BclUInt64 -> "Range", "rangeUInt64", addStep args
1378+
| Builtin BclDecimal -> "Range", "rangeDecimal", addStep args
1379+
| Builtin BclBigInt -> "Range", "rangeBigInt", addStep args
1380+
| _ -> "Range", "rangeDouble", addStep args
13811381
Helper.LibCall(com, modul, meth, t, args, i.SignatureArgTypes, ?loc=r) |> Some
13821382
// Pipes and composition
13831383
| "op_PipeRight", [x; f]
@@ -1542,7 +1542,7 @@ let implementedStringFunctions =
15421542
|]
15431543

15441544
let getEnumerator com r t expr =
1545-
Helper.LibCall(com, "Seq", "getEnumerator", t, [toSeq Any expr], ?loc=r)
1545+
Helper.LibCall(com, "Util", "getEnumerator", t, [toSeq Any expr], ?loc=r)
15461546

15471547
let strings (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
15481548
match i.CompiledName, thisArg, args with
@@ -1660,7 +1660,7 @@ let stringModule (com: ICompiler) (ctx: Context) r t (i: CallInfo) (_: Expr opti
16601660
| meth, args ->
16611661
Helper.LibCall(com, "String", Naming.lowerFirst meth, t, args, i.SignatureArgTypes, ?loc=r) |> Some
16621662

1663-
let seqs (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
1663+
let seqModule (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
16641664
let sort r returnType descending projection args genArg =
16651665
let compareFn =
16661666
let identExpr ident =
@@ -1749,16 +1749,16 @@ let resizeArrays (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (this
17491749
| "Clear", Some ar, _ ->
17501750
Helper.LibCall(com, "Util", "clear", t, [ar], ?loc=r) |> Some
17511751
| "Find", Some ar, [arg] ->
1752-
let opt = Helper.LibCall(com, "Seq", "tryFind", t, [arg; ar; defaultof com ctx t], ?loc=r)
1753-
Helper.LibCall(com, "Option", "value", t, [opt], ?loc=r) |> Some
1752+
let opt = Helper.LibCall(com, "Array", "tryFind", t, [arg; ar], ?loc=r)
1753+
Helper.LibCall(com, "Option", "defaultArg", t, [opt; defaultof com ctx t], ?loc=r) |> Some
17541754
| "Exists", Some ar, [arg] ->
17551755
let left = Helper.InstanceCall(ar, "findIndex", Number Int32, [arg], ?loc=r)
17561756
makeEqOp r left (makeIntConst -1) BinaryGreater |> Some
17571757
| "FindLast", Some ar, [arg] ->
1758-
let opt = Helper.LibCall(com, "Seq", "tryFindBack", t, [arg; ar; defaultof com ctx t], ?loc=r)
1759-
Helper.LibCall(com, "Option", "value", t, [opt], ?loc=r) |> Some
1758+
let opt = Helper.LibCall(com, "Array", "tryFindBack", t, [arg; ar], ?loc=r)
1759+
Helper.LibCall(com, "Option", "defaultArg", t, [opt; defaultof com ctx t], ?loc=r) |> Some
17601760
| "FindAll", Some ar, [arg] ->
1761-
Helper.LibCall(com, "Seq", "filter", t, [arg; ar], ?loc=r) |> toArray r t |> Some
1761+
Helper.LibCall(com, "Array", "filter", t, [arg; ar], ?loc=r) |> Some
17621762
| "AddRange", Some ar, [arg] ->
17631763
Helper.LibCall(com, "Array", "addRangeInPlace", t, [arg; ar], ?loc=r) |> Some
17641764
| "GetRange", Some ar, [idx; cnt] ->
@@ -1860,16 +1860,13 @@ let arrayModule (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (_: Ex
18601860
newArray (makeIntConst 0) t |> Some
18611861
| "IsEmpty", [ar] ->
18621862
eq (get r (Number Int32) ar "length") (makeIntConst 0) |> Some
1863-
| "AllPairs", args ->
1864-
let allPairs = Helper.LibCall(com, "Seq", "allPairs", t, args, i.SignatureArgTypes, ?loc=r)
1865-
toArray r t allPairs |> Some
18661863
| "TryExactlyOne", args ->
18671864
tryCoreOp com r t "Array" "exactlyOne" args |> Some
1868-
| "SortInPlace", args ->
1869-
let _, thisArg = List.splitLast args
1870-
let argTypes = List.take (List.length args) i.SignatureArgTypes
1871-
let compareFn = (genArg com ctx r 0 i.GenericArgs) |> makeComparerFunction com ctx
1872-
Helper.InstanceCall(thisArg, "sort", t, [compareFn], argTypes, ?loc=r) |> Some
1865+
// | "SortInPlace", args ->
1866+
// let _, thisArg = List.splitLast args
1867+
// let argTypes = List.take (List.length args) i.SignatureArgTypes
1868+
// let compareFn = (genArg com ctx r 0 i.GenericArgs) |> makeComparerFunction com ctx
1869+
// Helper.InstanceCall(thisArg, "sort", t, [compareFn], argTypes, ?loc=r) |> Some
18731870
| "CopyTo", args ->
18741871
copyToArray com r t i args
18751872
| Patterns.DicContains nativeArrayFunctions meth, _ ->
@@ -1908,9 +1905,6 @@ let listModule (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (_: Exp
19081905
// Use a cast to give it better chances of optimization (e.g. converting list
19091906
// literals to arrays) after the beta reduction pass
19101907
| "ToSeq", [x] -> toSeq t x |> Some
1911-
| "AllPairs", args ->
1912-
let allPairs = Helper.LibCall(com, "Seq", "allPairs", t, args, i.SignatureArgTypes, ?loc=r)
1913-
toList com t allPairs |> Some
19141908
| "TryExactlyOne", args ->
19151909
tryCoreOp com r t "List" "exactlyOne" args |> Some
19161910
| meth, _ ->
@@ -1990,19 +1984,20 @@ let optionModule (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (_: E
19901984
Helper.LibCall(com, "Option", Naming.lowerFirst meth, t, args, i.SignatureArgTypes, ?loc=r) |> Some
19911985
| "ToArray", [arg] ->
19921986
toArray r t arg |> Some
1987+
| "ToList", [arg] ->
1988+
let args = args |> List.replaceLast (toArray None t)
1989+
Helper.LibCall(com, "List", "ofArray", t, args, ?loc=r) |> Some
19931990
| "FoldBack", [folder; opt; state] ->
19941991
Helper.LibCall(com, "Seq", "foldBack", t, [folder; toArray None t opt; state], i.SignatureArgTypes, ?loc=r) |> Some
19951992
| ("DefaultValue" | "OrElse"), _ ->
19961993
Helper.LibCall(com, "Option", "defaultArg", t, List.rev args, ?loc=r) |> Some
19971994
| ("DefaultWith" | "OrElseWith"), _ ->
19981995
Helper.LibCall(com, "Option", "defaultArgWith", t, List.rev args, List.rev i.SignatureArgTypes, ?loc=r) |> Some
1999-
| ("Count" | "Contains" | "Exists" | "Fold" | "ForAll" | "Iterate" | "ToList" as meth), _ ->
1996+
| ("Count" | "Contains" | "Exists" | "Fold" | "ForAll" | "Iterate" as meth), _ ->
1997+
let meth = Naming.lowerFirst meth
20001998
let args = args |> List.replaceLast (toArray None t)
2001-
let moduleName, meth =
2002-
if meth = "ToList"
2003-
then "List", "ofArray"
2004-
else "Seq", Naming.lowerFirst meth
2005-
Helper.LibCall(com, moduleName, meth, t, args, i.SignatureArgTypes, ?loc=r) |> Some
1999+
let args = injectArg com ctx r "Seq" meth i.GenericArgs args
2000+
Helper.LibCall(com, "Seq", meth, t, args, i.SignatureArgTypes, ?loc=r) |> Some
20062001
| _ -> None
20072002

20082003
let parseBool (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
@@ -2267,18 +2262,19 @@ let intrinsicFunctions (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisAr
22672262
// Type: RangeChar : char -> char -> seq<char>
22682263
// Usage: RangeChar start stop
22692264
| "RangeChar", None, _ ->
2270-
Helper.LibCall(com, "Seq", "rangeChar", t, args, i.SignatureArgTypes, ?loc=r) |> Some
2265+
Helper.LibCall(com, "Range", "rangeChar", t, args, i.SignatureArgTypes, ?loc=r) |> Some
22712266
// reference: https://msdn.microsoft.com/visualfsharpdocs/conceptual/operatorintrinsics.rangedouble-function-%5bfsharp%5d
22722267
// Type: RangeDouble: float -> float -> float -> seq<float>
22732268
// Usage: RangeDouble start step stop
22742269
| ("RangeSByte" | "RangeByte"
22752270
| "RangeInt16" | "RangeUInt16"
22762271
| "RangeInt32" | "RangeUInt32"
22772272
| "RangeSingle" | "RangeDouble"), None, args ->
2278-
Helper.LibCall(com, "Seq", "rangeNumber", t, args, i.SignatureArgTypes, ?loc=r) |> Some
2279-
| ("RangeInt64" | "RangeUInt64"), None, args ->
2280-
let isUnsigned = makeBoolConst (i.CompiledName = "RangeUInt64")
2281-
Helper.LibCall(com, "Seq", "rangeLong", t, args @ [isUnsigned] , i.SignatureArgTypes, ?loc=r) |> Some
2273+
Helper.LibCall(com, "Range", "rangeDouble", t, args, i.SignatureArgTypes, ?loc=r) |> Some
2274+
| "RangeInt64", None, args ->
2275+
Helper.LibCall(com, "Range", "rangeInt64", t, args, i.SignatureArgTypes, ?loc=r) |> Some
2276+
| "RangeUInt64", None, args ->
2277+
Helper.LibCall(com, "Range", "rangeUInt64", t, args, i.SignatureArgTypes, ?loc=r) |> Some
22822278
| _ -> None
22832279

22842280
let runtimeHelpers (com: ICompiler) (ctx: Context) r t (i: CallInfo) thisArg args =
@@ -3062,8 +3058,8 @@ let private replacedModules =
30623058
"Microsoft.FSharp.Collections.ListModule", listModule
30633059
"Microsoft.FSharp.Collections.HashIdentity", fsharpModule
30643060
"Microsoft.FSharp.Collections.ComparisonIdentity", fsharpModule
3065-
"Microsoft.FSharp.Core.CompilerServices.RuntimeHelpers", seqs
3066-
"Microsoft.FSharp.Collections.SeqModule", seqs
3061+
"Microsoft.FSharp.Core.CompilerServices.RuntimeHelpers", seqModule
3062+
"Microsoft.FSharp.Collections.SeqModule", seqModule
30673063
"System.Collections.Generic.KeyValuePair`2", keyValuePairs
30683064
"System.Collections.Generic.Comparer`1", bclType
30693065
"System.Collections.Generic.EqualityComparer`1", bclType

src/Fable.Transforms/ReplacementsInject.fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ let fableReplacementsModules =
7878
"groupBy", (Types.equalityComparer, 1)
7979
"countBy", (Types.equalityComparer, 1)
8080
]
81+
"Seq", Map [
82+
"contains", (Types.equalityComparer, 0)
83+
"except", (Types.equalityComparer, 0)
84+
"sort", (Types.comparer, 0)
85+
"sortBy", (Types.comparer, 1)
86+
"sortDescending", (Types.comparer, 0)
87+
"sortByDescending", (Types.comparer, 1)
88+
"sum", ("Fable.Core.IGenericAdder`1", 0)
89+
"sumBy", ("Fable.Core.IGenericAdder`1", 1)
90+
"maxBy", (Types.comparer, 1)
91+
"max", (Types.comparer, 0)
92+
"minBy", (Types.comparer, 1)
93+
"min", (Types.comparer, 0)
94+
"average", ("Fable.Core.IGenericAverager`1", 0)
95+
"averageBy", ("Fable.Core.IGenericAverager`1", 1)
96+
]
8197
"Set", Map [
8298
"FSharpSet__Map", (Types.comparer, 1)
8399
"singleton", (Types.comparer, 0)

src/fable-library/Array.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,15 @@ let sortByDescending (projection: 'a->'b) (xs: 'a[]) ([<Inject>] comparer: IComp
676676
let sortWith (comparer: 'T -> 'T -> int) (xs: 'T[]): 'T[] =
677677
sortInPlaceWith comparer (copyImpl xs)
678678

679+
let allPairs (xs: 'T1[]) (ys: 'T2[]): ('T1 * 'T2)[] =
680+
let len1 = xs.Length
681+
let len2 = ys.Length
682+
let res = allocateArray (len1 * len2)
683+
for i = 0 to xs.Length-1 do
684+
for j = 0 to ys.Length-1 do
685+
res.[i * len2 + j] <- (xs.[i], ys.[j])
686+
res
687+
679688
let unfold<'T, 'State> (generator: 'State -> ('T*'State) option) (state: 'State): 'T[] =
680689
let res: 'T[] = [||]
681690
let rec loop state =

src/fable-library/Async.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { protectedCont } from "./AsyncBuilder.js";
77
import { protectedBind } from "./AsyncBuilder.js";
88
import { protectedReturn } from "./AsyncBuilder.js";
99
import { FSharpChoice$2, Choice_makeChoice1Of2, Choice_makeChoice2Of2 } from "./Choice.js";
10-
import { map } from "./Seq.js";
1110

1211
// Implemented just for type references
1312
export class Async<_T> { }
@@ -102,7 +101,7 @@ export function ignore<T>(computation: IAsync<T>) {
102101
}
103102

104103
export function parallel<T>(computations: Iterable<IAsync<T>>) {
105-
return awaitPromise(Promise.all(map((w) => startAsPromise(w), computations)));
104+
return awaitPromise(Promise.all(Array.from(computations, (w) => startAsPromise(w))));
106105
}
107106

108107
export function sleep(millisecondsDueTime: number) {

src/fable-library/BigInt.fs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -170,19 +170,3 @@ let fromByteArray (bytes:byte array) =
170170
||| (uint32 b3 <<< 24)
171171
loop (value :: accumUInt32) (currIndex + bytesToProcess) (bytesRemaining - bytesToProcess) lowBitFound
172172
loop [] 0 bytes.Length false
173-
174-
let makeRangeStepFunction (step: bigint) (last: bigint) =
175-
let stepComparedWithZero = compare step zero
176-
if stepComparedWithZero = 0 then
177-
failwith "The step of a range cannot be zero"
178-
let stepGreaterThanZero = stepComparedWithZero > 0
179-
fun (x: bigint) ->
180-
let comparedWithLast = compare x last
181-
if (stepGreaterThanZero && comparedWithLast <= 0)
182-
|| (not stepGreaterThanZero && comparedWithLast >= 0) then
183-
Some (x, x + step)
184-
else None
185-
186-
let range (first: bigint) (step: bigint) (last: bigint) =
187-
let stepFn = makeRangeStepFunction step last
188-
Seq.delay(fun () -> Seq.unfold stepFn first)

src/fable-library/Decimal.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,19 @@ export function getBits(d: Decimal) {
194194
return [low, mid, high, signExp];
195195
}
196196

197-
export function makeRangeStepFunction(step: Decimal, last: Decimal) {
198-
const stepComparedWithZero = step.cmp(get_Zero);
199-
if (stepComparedWithZero === 0) {
200-
throw new Error("The step of a range cannot be zero");
201-
}
202-
const stepGreaterThanZero = stepComparedWithZero > 0;
203-
return (x: Decimal) => {
204-
const comparedWithLast = x.cmp(last);
205-
if ((stepGreaterThanZero && comparedWithLast <= 0)
206-
|| (!stepGreaterThanZero && comparedWithLast >= 0)) {
207-
return [x, op_Addition(x, step)];
208-
} else {
209-
return undefined;
210-
}
211-
};
212-
}
197+
// export function makeRangeStepFunction(step: Decimal, last: Decimal) {
198+
// const stepComparedWithZero = step.cmp(get_Zero);
199+
// if (stepComparedWithZero === 0) {
200+
// throw new Error("The step of a range cannot be zero");
201+
// }
202+
// const stepGreaterThanZero = stepComparedWithZero > 0;
203+
// return (x: Decimal) => {
204+
// const comparedWithLast = x.cmp(last);
205+
// if ((stepGreaterThanZero && comparedWithLast <= 0)
206+
// || (!stepGreaterThanZero && comparedWithLast >= 0)) {
207+
// return [x, op_Addition(x, step)];
208+
// } else {
209+
// return undefined;
210+
// }
211+
// };
212+
// }

src/fable-library/Event.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { IObservable, IObserver, Observer, protect } from "./Observable.js";
22
import { Option, some, value } from "./Option.js";
33
import { FSharpChoice$2, Choice_tryValueIfChoice1Of2, Choice_tryValueIfChoice2Of2 } from "./Choice.js";
4-
import { iterate as seqIterate } from "./Seq.js";
54
import { IDisposable } from "./Util.js";
65

76
export type Delegate<T> = (x: T) => void;
@@ -38,7 +37,7 @@ export class Event<T> implements IEvent<T> {
3837
}
3938

4039
public Trigger(value: T) {
41-
seqIterate((f) => f(value), this.delegates);
40+
this.delegates.forEach((f) => f(value));
4241
}
4342

4443
// IDelegateEvent<T> methods

src/fable-library/Fable.Library.fsproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
<Compile Include="Choice.fs" />
2525
<Compile Include="Array.fs" />
2626
<Compile Include="List.fs" />
27+
<Compile Include="Seq.fs" />
28+
<Compile Include="Range.fs" />
2729
<Compile Include="Set.fs" />
2830
<Compile Include="Map.fs" />
2931
</ItemGroup>

0 commit comments

Comments
 (0)