Тёмный

Fast F#: Intersect of Seq using Seq Computation Expression 

Fast F#
Подписаться 2,3 тыс.
Просмотров 631
50% 1

Опубликовано:

 

2 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 8   
@robertlenders8755
@robertlenders8755 Год назад
Assume this is hugely faster than the otherwise trivial implementation? `let intersect (a: 'T seq) (b: 'T seq): 'T seq = Set.intersect (Set.ofSeq a) (Set.ofSeq b) :> 'T seq`
@FastFSharp
@FastFSharp Год назад
That's a good thought. The downside is that you are allocating memory for the Set collection. In this scenario I am not wanting any extra allocations so I avoided the use of additional collections. Set is also slow compared to doing something similar with a HashSet.
@WerewolfAll
@WerewolfAll Год назад
Code in initial comment doesn't require your original sequences to be sorted or contain unique values. So it's more convenient. On the other hand you must rename your function to reflect that it accepts only sorted unique values.
@MichaelOyer
@MichaelOyer Год назад
One thing to add here as a best practice is to use the `use` keyword on your enumerators. IEnumerator implements the IDisposable interface. `for` loops do this automatically.
@10199able
@10199able Год назад
I swear you make them faster than I watch
@FastFSharp
@FastFSharp Год назад
Don't worry. It will slow down. This is double feature week since I was out sick last week 😊
@iandavies2209
@iandavies2209 Год назад
It seems to me that i would probably have reached for a reduction before a computation expression. However the post is enlightening for other reasons so thanks!
@FastFSharp
@FastFSharp Год назад
You certainly could.