Reinventing LINQ

by Matt 20. March 2008 19:54

Daniel Cazzulino made an interesting post a little while ago that I've been meaning to follow up. Ian Griffiths picked up on one aspect of it (the interesting, obscure type inference issue - and I love the idea of "mumble types") which reminded me to go have another look.

The gist of Daniel's post is to rewrite some Ruby code using some of the nice new C# features. The Ruby code is using Fibers (think iterators) to take a sequence as input, and output a new sequence of different values, or a new sequence made by skipping values.

And I really had to torture that sentence to avoid saying "map" and "filter". Remember, they're the magic words...

Daniel follows the original article very faithfully. He replaces the Fibers with iterators, adds some syntax sugar with extension methods and lambda functions, and duplicates the functionality of LINQ. He just stops short of using the nice from...where...select syntax. So let's have a look at the last few examples.

Get 10 integers that are both even and multiples of three:

var evenMultiplesOfThree = from x in Enumerable.Range(1, 1000)
                           where (x % 2) == 0 && (x % 3) == 0
                           select x;
foreach (int i in evenMultiplesOfThree.Take(10))
    Console.WriteLine(i);

(OK, slightly different. Daniel's range is infinite, I've capped mine.)

The pipeline for tripling even numbers, adding one and only printing the first 5 multiples of 5 (and this one lets me use "let"):

var results = from x in Enumerable.Range(1, 1000)
              let y = (x * 3) + 1
              where (y % 5) == 0
              select y;
foreach (int i in results.Take(5))
    Console.WriteLine(i);

And the (somewhat unwieldy) palindrome finder:

var words = "Madam, the civic radar rotator is not level".Split(' ');
var palindromes = from word in words
                  let normalised = new { 
                      Original = word, 
                      Normalised = word.ToLower() 
                  }
                  select "'" + normalised.Original + "' is " + 
                    ((normalised.Normalised != new string(normalised.Normalised.Reverse().ToArray()))
                      ? "not " : "") + "a palindrome";
foreach(var palindrome in palindromes)
    Console.WriteLine(palindrome);

But this is an awkward query. Perhaps a poor example to convert into a query, and a simple loop might have served better.

Personally, I don't like the idea of having a ForEach extension method. I like doing the explicit foreach command. I think it separates the enumeration over the results from the description of the query (pipeline).

I think Daniel's post validates the design of LINQ. Independently, he took iterators, lambdas and extension methods and built something that, apart from the syntax, is LINQ. That's good design, and shows that LINQ is really evolutionary in the way it's built on so many smaller features in the runtime. But by not going that final step, it also shows that LINQ, and especially the query syntax, is really revolutionary. I think it's quite a mind shift to go from a "pipeline", a processing concept that's very procedural, explicitly procedural, and convert it into a query, something that's much more declarative.

Tags:

Comments (57) -

best suv
best suv
7/20/2011 10:30:13 PM #

I wonder if he cheated on her? I remember he cheated on his previous wife with JLO so it wouldn’t be surprising.

Reply

best hybrid cars
best hybrid cars
7/22/2011 4:14:19 AM #

Is it ok if I quote your article in my monthly newsletter? I would think this article suits my topic perfectly. Well ya, thanks for posting this article.

Reply

iphone developer program
iphone developer program
7/24/2011 5:51:47 AM #

Is it ok if I quote your article in my monthly newsletter? I would think this article suits my topic perfectly. Well ya, thanks for posting this article.

Reply

Philix
Philix United States
11/21/2015 3:00:08 AM #

Good Website.

Reply

Natividad Sluder
Natividad Sluder United States
1/7/2016 2:53:04 AM #

Let me be the first to say: Congrats, Neil! I've been following this site from day one, and have loved every single day of awesomeness that it has delivered. You deserve all the success coming your way. Keep it up!AWESOME!

Reply

Raleigh Maarx
Raleigh Maarx United States
1/15/2016 8:32:36 AM #

getting the last pair of shoes in your size AWESOME!

Reply

Rodolfo Thomases
Rodolfo Thomases United States
1/15/2016 8:55:35 AM #

Congratulations on your success! You deserve it for brightening all of my work days with something to be excited about.

Reply

Chad Cassem
Chad Cassem United States
7/25/2016 3:21:50 PM #

Times are changing and so as technology. We hope to find smart home architectural planners using the updated technologies obtainable in the market.

Reply

Scott Kevin
Scott Kevin United States
8/5/2016 5:56:54 AM #

Представляем новый веб- сайт знакомств будет оштрафован на широкий свою настоящую любовь в http://youllfindlove.com/

Reply

Frances Ward
Frances Ward United States
8/16/2016 8:42:59 PM #

F*ckin’ awesome issues here. I’m very satisfied to peer your article. Thanks so much and i am looking forward to touch you. Will you kindly drop me a mail?

Reply

Mathilda Kieser
Mathilda Kieser United States
8/25/2016 4:27:58 AM #

Hi First of all I would like to say what a nice blog! I had a quick question that I’d like to ask if you don’t mind. I was curious to understand how you center yourself and clear your mind prior to blogging. I’ve had trouble clearing my thoughts in getting my thoughts out. I do enjoy posting blog but it just seems like the first 10 to 15 minutes are unproductive simply just trying to figure out how to begin. Any thought or tips? Thank you!

Reply

James Fly
James Fly United States
9/25/2016 10:09:31 PM #

Это такой хороший сайт с большим количеством полезной информации . Приходите присоединиться к нашему новый бесплатный социальный сайт, как Facebook . У нас есть группы друзей , объявления, сайт знакомств играть в игры бесплатно для всех на http://mysocialspot.ca/

Reply

baexkitty
baexkitty United States
5/22/2018 10:17:57 PM #

She may take her panties off at any time!

Reply

Mason Flanery
Mason Flanery United States
9/27/2018 8:43:09 PM #

Do you mind if I quote a few of your posts as long as I provide credit and sources back to your blog? My website is in the very same area of interest as yours and my users would really benefit from some of the information you present here. Please let me know if this ok with you. Cheers!

Reply

Marian Overholtzer
Marian Overholtzer United States
9/28/2018 2:03:12 AM #

Great post. I was checking constantly this blog and I am impressed! Very useful information particularly the remaining section Smile I take care of such info a lot. I used to be seeking this particular information for a very lengthy time. Thank you and best of luck.

Reply

Arielle Kha
Arielle Kha United States
9/28/2018 8:23:07 AM #

You can definitely see your skills in the work you write. The world hopes for even more passionate writers like you who are not afraid to say how they believe. Always go after your heart.

Reply

Ulysses Laduke
Ulysses Laduke United States
9/28/2018 11:07:33 PM #

Wow that was unusual. I just wrote an very long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Anyway, just wanted to say fantastic blog!

Reply

Judson Chanco
Judson Chanco United States
9/29/2018 5:03:34 AM #

I’ve just been talking to Sean Gallagher about his upcoming Instant Income Cash Machine course, and he’s been kind enough to fill me in on a few details regarding his upcoming course.

Reply

Gwyneth Applonie
Gwyneth Applonie United States
9/29/2018 11:12:53 AM #

You made some respectable factors there. I appeared on the internet for the issue and found most individuals will associate with together with your website.

Reply

Lucius Soza
Lucius Soza United States
9/29/2018 3:08:34 PM #

Nice read, I just passed this onto a friend who was doing a little research on that. And he actually bought me lunch as I found it for him smile So let me rephrase that: Thanks for lunch!

Reply

Latanya Merschman
Latanya Merschman United States
9/29/2018 11:56:58 PM #

It’s great page, I was looking for something like this

Reply

Pennie Hedtke
Pennie Hedtke United States
10/8/2018 9:57:27 PM #

I don?t even know how I ended up here, but I thought this post was good. I do not know who you are but definitely you're going to a famous blogger if you are not already ;) Cheers!

Reply

Leonor Volland
Leonor Volland United States
10/9/2018 4:39:33 AM #

Just about all of the things you articulate is astonishingly appropriate and it makes me wonder why I hadn't looked at this with this light before. Your piece truly did switch the light on for me personally as far as this particular subject goes. Nevertheless at this time there is actually 1 issue I am not necessarily too comfy with and while I make an effort to reconcile that with the actual central theme of your issue, let me see just what the rest of your visitors have to point out.Very well done.

Reply

Responsive Website Design
Responsive Website Design United States
10/9/2018 1:23:12 PM #

PIM商品管理系統整合

Reply

Stacy Pleasanton
Stacy Pleasanton United States
10/9/2018 9:16:32 PM #

Oh my goodness! a tremendous article dude. Thank you However I am experiencing problem with ur rss . Don’t know why Unable to subscribe to it. Is there anyone getting similar rss downside? Anyone who is aware of kindly respond. Thnkx

Reply

Mike Brezeale
Mike Brezeale United States
10/9/2018 10:20:36 PM #

hello!,I like your writing so much! share we communicate more about your post on AOL? I need a specialist on this area to solve my problem. May be that's you! Looking forward to see you.

Reply

Rocco Shultis
Rocco Shultis United States
10/10/2018 7:10:52 AM #

It's perfect time to make some plans for the future and it is time to be happy. I have read this post and if I could I wish to suggest you some interesting things or advice. Perhaps you can write next articles referring to this article. I desire to read more things about it!

Reply

玻尿酸隆鼻失敗
玻尿酸隆鼻失敗 United States
10/10/2018 10:26:45 AM #

豐盈水潤,造就柔嫩飽滿,滋潤有彈性、晶瑩煥白的肌膚。 乾性肌膚使用

Reply

醫學美容 爽身 cosmetic.wiki
醫學美容 爽身 cosmetic.wiki United States
10/10/2018 9:10:28 PM #

到底Meghan Markle是甚麼來頭?她憑什麼能俘獲英國二王子?    Marie Claire (HK) Edition  又有一名鑽石王老五被封盤了!這位還要是一位王子啊!相信大家都知道英國哈里王子已經宣布要迎娶美國女演員Meghan Markle了 ,二人很快地就接受了訪問與大家分享好消息。 哈里王子與Meghan是    

Reply

Percy Hilke
Percy Hilke United States
10/11/2018 7:39:47 AM #

You made some respectable factors there. I regarded on the web for the difficulty and located most people will go along with together with your website.

Reply

Elwood Buemi
Elwood Buemi United States
10/11/2018 12:11:26 PM #

I like the helpful info you provide in your articles. I will bookmark your weblog and check again here regularly. I'm quite certain I?ll learn many new stuff right here! Good luck for the next!

Reply

Dorla Feinman
Dorla Feinman United States
10/11/2018 4:39:52 PM #

F*ckin’ remarkable things here. I am very happy to peer your article. Thanks so much and i am looking forward to touch you. Will you please drop me a mail?

Reply

Laraine Zboral
Laraine Zboral United States
10/12/2018 6:00:27 AM #

Wow! This blog looks exactly like my old one! It's on a totally different topic but it has pretty much the same layout and design. Superb choice of colors!

Reply

Corrine Bernosky
Corrine Bernosky United States
10/14/2018 2:17:30 AM #

Youre so cool! I dont suppose Ive read anything similar to this before. So nice to discover somebody with original ideas on this subject. realy thank you for starting this up. this excellent website is one area that is needed on-line, an individual if we do originality. valuable project for bringing new things towards the internet!

Reply

醫學美容 Derma Veil 童顏針
醫學美容 Derma Veil 童顏針 United States
10/14/2018 5:05:46 PM #

Derma Veil ® 被喻為新世代逆齡完美輪廓塑造,最新一代的PLLA膠原。 2003年獲得Mexican Ministry of Health (SSA)認證及美國FDA出口認證,並於2006年在拉丁美洲及遠東至東南亞地區廣泛使用,多個臨床實例見證能改善老化、遺傳、疾病(如脂肪萎縮)等引起的凹陷問題,其效果備受認同。蘊含兩大活性成分均具有生物兼容性及分解性,可逐步被人體自然分解吸收,有效塑造童顏肌 : 1. 聚左乳酸 (Poly-L-lactic acid / PLLA) : 促進骨膠原生長 2. 甘醇酸(Glycolic Acid ) 使皮膚表皮層黏膠性脂質鬆軟,改善皮膚厚度,加速細胞再生,減少皺紋及疤痕,加強保濕功能,增加光澤,美白效果。 由於甘醇酸分子較小,容易滲透皮膚 治療前: 皮膚的凹陷/皺紋 治療後: 成分被人體吸收、並刺激膠原增生,撫平皺紋及凹陷部位。 注入BOTOX(保妥適)會抑制突觸前膜釋放神經遞質,阻斷乙酰膽鹼(Acetylcholine)的釋放,從而使肌肉張力下降或癱瘓麻痺,皺紋也隨之而逐漸消失。

Reply

Maryjane Homme
Maryjane Homme United States
10/14/2018 10:48:40 PM #

Thanks  for every other informative web site. Where else may I get that type of info written in such an ideal method? I have a venture that I'm just now operating on, and I've been on the look out for such information.

Reply

醫學美容 養護 cosmetic.wiki
醫學美容 養護 cosmetic.wiki United States
10/17/2018 4:18:40 PM #

LoveVivi & Vstyle 【馬甲彩妝系列】迷你偽裝寶盒的商品介紹   LoveVivi & Vstyle,馬甲彩妝系列,迷你偽裝寶盒       

Reply

Erminia Aronica
Erminia Aronica United States
10/19/2018 3:06:05 AM #

Excellent goods from you, man. I have understand your stuff previous to and you are just too magnificent. I actually like what you have acquired here, really like what you're stating and the way in which you say it. You make it entertaining and you still care for to keep it sensible. I can not wait to read far more from you. This is actually a terrific site.

Reply

Shaunna Vuillemot
Shaunna Vuillemot United States
10/20/2018 12:06:06 AM #

We're a group of volunteers and starting a new scheme in our community. Your site offered us with valuable info to work on. You have done an impressive job and our whole community will be thankful to you.

Reply

Pat Veasman
Pat Veasman United States
10/20/2018 1:13:44 PM #

I discovered your weblog web site on google and test a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN Information Reader. Searching for ahead to studying more from you later on!…

Reply

醫學美容 氦氖 cosmetic.wiki
醫學美容 氦氖 cosmetic.wiki United States
10/20/2018 6:39:08 PM #

UltraCeuticals 【面膜系列*】純維A美肌淨膚保濕面膜 UC Ultra A Hydration Booster的商品介紹  UrCosme (@cosme TAIWAN) 商品資訊  UltraCeuticals ,面膜系列*,純維A美肌淨膚保濕面膜 UC Ultra A Hydration Booster        

Reply

Bette Youngerman
Bette Youngerman United States
10/21/2018 12:05:40 AM #

Youre so cool! I dont suppose Ive read anything like this before. So nice to find any individual with some authentic thoughts on this subject. realy thank you for starting this up. this web site is one thing that is wanted on the web, someone with a little originality. helpful job for bringing something new to the web!

Reply

激光無創熱作用模式精準作用於陰道粘膜層、肌層,使陰道組織發生新生改變,修復由於順產而引起的損傷,解決一系列產後女性生殖系統常見問題。私密緊緻作用原理:刺激陰道粘膜固有層、粘膜肌層,使其膠原纖維、彈性纖維大量增生重塑;彈性纖維網修復,陰道收緊 SUI作用原理:恢復盆底正常解剖位置 陰道彈性纖維網得以修復,恢復到正常解剖位置避免了擠壓,牽拉尿道,使尿道角度恢復正常 盆底血供豐富,間接的使尿道括約肌得以修復進而停止漏尿或症狀減輕 私密敏感、潤滑作用原理:CO2微脈管作用,使血管重建、促進血液循環 血管活性腸肽(VIP)和神經肽Y(NPY) 表達增加,敏感度增加 血流量增加,陰道上皮細胞功能增加、陰道粘膜自分泌功能增強,潤滑度增加

Reply

Kaitlyn Wente
Kaitlyn Wente United States
10/23/2018 11:39:38 PM #

It’s really a cool and helpful piece of information. I’m glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing.

Reply

Out of the deep freeze comes the Champions League with two scoops of England versus Italy to send a tingle down the spine. Remember when Italy ruled Europe? Arsenal and Chelsea should have nothing to fear now... Serie A on the slide

Reply

Elizbeth Huffstutler
Elizbeth Huffstutler United States
11/6/2018 9:53:18 PM #

Somebody necessarily assist to make critically posts I might state. This is the very first time I frequented your web page and to this point? I amazed with the analysis you made to create this particular publish incredible. Excellent task!

Reply

Gerardo Claro
Gerardo Claro United States
11/10/2018 9:20:18 PM #

Can I simply say what a aid to find somebody who actually knows what theyre speaking about on the internet. You definitely know learn how to convey a difficulty to mild and make it important. More people have to read this and understand this facet of the story. I cant imagine youre no more popular since you undoubtedly have the gift.

Reply

Joey Puffenberger
Joey Puffenberger United States
11/11/2018 8:37:08 PM #

Greetings from Los angeles! I'm bored to tears at work so I decided to check out your blog on my iphone during lunch break. I love the knowledge you provide here and can't wait to take a look when I get home. I'm shocked at how fast your blog loaded on my mobile .. I'm not even using WIFI, just 3G .. Anyways, fantastic site!

Reply

Jacob Carilli
Jacob Carilli United States
11/12/2018 7:05:19 PM #

Wanted to say this blog is quite good. I always want to hear something totally new about this because We have the same blog in my Country about this subject so this help´s me a lot. I did looking about the issue and located a large amount of blogs but nothing beats this. Thanks for sharing so much inside your blog..

Reply

Susy Kusterer
Susy Kusterer United States
11/15/2018 11:24:40 PM #

Wonderful article, thank you for sharing the info. It isn’t too often that you simply read articles where the poster understands what they’re blogging about. Grammar as well as punctuational are spot on too, only trouble I seemed to have had been bringing up the site, appeared sluggish. Looks like other visitors experienced the same trouble?

Reply

Marvel Manganiello
Marvel Manganiello United States
11/16/2018 11:45:44 PM #

hello there and thanks for your information – I have certainly picked up anything new from proper here. I did alternatively experience several technical points the use of this web site, since I skilled to reload the website lots of times prior to I may just get it to load properly. I have been puzzling over in case your web hosting is OK? Now not that I'm complaining, however slow loading circumstances instances will sometimes impact your placement in google and can damage your high-quality rating if ads and ***********|advertising|advertising|advertising and *********** with Adwords. Anyway I’m adding this RSS to my e-mail and could glance out for much extra of your respective exciting content. Ensure that you replace this again very soon..

Reply

Jasper Greenlaw
Jasper Greenlaw United States
11/17/2018 1:20:37 PM #

I've been browsing online more than three hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my view, if all webmasters and bloggers made good content as you did, the web will be a lot more useful than ever before.

Reply

Sima Aiello
Sima Aiello United States
11/17/2018 9:22:56 PM #

I found your blog site on google and check a couple of of your early posts. Continue to maintain up the excellent operate. I simply additional up your RSS feed to my MSN Information Reader. Seeking forward to studying more from you afterward!…

Reply

Julius Klan
Julius Klan United States
11/17/2018 10:47:14 PM #

Its like you read my mind! You appear to grasp a lot approximately this, such as you wrote the ebook in it or something. I feel that you simply could do with some p.c. to pressure the message home a little bit, however other than that, this is fantastic blog. A great read. I'll definitely be back.

Reply

Grisel Meemken
Grisel Meemken United States
11/18/2018 9:10:17 AM #

magnificent put up, very informative. I'm wondering why the opposite experts of this sector don't understand this. You should continue your writing. I am sure, you have a great readers' base already!

Reply

Odette Gonyou
Odette Gonyou United States
11/18/2018 12:03:30 PM #

Usually I don't read article on blogs, but I would like to say that this write-up very forced me to try and do so! Your writing style has been surprised me. Thanks, very nice article.

Reply

Willard Lyell
Willard Lyell United States
11/18/2018 3:16:12 PM #

Useful information. Lucky me I discovered your website by accident, and I'm stunned why this accident didn't happened in advance! I bookmarked it.

Reply

Add comment

biuquote
  • Comment
  • Preview
Loading

Rel=Me

Month List

RecentComments

Comment RSS