who needs c++ when you have d and go

20
Copyright © 2013 Russel Winder 1 Who Needs C++ When You Have D and Go? Russel Winder email: [email protected] xmpp: [email protected] twitter: @russel_winder Web: http://www.russel.org.uk

Upload: russel-winder

Post on 29-Nov-2014

2.811 views

Category:

Technology


1 download

DESCRIPTION

Slides from my ACCU 2013 lightning talk. D is the real winner as the functions work out of the box. The Go code requires lots of extra code. Until std::range exists it is the loser.

TRANSCRIPT

Page 1: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 1

Who Needs C++ WhenYou Have D and Go?

Russel Winder

email: [email protected]: [email protected]

twitter: @russel_winderWeb: http://www.russel.org.uk

Page 2: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 2

controversy noun dispute, argument, or debate, especially one concerning a matter about which there is strong disagreement

Page 3: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 3

Calculate the sum of the squares of the numbers between 0 and 100 that are

divisible by 7.

Page 4: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 4

int sequential_iterative() { auto sum = 0; foreach (i; 0 .. 100) { if (i % 7 == 0) { sum += i * i; } } return sum;}

Page 5: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 5

func sequential_iterative() (sum int) {for i := 0; i < 100; i++ {

if (i % 7 == 0) {sum += i * i

}}return

}

Page 6: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 6

int sequential_iterative() { auto sum = 0; for (auto i = 0; i < 100; ++i) { if (i % 7 == 0) { sum += i * i; } } return sum;}

Page 7: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 7

So all native code programminglanguages are the same?

Page 8: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 8

We are, of course, expectedto be more declarative these days…

Page 9: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 9

Possibly even functional…

Page 10: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 10

reduce!"a + b"(map!"a * a"(filter!"a % 7 == 0"(iota(100))))

Page 11: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 11

functools.Inject(func (a, b int) int { return a + b },functools.Collect(func (i int) int { return i * i },

functools.Filter(func (i int) bool { return i % 7 == 0 },make([]int, 100))))

Page 12: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 12

int sequential_declarative() { std::vector<int> numbers (100); std::iota(numbers.begin(), numbers.end(), 1); std::list<int> filtered; std::copy_if(numbers.begin(), numbers.end(), std::back_insert_iterator<std::list<int>>(filtered), [=] (int i) { return i % 7 == 0; }); std::vector<int> squares (filtered.size()); std::transform(filtered.begin(), filtered.end(), std::back_insert_iterator<std::vector<int>>(squares), [=] (int i) { return i * i;}); return std::accumulate(squares.begin(), squares.end(), 0, [=] (int i, int j) { return i + j;});}

Page 13: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 13

What about being fluent…

Page 14: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 14

iota(100).filter!"a % 7 == 0"().map!"a * a"().reduce!"a + b"()

Page 15: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 15

functools.IntSlice(make([]int, 100)).Filter(func (i int) bool { return i % 7 == 0 }).

Collect(func (i int) int { return i * i }).Inject(func (a, b int) int { return a + b })

Page 16: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 16

?

Page 17: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 17

Execute it!

Page 18: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 18

Use D

Use Go

Use Python

Anything but C++

Page 19: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 19

http://www.dlang.org

http://www.go-lang.org

Page 20: Who needs C++ when you have D and Go

Copyright © 2013 Russel Winder 20

Who Needs C++ WhenYou Have D and Go?

Russel Winder

email: [email protected]: [email protected]

twitter: @russel_winderWeb: http://www.russel.org.uk