@@ -236,47 +236,39 @@ module TaskSeq =
236236
237237 let tryHead source = Internal.tryHead source
238238
239- let head source = task {
240- match ! Internal.tryHead source with
241- | Some head -> return head
242- | None -> return Internal.raiseEmptySeq ()
243- }
239+ let head source =
240+ Internal.tryHead source
241+ |> Task.map ( Option.defaultWith Internal.raiseEmptySeq)
244242
245243 let tryLast source = Internal.tryLast source
246244
247- let last source = task {
248- match ! Internal.tryLast source with
249- | Some last -> return last
250- | None -> return Internal.raiseEmptySeq ()
251- }
245+ let last source =
246+ Internal.tryLast source
247+ |> Task.map ( Option.defaultWith Internal.raiseEmptySeq)
252248
253249 let tryTail source = Internal.tryTail source
254250
255- let tail source = task {
256- match ! Internal.tryTail source with
257- | Some result -> return result
258- | None -> return Internal.raiseEmptySeq ()
259- }
251+ let tail source =
252+ Internal.tryTail source
253+ |> Task.map ( Option.defaultWith Internal.raiseEmptySeq)
260254
261255 let tryItem index source = Internal.tryItem index source
262256
263- let item index source = task {
264- match ! Internal.tryItem index source with
265- | Some item -> return item
266- | None ->
267- if index < 0 then
268- return invalidArg ( nameof index) " The input must be non-negative."
269- else
270- return Internal.raiseInsufficient ()
271- }
257+ let item index source =
258+ if index < 0 then
259+ invalidArg ( nameof index) " The input must be non-negative."
260+
261+ Internal.tryItem index source
262+ |> Task.map ( Option.defaultWith Internal.raiseInsufficient)
272263
273264 let tryExactlyOne source = Internal.tryExactlyOne source
274265
275- let exactlyOne source = task {
276- match ! Internal.tryExactlyOne source with
277- | Some item -> return item
278- | None -> return invalidArg ( nameof source) " The input sequence contains more than one element."
279- }
266+ let exactlyOne source =
267+ Internal.tryExactlyOne source
268+ |> Task.map (
269+ Option.defaultWith ( fun () ->
270+ invalidArg ( nameof source) " The input sequence contains more than one element." )
271+ )
280272
281273 let indexed ( source : taskSeq < 'T >) =
282274 Internal.checkNonNull ( nameof source) source
@@ -314,47 +306,34 @@ module TaskSeq =
314306 Internal.tryFind ( Predicate((=) value)) source
315307 |> Task.map ( Option.isSome)
316308
317- let pick chooser source = task {
318- match ! Internal.tryPick ( TryPick chooser) source with
319- | Some item -> return item
320- | None -> return Internal.raiseNotFound ()
321- }
309+ let pick chooser source =
310+ Internal.tryPick ( TryPick chooser) source
311+ |> Task.map ( Option.defaultWith Internal.raiseNotFound)
322312
323- let pickAsync chooser source = task {
324- match ! Internal.tryPick ( TryPickAsync chooser) source with
325- | Some item -> return item
326- | None -> return Internal.raiseNotFound ()
327- }
328-
329- let find predicate source = task {
330- match ! Internal.tryFind ( Predicate predicate) source with
331- | Some item -> return item
332- | None -> return Internal.raiseNotFound ()
333- }
313+ let pickAsync chooser source =
314+ Internal.tryPick ( TryPickAsync chooser) source
315+ |> Task.map ( Option.defaultWith Internal.raiseNotFound)
334316
317+ let find predicate source =
318+ Internal.tryFind ( Predicate predicate) source
319+ |> Task.map ( Option.defaultWith Internal.raiseNotFound)
335320
336- let findAsync predicate source = task {
337- match ! Internal.tryFind ( PredicateAsync predicate) source with
338- | Some item -> return item
339- | None -> return Internal.raiseNotFound ()
340- }
321+ let findAsync predicate source =
322+ Internal.tryFind ( PredicateAsync predicate) source
323+ |> Task.map ( Option.defaultWith Internal.raiseNotFound)
341324
342- let findIndex predicate source = task {
343- match ! Internal.tryFindIndex ( Predicate predicate) source with
344- | Some item -> return item
345- | None -> return Internal.raiseNotFound ()
346- }
325+ let findIndex predicate source =
326+ Internal.tryFindIndex ( Predicate predicate) source
327+ |> Task.map ( Option.defaultWith Internal.raiseNotFound)
347328
348- let findIndexAsync predicate source = task {
349- match ! Internal.tryFindIndex ( PredicateAsync predicate) source with
350- | Some item -> return item
351- | None -> return Internal.raiseNotFound ()
352- }
329+ let findIndexAsync predicate source =
330+ Internal.tryFindIndex ( PredicateAsync predicate) source
331+ |> Task.map ( Option.defaultWith Internal.raiseNotFound)
353332
354333
355334
356335 //
357- // zip/unzip etc functions
336+ // zip/unzip/fold etc functions
358337 //
359338
360339 let zip source1 source2 = Internal.zip source1 source2
0 commit comments