Why learn new programming languages

3,052 views

Published on

A presentation sharing my pe

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,052
On SlideShare
0
From Embeds
0
Number of Embeds
100
Actions
Shares
0
Downloads
15
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Why learn new programming languages

  1. 1. WHY LEARN NEWPROGRAMMING LANGUAGES?What I’ve learned from Ruby and Scheme thatapplies to my daily work in C# and JavaScript Trondheim XP Meetup Jonas Follesø, Scientist/Manager BEKK Trondheim 08/02/2012
  2. 2. 3
  3. 3. PERSONAL HISTORY OF PROGRAMMING LANGUAGES 4 QBasic Turbo Pascal Delphi VBScript (1996) (1998) (1999) (2001) C# JavaScript Java Python Scheme Ruby (2002) (2003) (2004) (2005) (2006) (2010)
  4. 4. PERSONAL HISTORY OF PROGRAMMING LANGUAGES 5 QBasic Turbo Pascal Delphi VBScript (1996) (1998) (1999) (2001) C# JavaScript Java Python Scheme Ruby (2002) (2003) (2004) (2005) (2006) (2010)
  5. 5. HIGHER ORDER FUNCTIONS 6 Formulating Abstractions with Higher-Order Functions
  6. 6. ABSTRACTIONS THROUGH FUNCTIONS 7(* 3 3 3) ; outputs 27(define (cube x) (* x x x))(cube 3) ; outputs 27
  7. 7. ABSTRACTIONS THROUGH FUNCTIONS 8(define (sum-int a b) (if (> a b) 0 (+ a (sum-int (+ a 1) b))))(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))
  8. 8. ABSTRACTIONS THROUGH FUNCTIONS 9(define (sum-int a b) (if (> a b) 0 (+ a (sum-int (+ a 1) b))))(define (sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes (+ a 1) b))))
  9. 9. ABSTRACTIONS THROUGH FUNCTIONS 10(define (<name> a b) (if (> a b) 0 (+ <term> (<name> (<next> a) b))))
  10. 10. ABSTRACTIONS THROUGH FUNCTIONS 11(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b))))(define (inc n) (+ n 1))(define (identity n) n)(define (sum-int a b) (sum identity a inc b))(define (sum-cubes a b) (sum cube a inc b))(sum-int 0 10) ; outputs 55(sum-cubes 0 10) ; outputs 3025
  11. 11. ABSTRACTIONS THROUGH FUNCTIONS 12(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b))))(define (inc n) (+ n 1))(define (identity n) n)(define (sum-int a b) (sum identity a inc b))(define (sum-cubes a b) (sum cube a inc b))(sum-int 0 10) ; outputs 55(sum-cubes 0 10) ; outputs 3025
  12. 12. ABSTRACTIONS THROUGH FUNCTIONS - JAVASCRIPT 13$("li").click(function() { $(this).css({color:yellow});});$("li").filter(function(index) { return index % 3 == 2;}).css({color:red});$.get("/some-content", function(data) { // update page with data...});
  13. 13. ABSTRACTIONS THROUGH FUNCTIONS – C# 14SumEvenCubes(1, 10); // outputs 1800public int SumEvenCubes() { var num = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; return num .Where(IsEven) .Select(Cube) .Aggregate(Sum);}public bool IsEven(int n) { return n % 2 == 0; }public int Cube(int n) { return n*n*n; }public int Sum(int a, int b) { return a + b; }
  14. 14. OBJECTS FROM CLOSURES 15 You don’t need classes to create objects
  15. 15. OBJECTS FROM CLOSURES IN SCHEME 16(define (make-account balance) (define (withdraw amount) (if (>= balance amount) (set! balance (- balance amount)) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch m . args) (case m ((withdraw) (apply withdraw args)) ((deposit) (apply deposit args)))) dispatch)(define account1 (make-account 100))(account1 withdraw 50)(account1 deposit 25) ; outputs 75
  16. 16. OBJECTS FROM CLOSURES IN JAVASCRIPT 17var make_account = function (balance) { var withdraw = function (amount) { if (balance >= amount) balance -= amount; else throw "Insufficient funds"; return balance; }; var deposit = function (amount) { balance += amount; return balance; }; return { withdraw: withdraw, deposit: deposit };};var account1 = make_account(100);account1.withdraw(50);account1.deposit(25); // outputs 75
  17. 17. READABILITY 18 Extreme emphasis on expressiveness
  18. 18. READABILITY 19 “[…] we need to focus on humans, on how humans care about doing programming or operating the application of the machines. We are the masters. They are the slaves…”
  19. 19. EXAMPLES OF RUBY IDIOMS 20abort unless str.include? "OK"x = x * 2 until x > 1003.times { puts "olleh" }10.days_ago2.minutes + 30.seconds1.upto(100) do |i| puts iend
  20. 20. EXAMPLE OF RSPEC TEST 21describe Bowling do it "should score 0 for gutter game" do bowling = Bowling.new 20.times { bowling.hit(0) } bowling.score.should == 0 endend
  21. 21. C# UNIT TEST INFLUENCED BY RSPEC 22public class Bowling_specs { public void Should_score_0_for_gutter_game() { var bowling = new Bowling(); 20.Times(() => bowling.Hit(0)); bowling.Score.Should().Be(0); }}public static class IntExtensions { public static void Times(this int times, Action action) { for (int i = 0; i <= times; ++i) action(); }}
  22. 22. C# FLUENT ASSERTIONS 23string actual = "ABCDEFGHI";actual.Should().StartWith("AB") .And.EndWith("HI") .And.Contain("EF") .And.HaveLength(9);IEnumerable collection = new[] { 1, 2, 3 };collection.Should().HaveCount(4, "we put three items in collection"))collection.Should().Contain(i => i > 0);
  23. 23. READABILITY 24 Internal DSLs and Fluent Interfaces
  24. 24. RUBY DSL EXAMPLES - MARKABY 25
  25. 25. RUBY DSL EXAMPLES – ENUMERABLE PROXY API 26
  26. 26. C# LINQ EXAMPLE 27static void Main(string[] args){ var people = new[] { new Person { Age=28, Name = "Jonas Follesø"}, new Person { Age=25, Name = "Per Persen"}, new Person { Age=55, Name = "Ole Hansen"}, new Person { Age=40, Name = "Arne Asbjørnsen"}, }; var old = from p in people where p.Age > 25 orderby p.Name ascending select p; foreach(var p in old) Console.WriteLine(p.Name);}---Arne AsbjørnsenJonas FollesøOle Hansen
  27. 27. C# STORYQ EXAMPLE 28[Test]public void Gutter_game(){ new Story("Score calculation") .InOrderTo("Know my performance") .AsA("Player") .IWant("The system to calculate my total score") .WithScenario("Gutter game") .Given(ANewBowlingGame) .When(AllOfMyBallsAreLandingInTheGutter) .Then(MyTotalScoreShouldBe, 0) .WithScenario("All strikes") .Given(ANewBowlingGame) .When(AllOfMyBallsAreStrikes) .Then(MyTotalScoreShouldBe, 300) .ExecuteWithReport(MethodBase.GetCurrentMethod());}
  28. 28. C# OBJECT BUILDER PATTERN 29[Test]public void Should_generate_shipping_statement_for_order(){ Order order = Build .AnOrder() .ForCustomer(Build.ACustomer()) .WithLine("Some product", quantity: 1) .WithLine("Some other product", quantity: 2); var orderProcessing = new OrderProcessing(); orderProcessing.ProcessOrder(order);}
  29. 29. MONKEY PATCHING 30 Runtime generation of code and behaviour
  30. 30. RUBY ACTIVE RECORD AND METHOD MISSING 31
  31. 31. C# DYNAMIC AND EXPANDO OBJECT 32static void Main(string[] args){ dynamic person = new ExpandoObject(); person.Name = "Jonas Follesø"; person.Age = 28; Console.WriteLine("{0} ({1})", person.Name, person.Age);}Jonas Follesø (28)
  32. 32. C# MONKEY PATCHING 33public class DynamicRequest : DynamicObject{ public override bool TryGetMember(GetMemberBinder binder, out object result) { string key = binder.Name; result = HttpContext.Current.Request[key] ?? ""; return true; }}@using MvcApplication1.Controllers@{ Page.Request = new DynamicRequest();}<h4>Hello @Page.Request.Name</h4><h4>Hello @HttpContext.Current.Request["Name"]</h4>
  33. 33. C# MICRO ORMS 34• Afrer C# 4.0 with support for dynamic typing a whole new category of micro ORMs has emerged: • Massive • Simple.Data • Peta Pico • Dapper
  34. 34. C# MICRO ORMS 35IEnumerable<User> u = db.Users.FindAllByName("Bob").Cast<User>();db.Users.FindByNameAndPassword(name, password)// SELECT * FROM Users WHERE Name = @p1 and Password = @p2db.Users.FindAllByJoinDate("2010-01-01".to("2010-12-31"))// SELECT * FROM [Users] WHERE [Users].[JoinDate] <= @p1
  35. 35. SAPIR–WHORF HYPOTHESIS 36 The principle of linguistic relativity holds that the structure of a language affects the ways in which its speakers are able to conceptualize their world, i.e. their world view. http://en.wikipedia.org/wiki/Linguistic_relativity
  36. 36. 37TAKK FOR MEG Jonas Follesø

×