Geeky
As a developer in the .NET world where LINQ is first class citizen, when going to JavaScript it seems that some methods are missing. There is even a few libraries that tries to remedy this, but if you are just looking to get the job done the most often used methods are right at hand. (Looking for features such as Lazy-evaluation or observables, linq.js and RxJS offers these).
In the following I’ll list the most used operations and their JavaScript equivalents cheat sheet style. Notice that I’m using Lambda expressions in JavaScript to get the code a bit more concise, if they aren’t available just replace expressions like:
(data) => {}
with
function(data) {}
All operations are executed against this example:
var persons = [ { firstname: 'Peter', lastname: 'Jensen', type: 'Person', age: 30 }, { firstname: 'Anne', lastname: 'Jensen', type: 'Person', age: 50 }, { firstname: 'Kurt', lastname: 'Hansen', type: 'Person', age: 40 } ];
Notice that some of the operations modifies the source array, if you don’t want that just clone it with:
persons.slice(0);
Every operation is named after the method’s name in LINQ:
var result = persons.All(person => person.type == "Person");
var result = persons.filter(person => person.type == 'Person').length == persons.length;
var result = persons.Concat(persons);
var result = persons.concat(persons);
var result = persons.Count();
var result = persons.length;
var lastnames = persons.Select(person => person.lastname); var result = lastnames.Distinct();
var lastnames = persons.map(person => person.lastname); var result = lastnames.filter((value, index) => lastnames.indexOf(value) == index);
var result = Enumerable.Empty<dynamic>();
var result = [];
var result = persons.First();
var result = persons[0]; if (!result) throw new Error('Expected at least one element to take first')
var result = persons.FirstOrDefault();
var result = persons[0];
var fullnames = new List<string>(); persons.ForEach(person => fullnames.Add(person.firstname + " " + person.lastname));
var fullnames = []; persons.forEach(person => fullnames.push(person.firstname + ' ' + person.lastname))
var result = persons.GroupBy(person => person.lastname);
var result = persons.reduce((previous, person) => { (previous[person.lastname] = previous[person.lastname] || []).push(person); return previous; }, []);
var result = persons.IndexOf(persons[2]);
var result = persons.indexOf(persons[2]);
var result = persons.Last();
var result = persons[persons.length-1]; if (!result) throw new Error('Expected at least one element to take last')
var result = persons.LastOrDefault();
var result = persons[persons.length-1];
var result = persons.OrderBy(person => person.firstname);
persons.sort((person1, person2) => person1.firstname.localeCompare(person2.firstname));
var result = persons.OrderByDescending(person => person.firstname);
persons.sort((person1, person2) => person2.firstname.localeCompare(person1.firstname));
persons.Reverse();
var result = persons.reverse();
var result = persons.Select(person => new {fullname = person.firstname + " " + person.lastname});
var result = persons.map(person => ({ fullname: person.firstname + ' ' + person.lastname }) );
var result = persons.Single(person => person.firstname == "Peter");
var onePerson = persons.filter(person => person.firstname == "Peter"); if (onePerson.length != 1) throw new Error('Expected at excactly one element to take single') var result = onePerson[0];
var result = persons.Skip(2);
var result = persons.slice(2, persons.length);
var result = persons.Take(2);
var result = persons.slice(0, 2);
var result = persons.Where(person => person.lastname == "Jensen");
var result = persons.filter(person => person.lastname == 'Jensen');
So clearly you don’t need a library if you only need the basic operations. The code above with tests is available at GitHub.
This post is also available in Danish at QED.dk.
The last year we have been travelling a lot. We have visited Vietnam, Thailand, Malaysia, The Netherlands and Spain and even managed to stay a few months in our home country Denmark as well. In that time things have been crazy busy (but then again – everybody seems busy these days to a point where it doesn’t mean anything to say you’re busy).
Travelling takes a lot of time, but we have also found time to start up our own business, Monzoom, doing consultant work for a couple of Danish companies (e.g. internationally known Danish toy company known for “blocks” – you know who I mean, right?) and we have found time to make our first product xiive. I’m really scratching my own itch with xiive – it’s a social media filtering site with special emphasis on how much a topic is mentioned (seen over time) and comparing these numbers with those of other topics.
There are already many such sites, but the special thing about xiive is that we did not follow the traditional model of letting the user choose x topics (usually 3) to track in private. We have chosen instead to make all the data public so it can be shared, embedded, compared and discussed.
We are currently in private beta, and I can’t wait to show the site to the world. You can sign up for an invite here if you want, but I have to warn you – we don’t really like those “viral” beta invite sites, so you will not get in front of the line by inviting your friends. We think you should only call on your social network if you really mean it and not to get special treatment.
Today we launched a new landing page and I think it is quite the improvement, but you be the judge of that. Here is the old landing page and the new landing page:
Right now we sit in a hotel apartment in Bangkok (no flood in sight here, but some areas are badly hit). We have just been in southern Thailand for a month and we are going to stay in Bangkok for a month and then go home to Denmark for Christmas.
Life is good!
Reading through the YOW! 2010 conference program I was reminded of the rallying cry “more cowbell”. The evening keynote on the first day, 50 in 50 by Guy Steele and Richard Gabriel, really brings back memories. I first saw this keynote a few years ago at JAOO Aarhus 2008 and I was in awe of the artful presentation of so many programming languages! It’s not every day you see Guy Steele rapping.
Fortunately it was taped and the video is here:
How many of the programming languages did you know?
Just wrapping up the last things about the JAOO conference these days. During JAOO I did an interview with JAOO speaker and Android developer Stefan Meisner Larsen and now it is on InformIT.
Just to give you a taste:
TH: If you could improve the Android platform, what would be your first priority?
SML: Mobile devices have great potential in an enterprise setting as communication and application platforms. But there are challenges with administration and security that the Android platform does not address. I miss those systems that exist today for administration of PCs for the Android platform. There are undoubtedly a lot of obvious applications for mobile devices that will never make it because of security issues.
If this sounds interesting to you, you can read the whole interview at the InformIT site.
P.S. Google Android turned 3 today.
The other evening I was at a party with a lot of other geeks at Geekhouse in Aarhus. It was an evening of lightning talks by the Aarhus geek community followed by free beer, wine and fun networking time (exactly what I’m going to miss the most when I leave Aarhus).
Being a girl at these events usually means you are the odd one out, which doesn’t bother me. It also means that sometimes what you get the most excited about is not the same as what the guys get excited about. What I remember most from that evening was a demonstration of a new brand of covers and handbags designed for women with gadgets.
My favorite item they have made is this simple and elegant iPhone cover made in really fine materials with a soft leather lining THAT POLISHES YOUR IPHONE. I know most men don’t care about the greasy fingerprints on their smart phone but as a woman it bothers me and this is such a simple solution.
One of the guys behind this new brand saw that I was falling in love with this cover and he gave me one as a gift. Oh, how I love being the odd one out :-). This brand is only sold in USA so here in Denmark it’s a rare item.
P.S. You could speculate that I’m just writing this blog post so I could get one of these as a gift. This is not the case. I did get one for free but I write this blog post because I like the brand, not because the brand told me to. And yes, I stole the product photos from the coverlicious website.
Most of the time startups focus on getting investors and optimizing for how much money they/we get out of it. But is that really the best strategy?
Github founder Tom Preston-Werner doesn’t think so. Github has never taken founding from VCs or Angel investors. Tom believes that you should optimize for happiness instead and actually that could also have a positive effect on the money part of things. If you build it they will come…
I recently found a video of Tom giving a presentation at startup school. I really likes his way of thinking:
Watch live video from c3oorg on Justin.tv
I was (as I previously mentioned) at a talk by Tom Preston-Werner a few weeks ago at JAOO about Github and Git. He is a really great speaker and his star in my book did not fade because he and the other github-people sponsered a drinkup during JAOO. I got to talk to some really interesting people and that lead to an idea that I’m going to work on in the future. It seems like what Tom is talking about in the video really works.
My main takeaways from Toms presentation: creating win-win situations, helping luck through proximity and being creative in not paying for things. It seems like I have been on the right path all along ;-).
If you have to chance to hear Tom present, don’t miss it. He is a laid-back, really cool guy.
I just have to share this – Bill Gates having an affair.
It is not my favorite strip from Wulff-Morgenthaler though – this one about computer scientists is.
Last week I was at the JAOO conference – the last JAOO conference ever. The conference runs from Monday-Wednesday with training days at Sunday and Thursday-Friday. On Monday the JAOO organizing team had a surprise for all attendees: the conference changed it’s name to GOTO (in the logo it’s written goto;). The next day all the JAOO banners were gone and big GOTO-banners had replaced them – and now the conference is PINK. A very bold move for a techie conference, but then again so is the new name GOTO (the jokes are endless: goto considered harmful is the most obvious).
I have been part of the JAOO conference for a few years now and I guess now all my conference-stuff with the JAOO logo is going to be worth a lot of money on Ebay for all the true JAOO fans – or maybe not :-). My favorite thing is these JAOO green shoes (with the JAOO logo on the toes) that is really nice to wear even for a whole conference.
JAOO is dead – long live GOTO!
So far all my code versioning needs has been covered by CVS and SVN, but recently I keep hearing about distributed version control. Git and Mercurial are very popular topics in certain parts of my social circle, so I decided that I need to look into the topic (some very painful personal experience with a large programming project on Subversion also had some motivational influence on that decision).
So where to start?
I decided to start looking into Git. This was not a deliberate deselection of Mercurial but motivated by my attendance at the IT-conference JAOO in a months time and at JAOO Scott Chacon from GitHub will host a Git 101 tutorial, which I am considering attending (oh, and one of the GitHub founders, Tom Preston-Werner, will do a presentation called “Mastering Git Basics”, which also could be interesting).
My starting point was wikipedia and as it mentions that Git is designed and originally developed by Linus Thorvalds, I googled Git together with his name to see what he had to say about the subject. The result was this Tech talk from Google, that I quite enjoyed:
Then I told my twitter friends that am looking into Git and as always they give great feedback. My favorite was from jlouis666, who pointed me to a page with the Pro Git book by aforementioned Scott Chacon (the guy hosting a Git tutorial at JAOO), which is available online. I’m hoping to look into it before his tutorial.
So far I have not had time to really play with Git. Because all my existing projects are in SVN and migrating between two such different systems would probably be unwise/a pain I am saving my practical experience with Git for my next project. I hope that these things I am looking into will prepare me for the practical experience, but any advise you have could really help too…
After JAOO I plan to look into Mercurial/hg – If you know of good sources of information for that I would love to hear about it. (I guess with all the Git-stuff at JAOO they didn’t have any time slots left for Mercurial or maybe they have just chosen their favorite…)
Sometimes you have to remember to do something good for your blog readers (and twitter followers) and because I have been behind the scenes at JAOO the last few years, I was able to negotiate a good discount for the JAOO IT-conference.
So if you want to go to JAOO in Aarhus and hear some great speakers like Tim Bray, Oren Eini (Ayende), Dan North, Erik Meijer, James Gosling, Mary Poppendieck, Michael Nygard, Dan Ingalls, Dave Thomas and many more, you can get a 20 % discount using the discount code “jaoovip2010” when signing up.
If you have questions about going to the conference, feel free to ask. You can leave a comment on this blog or write to me on twitter @qedtherese.