Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/FSharpPlus/Data/NonEmptyList.fs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ module NonEmptyList =
/// <summary>
/// Zip safely two lists. If one list is shorter, excess elements are discarded from the right end of the longer list.
/// </summary>
/// <param name="a1">First input list.</param>
/// <param name="a2">Second input list.</param>
/// <param name="list1">First input list.</param>
/// <param name="list2">Second input list.</param>
/// <returns>List with corresponding pairs of input lists.</returns>
let zipShortest (list1: NonEmptyList<'T>) (list2: NonEmptyList<'U>) =
{ Head = (list1.Head, list2.Head); Tail = List.zipShortest list1.Tail list2.Tail }
Expand Down
4 changes: 2 additions & 2 deletions src/FSharpPlus/Data/NonEmptyMap.fs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ module NonEmptyMap =
| x::xs -> create x xs

/// <summary>Builds a non empty map from the given non-empty sequence.</summary>
/// <param name="sequence">The input sequence.</param>
/// <param name="source">The input sequence.</param>
/// <returns>Non empty map containing the elements of the non-empty sequence.</returns>
let ofNonEmptySeq (seq: _ NonEmptySeq) = create seq.First (Seq.tail seq)
let ofNonEmptySeq (source: _ NonEmptySeq) = create source.First (Seq.tail source)

/// <summary>Builds a non empty map from the given map.</summary>
/// <param name="map">The input map.</param>
Expand Down
6 changes: 3 additions & 3 deletions src/FSharpPlus/Data/NonEmptySeq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ module NonEmptySeq =
else Some (unsafeOfArray array)

/// <summary>Builds a non empty sequence from the given list.</summary>
/// <param name="seq">The input list.</param>
/// <param name="list">The input list.</param>
/// <returns>Non empty sequence containing the elements of the list.</returns>
/// <exception cref="System.ArgumentException">Thrown when the input list is empty.</exception>
/// <remarks>Throws exception for empty list</remarks>
Expand Down Expand Up @@ -391,7 +391,7 @@ module NonEmptySeq =
///
/// This is a stable sort, that is the original order of equal elements is preserved.</remarks>
/// <param name="comparer">The function to compare the collection elements.</param>
/// <param name="list">The input sequence.</param>
/// <param name="source">The input sequence.</param>
/// <returns>The result sequence.</returns>
/// <remarks>This function consumes the whole input sequence before yielding the first element of the result sequence.</remarks>
let sortWith comparer (source: NonEmptySeq<_>) = Seq.sortWith comparer source |> unsafeOfSeq
Expand Down Expand Up @@ -528,4 +528,4 @@ module NonEmptySeqBuilder =
member __.Yield x = NonEmptySeq.singleton x
member __.Delay expr = expr () : NonEmptySeq<'T>

let neseq = NESeqBuilder ()
let neseq = NESeqBuilder ()
14 changes: 7 additions & 7 deletions src/FSharpPlus/Data/NonEmptySet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ module NonEmptySet =
| x::xs -> create x xs

/// <summary>Builds a non empty set from the given non-empty sequence.</summary>
/// <param name="list">The input sequence.</param>
/// <param name="source">The input sequence.</param>
/// <returns>Non empty set containing the elements of the non-empty sequence.</returns>
let ofNonEmptySeq (seq: _ NonEmptySeq) =
create seq.First (Seq.tail seq)
let ofNonEmptySeq (source: _ NonEmptySeq) =
create source.First (Seq.tail source)

/// <summary>Builds a non empty set from the given set.</summary>
/// <param name="set">The input set.</param>
Expand All @@ -120,15 +120,15 @@ module NonEmptySet =
/// <summary>Returns a new set with an element added to the set. No exception is raised if
/// the set already contains the given element.</summary>
/// <param name="value">The value to add.</param>
/// <param name="set">The input set.</param>
/// <param name="source">The input set.</param>
/// <returns>A new set containing <c>value</c>.</returns>
let add value (nes: _ NonEmptySet) = { Value = Set.add value nes.Value }
let add value (source: _ NonEmptySet) = { Value = Set.add value source.Value }

/// <summary>Evaluates to "true" if the given element is in the given set.</summary>
/// <param name="element">The element to test.</param>
/// <param name="set">The input set.</param>
/// <param name="source">The input set.</param>
/// <returns>True if <c>element</c> is in <c>set</c>.</returns>
let contains element (nes: _ NonEmptySet) = nes.Value |> Set.contains element
let contains element (source: _ NonEmptySet) = source.Value |> Set.contains element

/// <summary>Evaluates to "true" if all elements of the first set are in the second</summary>
/// <param name="set1">The potential subset.</param>
Expand Down
28 changes: 14 additions & 14 deletions src/FSharpPlus/Extensions/Array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ module Array =


/// <summary>Combines all values from three arrays and calls a mapping function on this combination.</summary>
/// <param name="f">Mapping function taking three element combination as input.</param>
/// <param name="x1">First array.</param>
/// <param name="x2">Second array.</param>
/// <param name="x3">Third array.</param>
/// <param name="mapping">Mapping function taking three element combination as input.</param>
/// <param name="list1">First array.</param>
/// <param name="list2">Second array.</param>
/// <param name="list3">Third array.</param>
///
/// <returns>Array with values returned from mapping function.</returns>
let lift3 f x y z =
let lenx, leny, lenz = Array.length x, Array.length y, Array.length z
let combinedFirstTwo = Array.init (lenx * leny) (fun i -> (x.[i / leny], y.[i % leny]))
let lift3 mapping list1 list2 list3 =
let lenx, leny, lenz = Array.length list1, Array.length list2, Array.length list3
let combinedFirstTwo = Array.init (lenx * leny) (fun i -> (list1.[i / leny], list2.[i % leny]))

Array.init (lenx * leny * lenz) (fun i -> combinedFirstTwo.[i/leny], z.[i%leny])
|> Array.map (fun x -> f (fst (fst x)) (snd (fst x)) (snd x))
Array.init (lenx * leny * lenz) (fun i -> combinedFirstTwo.[i/leny], list3.[i%leny])
|> Array.map (fun x -> mapping (fst (fst x)) (snd (fst x)) (snd x))

/// Concatenates all elements, using the specified separator between each element.
let intercalate (separator: _ []) (source: seq<_ []>) = source |> Seq.intercalate separator |> Seq.toArray
Expand Down Expand Up @@ -118,13 +118,13 @@ module Array =
Array.init (min a1.Length a2.Length) (fun i -> a1.[i], a2.[i])

/// <summary>Same as choose but with access to the index.</summary>
/// <param name="f">The mapping function, taking index and element as parameters.</param>
/// <param name="x">The input array.</param>
/// <param name="mapping">The mapping function, taking index and element as parameters.</param>
/// <param name="source">The input array.</param>
///
/// <returns>Array with values x for each Array value where the function returns Some(x).</returns>
let choosei f a =
let choosei mapping source =
let mutable i = ref -1
let fi x =
incr i
f !i x
Array.choose fi a
mapping !i x
Array.choose fi source
12 changes: 9 additions & 3 deletions src/FSharpPlus/Extensions/Choice.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,27 @@ module Choice =
let map (mapping: 'T->'U) (source: Choice<'T,'T2>) = match source with Choice1Of2 v -> Choice1Of2 (mapping v) | Choice2Of2 e -> Choice2Of2 e

/// <summary>Creates a Choice value from a pair of Choice values, using a function to combine the Choice1Of2 values.</summary>
/// <param name="mapping">A function to apply to the Choice1Of2 values.</param>
/// <param name="x">The first Choice value.</param>
/// <param name="y">The second Choice value.</param>
///
/// <returns>The combined value, or the first Choice2Of2.</returns>
let map2 f (x: Choice<'T,'Error>) (y: Choice<'U,'Error>) : Choice<'V,'Error> = match x, y with Choice1Of2 a, Choice1Of2 b -> Choice1Of2 (f a b) | Choice2Of2 e, _ | _, Choice2Of2 e -> Choice2Of2 e
let map2 mapping (x: Choice<'T,'Error>) (y: Choice<'U,'Error>) : Choice<'V,'Error> =
match x, y with
| Choice1Of2 a, Choice1Of2 b -> Choice1Of2 (mapping a b)
| Choice2Of2 e, _
| _, Choice2Of2 e -> Choice2Of2 e

/// <summary>Creates a Choice value from three of Choice values, using a function to combine the Choice1Of2 values.</summary>
/// <param name="mapping">A function to apply to the Choice1Of2 values.</param>
/// <param name="x">The first Choice value.</param>
/// <param name="y">The second Choice value.</param>
/// <param name="z">The third Choice value.</param>
///
/// <returns>The combined value, or the first Choice2Of2.</returns>
let map3 f (x: Choice<'T,'Error>) (y: Choice<'U,'Error>) (z: Choice<'V, 'Error>) : Choice<'W,'Error> =
let map3 mapping (x: Choice<'T,'Error>) (y: Choice<'U,'Error>) (z: Choice<'V, 'Error>) : Choice<'W,'Error> =
match x, y, z with
| Choice1Of2 a, Choice1Of2 b, Choice1Of2 c -> Choice1Of2 (f a b c)
| Choice1Of2 a, Choice1Of2 b, Choice1Of2 c -> Choice1Of2 (mapping a b c)
| Choice2Of2 e, _ , _
| _ , Choice2Of2 e, _
| _ , _ , Choice2Of2 e -> Choice2Of2 e
Expand Down
21 changes: 11 additions & 10 deletions src/FSharpPlus/Extensions/Dictionary.fs
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,26 @@ module Dictionary =
let values (source: Dictionary<_,_>) = Seq.map (fun (KeyValue(_, v)) -> v) source

/// <summary>Maps the given function over each value in the dictionary.</summary>
/// <param name="f">The mapping function.</param>
/// <param name="mapping">The mapping function.</param>
/// <param name="x">The input dictionary.</param>
///
/// <returns>The mapped dictionary.</returns>
let map f (x: Dictionary<'Key, 'T>) =
let map mapping (x: Dictionary<'Key, 'T>) =
let dct = Dictionary<'Key, 'U> ()
for KeyValue(k, v) in x do
dct.Add (k, f v)
dct.Add (k, mapping v)
dct

/// <summary>Creates a Dictionary value from a pair of Dictionaries, using a function to combine them.</summary>
/// <remarks>Keys that are not present on both dictionaries are dropped.</remarks>
/// <param name="mapping">The mapping function.</param>
/// <param name="x">The first input dictionary.</param>
/// <param name="y">The second input dictionary.</param>
///
/// <returns>The combined dictionary.</returns>
let map2 f (x: Dictionary<'Key, 'T1>) (y: Dictionary<'Key, 'T2>) =
let map2 mapping (x: Dictionary<'Key, 'T1>) (y: Dictionary<'Key, 'T2>) =
let dct = Dictionary<'Key, 'U> ()
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt f
let f = OptimizedClosures.FSharpFunc<_,_,_>.Adapt mapping
for KeyValue(k, vx) in x do
match tryGetValue k y with
| Some vy -> dct.Add (k, f.Invoke (vx, vy))
Expand All @@ -76,15 +77,15 @@ module Dictionary =

/// <summary>Combines values from three Dictionaries using mapping function.</summary>
/// <remarks>Keys that are not present on every Dictionary are dropped.</remarks>
/// <param name="f">The mapping function.</param>
/// <param name="mapping">The mapping function.</param>
/// <param name="x">First input Dictionary.</param>
/// <param name="y">Second input Dictionary.</param>
/// <param name="y">Third input Dictionary.</param>
/// <param name="z">Third input Dictionary.</param>
///
/// <returns>The mapped Dictionary.</returns>
let map3 f (x: Dictionary<'Key, 'T1>) (y: Dictionary<'Key, 'T2>) (z: Dictionary<'Key, 'T3>) =
let map3 mapping (x: Dictionary<'Key, 'T1>) (y: Dictionary<'Key, 'T2>) (z: Dictionary<'Key, 'T3>) =
let dct = Dictionary<'Key, 'U> ()
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt f
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt mapping
for KeyValue(k, vx) in x do
match tryGetValue k y, tryGetValue k z with
| Some vy, Some vz -> dct.Add (k, f.Invoke (vx, vy, vz))
Expand Down Expand Up @@ -178,4 +179,4 @@ module Dictionary =
match f k v with
| Some v -> dct.Add (k, v)
| None -> ()
dct
dct
14 changes: 12 additions & 2 deletions src/FSharpPlus/Extensions/Lazy.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,31 @@
module Lazy =

/// <summary>Creates a Lazy value from another Lazy value, mapping through a function.</summary>
/// <param name="mapping">The mapping function.</param>
/// <param name="x">The Lazy value.</param>
///
/// <returns>The mappeed value.</returns>
let map (mapping: 'T -> 'U) (x: Lazy<'T>) = Lazy<_>.Create (fun () -> mapping x.Value) : Lazy<'U>

/// <summary>Creates a Lazy value from a pair of Lazy values, using a mapping function to combine them.</summary>
/// <param name="mapping">The mapping function.</param>
/// <param name="x">The first Lazy value.</param>
/// <param name="y">The second Lazy value.</param>
///
/// <returns>The combined value.</returns>
let map2 (mapping: 'T->'U->'V) (x: Lazy<'T>) (y: Lazy<'U>) = Lazy<_>.Create (fun () -> mapping x.Value y.Value)

/// <summary>Creates a Lazy value from three Lazy values, using a function to combine them.</summary>
/// <param name="mapping">The mapping function.</param>
/// <param name="x">The first Lazy value.</param>
/// <param name="y">The second Lazy value.</param>
/// <param name="z">The third Lazy value.</param>
///
/// <returns>The combined value.</returns>
let map3 (f: 'T->'U->'V->'W) (x: Lazy<'T>) (y: Lazy<'U>) (z: Lazy<'V>) = Lazy<_>.Create (fun () -> f x.Value y.Value z.Value)
let map3 (mapping: 'T->'U->'V->'W) (x: Lazy<'T>) (y: Lazy<'U>) (z: Lazy<'V>) = Lazy<_>.Create (fun () -> mapping x.Value y.Value z.Value)

/// <summary>Applies a Lazy value to a Lazy function.</summary>
/// <param name="f">The Lazy function.</param>
/// <param name="x">The Lazy value.</param>
/// <returns>A Lazy value of the function applied to the value.</returns>
let apply (f: Lazy<'T->'U>) (x: Lazy<'T>) : Lazy<'U> = Lazy<_>.Create (fun () -> f.Value x.Value)
let apply (f: Lazy<'T->'U>) (x: Lazy<'T>) : Lazy<'U> = Lazy<_>.Create (fun () -> f.Value x.Value)
28 changes: 14 additions & 14 deletions src/FSharpPlus/Extensions/List.fs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ module List =
/// <param name="source">The input list.</param>
///
/// <returns>The result list.</returns>
let drop i list =
let drop count source =
let rec loop i lst =
match lst, i with
| [] as x, _ | x, 0 -> x
| x, n -> loop (n-1) (List.tail x)
if i > 0 then loop i list else list
if count > 0 then loop count source else source

/// Concatenates all elements, using the specified separator between each element.
let intercalate (separator: list<_>) (source: seq<list<_>>) = source |> Seq.intercalate separator |> Seq.toList
Expand Down Expand Up @@ -151,26 +151,26 @@ module List =
/// <summary>
/// Zip safely two lists. If one list is shorter, excess elements are discarded from the right end of the longer list.
/// </summary>
/// <param name="a1">First input list.</param>
/// <param name="a2">Second input list.</param>
/// <param name="list1">First input list.</param>
/// <param name="list2">Second input list.</param>
/// <returns>List with corresponding pairs of input lists.</returns>
let zipShortest (l1: list<'T1>) (l2: list<'T2>) =
let zipShortest (list1: list<'T1>) (list2: list<'T2>) =
let rec loop acc = function
| (l::ls,r::rs) -> loop ((l,r)::acc) (ls,rs)
| (_,_) -> acc
loop [] (l1,l2) |> List.rev
| (l::ls, r::rs) -> loop ((l, r)::acc) (ls, rs)
| (_, _) -> acc
loop [] (list1, list2) |> List.rev

/// <summary>Same as choose but with access to the index.</summary>
/// <param name="f">The mapping function, taking index and element as parameters.</param>
/// <param name="x">The input list.</param>
/// <param name="mapping">The mapping function, taking index and element as parameters.</param>
/// <param name="source">The input list.</param>
///
/// <returns>List with values x for each List value where the function returns Some(x).</returns>
let choosei f a =
let choosei mapping source =
let mutable i = ref -1
let fi x =
incr i
f !i x
List.choose fi a
mapping !i x
List.choose fi source

/// <summary>Attempts to remove an item from a list.</summary>
/// <param name="i">The index of the item to remove </param>
Expand All @@ -191,4 +191,4 @@ module List =
let setAt i x lst =
if List.length lst > i && i >= 0 then
lst.[0..i-1] @ x::lst.[i+1..]
else lst
else lst
21 changes: 12 additions & 9 deletions src/FSharpPlus/Extensions/Map.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ namespace FSharpPlus
/// Additional operations on Map<'Key, 'Value>
[<RequireQualifiedAccess>]
module Map =

open System.Collections.Generic

#if !FABLE_COMPILER

open System.Linq

#endif

/// <summary>Returns the keys of the given map.</summary>
Expand Down Expand Up @@ -48,27 +51,27 @@ module Map =

/// <summary>Combines values from three maps using mapping function.</summary>
/// <remarks>Keys that are not present on every Map are dropped.</remarks>
/// <param name="f">The mapping function.</param>
/// <param name="mapping">The mapping function.</param>
/// <param name="x">First input Map.</param>
/// <param name="y">Second input Map.</param>
/// <param name="y">Third input Map.</param>
/// <param name="z">Third input Map.</param>
///
/// <returns>The mapped Map.</returns>
let mapValues3 f (x: Map<'Key, 'T1>) (y: Map<'Key, 'T2>) (z: Map<'Key, 'T3>) = Map <| seq {
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt f
let mapValues3 mapping (x: Map<'Key, 'T1>) (y: Map<'Key, 'T2>) (z: Map<'Key, 'T3>) = Map <| seq {
let f = OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt mapping
for KeyValue(k, vx) in x do
match Map.tryFind k y, lazy Map.tryFind k z with
| Some vy, Lazy (Some vz) -> yield (k, f.Invoke (vx, vy, vz))
| _ , _ -> () }

/// <summary>Applies given function to each value of the given Map.</summary>
/// <param name="f">The mapping function.</param>
/// <param name="x">The input Map.</param>
/// <param name="mapping">The mapping function.</param>
/// <param name="source">The input Map.</param>
///
/// <returns>Returns Map with values x for each Map value where the function returns Some(x).</returns>
let chooseValues f (x: Map<'Key, 'T>) = Map <| seq {
for KeyValue(k, v) in x do
match f v with
let chooseValues mapping (source: Map<'Key, 'T>) = Map <| seq {
for KeyValue(k, v) in source do
match mapping v with
| Some v -> yield (k, v)
| None -> () }

Expand Down
4 changes: 2 additions & 2 deletions src/FSharpPlus/Extensions/ResizeArray.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module ResizeArray =
/// <returns>The result ResizeArray.</returns>
///
/// <exception cref="System.ArgumentNullException">Thrown when the input ResizeArray is null.</exception>
let map (f: 'T->'U) (x: ResizeArray<'T>) = ResizeArray (Seq.map f x)
let map (mapping: 'T->'U) (source: ResizeArray<'T>) = ResizeArray (Seq.map mapping source)

/// <summary>Applies a ResizeArray of functions to a ResizeArray of values and concatenates them.</summary>
/// <param name="f">The functions.</param>
Expand All @@ -34,7 +34,7 @@ module ResizeArray =
let lift2 mapping (x1: ResizeArray<'T>) (x2: ResizeArray<'U>) = ResizeArray (Seq.lift2 mapping x1 x2)

/// <summary>Combines values from three ResizeArrays and calls a mapping function on this combination.</summary>
/// <param name="f">Mapping function taking three element combination as input.</param>
/// <param name="mapping">Mapping function taking three element combination as input.</param>
/// <param name="x1">First ResizeArray.</param>
/// <param name="x2">Second ResizeArray.</param>
/// <param name="x3">Third ResizeArray.</param>
Expand Down
Loading