SlideShare a Scribd company logo
Except where otherwise noted, this work is licensed under: http:
//creativecommons.org/licenses/by-nc-sa/3.0/
The polyglot programmer
Leganés, feb 11, 12
David Muñoz
@voiser
objective:
qualitative description of languages
let’s focus on some simple concepts
style (structured / OO / prototype / functional / ...)
typing (static / dynamic / strong / weak)
execution model (high / low level, native, vm, thread safety, ...)
difference between syntax, semantics, idioms, libraries and tools
C (1972) - structured
#include <stdio.h>
void update(int i) {
i = i + 1;
}
int main() {
int i = 0;
println(“i = %dn”, i); // i = 0
update(i);
println(“i = %dn”, i); // i = ?
char * str = (char*)i; // ?
return 0;
}
hero
C (1972) - structured
#include <stdio.h>
void update(int i) {
i = i + 1;
}
int main() {
int i = 0;
println(“i = %dn”, i); // i = 0
update(i);
println(“i = %dn”, i); // i = ?
char * str = (char*)i; // ?
return 0;
}
master
static typing
pass by value
it’s a high level assembly!
weak type system
C (1972) - structured
0000000000400506 <update>:
400506: 55 push %rbp
400507: 48 89 e5 mov %rsp,%rbp
40050a: 89 7d fc mov %edi,-0x4(%rbp)
40050d: 83 45 fc 01 addl $0x1,-0x4(%rbp)
400511: 5d pop %rbp
400512: c3 retq
0000000000400513 <main>:
400513: 55 push %rbp
400514: 48 89 e5 mov %rsp,%rbp
400517: 48 83 ec 10 sub $0x10,%rsp
40051b: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp)
400522: 8b 45 fc mov -0x4(%rbp),%eax
400525: 89 c6 mov %eax,%esi
400527: bf e4 05 40 00 mov $0x4005e4,%edi
40052c: b8 00 00 00 00 mov $0x0,%eax
400531: e8 aa fe ff ff callq 4003e0 <printf@plt>
400536: 8b 45 fc mov -0x4(%rbp),%eax
400539: 89 c7 mov %eax,%edi
40053b: e8 c6 ff ff ff callq 400506 <update>
400540: 8b 45 fc mov -0x4(%rbp),%eax
400543: 89 c6 mov %eax,%esi
400545: bf e4 05 40 00 mov $0x4005e4,%edi
40054a: b8 00 00 00 00 mov $0x0,%eax
40054f: e8 8c fe ff ff callq 4003e0 <printf@plt>
400554: b8 00 00 00 00 mov $0x0,%eax
400559: c9 leaveq
40055a: c3 retq
40055b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
god of metal
C++ (1983) - Object oriented
#include <iostream>
using namespace std;
class Point
{
public:
float _x, _y;
Point(float x, float y)
: _x(x), _y(y) {}
};
ostream &operator<<
(ostream &os, Point const &p)
{
return os <<
"Point(" << p._x << "," << p._y << ")";
}
void update(Point p) {
p._x += 1;
p._y += 1;
}
int main() {
Point a(1, 2);
cout << a << endl; // Point(1, 2)
update(a);
cout << a << endl; // Point(?, ?)
char * str = (char*)a; // ?
char * str = (char*)&a; // ?
return 0;
}
C++ (1983) - Object oriented
#include <iostream>
using namespace std;
class Point
{
public:
float _x, _y;
Point(float x, float y)
: _x(x), _y(y) {}
};
ostream &operator<<
(ostream &os, Point const &p)
{
return os <<
"Point(" << p._x << "," << p._y << ")";
}
void update(Point p) {
p._x += 1;
p._y += 1;
}
int main() {
Point a(1, 2);
cout << a << endl;
update(a);
cout << a << endl;
char * str = (char*)a;
char * st2 = (char*)&a;
return 0;
}
static typing
pass by value (also by ref,
not shown in this
example)
a little bit higher level than assembly
weak type system
namespaces
(operator) overloading
module Example where
data Point a = Point a a deriving (Show)
update (Point x y) = Point (x+1) (y+1)
applyTwice f x = f (f x)
main =
let
a = Point 1 1
b = applyTwice update a
b = update b -- ?
in
print b
Haskell (1990) - functional
module Example where
data Point a = Point a a deriving (Show)
update (Point x y) = Point (x+1) (y+1)
applyTwice f x = f (f x)
main =
let
a = Point 1 1
b = applyTwice update a
b = update b -- ?
in
print b
Haskell (1990) - functional
data type and constructor
strong, static typing, type inference
looks and feels different. Don’t take a look at the native code generated.
immutability
Let’s give it a try
Python (1991)
describe me!
Python (1991) code example
class AgentManager:
def add(self, name, source, sender=None, locale="en"):
""" Add an agent to the list. """
self.set_locale(locale)
if not self.has_permissions(sender):
print(MSG_NO_PERM)
return self.feedback(MSG_NO_PERM, sender)
alist = self.read_list()
$ python3
...
>>> 1 + "2"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> a = 1
>>> a = "this is a string"
https://github.com/voiser/zoe-startup-kit/blob/master/agents/zam/zam.py
Python (1991)
high level
object oriented
dynamic, strong typing
vm, gc
pretty*syntax, significant whitespace
huge community
(probably there’s a python library for that!)
CPython / Jython / PyPy / IronPython
what if I describe you a language without
letting you see actual code?
high level
object oriented
vm jit, gc
static, explicit, strong* typing
(messy) generic types
simple*, verbose, bureaucratic
enormous community
Java - let me break your strong type system
import java.util.List;
public class T3chFest {
private Object[] stuff;
private List<Object> rooms;
public T3chFest(String[] talks, List<String> rooms) {
this.stuff = talks;
stuff[0] = Thread.currentThread(); // ?
this.rooms = rooms; // ?
List rooms2 = rooms; // ?
this.rooms = rooms2; // ?
}
}
~ java + ML
functional + object oriented
jvm
static, strong rock-solid adamantium-like
heavier-than-the-gods-of-metal typing
nice* syntax with (partial) type inference
A language build on its type system
into JVM? learn. it. now.
Scala (2003)
scala> val a = Array("a", "b", "c")
a: Array[String] = Array(a, b, c)
scala> val b: Array[Object] = a
<console>:11: error: type mismatch;
found : Array[String]
required: Array[Object]
Note: String <: Object, but class Array is invariant in type T.
Scala (2003) - fun with invariance
scala> class Animal;
scala> class Mammal extends Animal;
scala> class Human extends Mammal;
scala> class Group[ +A]
scala> def sizeof(g: Group[ Animal]) = ...
scala> sizeof(new Group[ Animal]())
scala> sizeof(new Group[ Mammal]())
scala> class Veterinary[ -A]
scala> def treat(g: Group[Mammal], v: Veterinary[ Mammal]) = ...
scala> treat(..., new Veterinary[ Mammal]())
scala> treat(..., new Veterinary[ Animal]())
scala> class Treatment[ T <: Mammal]
scala> def cure[B](x: Group[ B]) : Treatment[ B] = …
error: type arguments [B] do not conform to class Treatment's type parameter bounds [T
<: Mammal]
scala> def cure[ B <: Mammal](x: Group[B]) : Treatment[ B] = ...
Scala - basic types
typeclasses
structural types (a.k.a. duck typing)
refined types
self-recursive types
path-dependent types
...
tip of the iceberg
The recruiter’s infinite knowledge
Why are you interested in Scala, when
Java 8 already has lambdas?
Except where otherwise noted, this work is licensed under: http:
//creativecommons.org/licenses/by-nc-sa/3.0/
Leganés, feb 11, 12
Thanks!

More Related Content

PDF
GUL UC3M - Introduction to functional programming
PDF
The best language in the world
PDF
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
PDF
Functional Algebra: Monoids Applied
PDF
First-Class Patterns
PDF
Java Class Design
PDF
MTL Versus Free
PPTX
Lambda выражения и Java 8
GUL UC3M - Introduction to functional programming
The best language in the world
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Functional Algebra: Monoids Applied
First-Class Patterns
Java Class Design
MTL Versus Free
Lambda выражения и Java 8

What's hot (20)

PDF
Python Performance 101
PDF
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
PDF
The Design of the Scalaz 8 Effect System
PDF
Halogen: Past, Present, and Future
PPTX
Scala - where objects and functions meet
PDF
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
PDF
Why Haskell
PDF
Futures e abstração - QCon São Paulo 2015
PDF
Demystifying functional programming with Scala
PDF
The Death of Final Tagless
PDF
Pragmatic Real-World Scala (short version)
PDF
ES6 - Next Generation Javascript
PDF
Post-Free: Life After Free Monads
PPT
Collection Core Concept
PPT
Profiling and optimization
PDF
Design Patterns - Compiler Case Study - Hands-on Examples
PPT
Stack queue
PDF
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
PPTX
Joy of scala
PDF
RESTful API using scalaz (3)
Python Performance 101
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
The Design of the Scalaz 8 Effect System
Halogen: Past, Present, and Future
Scala - where objects and functions meet
"Немного о функциональном программирование в JavaScript" Алексей Коваленко
Why Haskell
Futures e abstração - QCon São Paulo 2015
Demystifying functional programming with Scala
The Death of Final Tagless
Pragmatic Real-World Scala (short version)
ES6 - Next Generation Javascript
Post-Free: Life After Free Monads
Collection Core Concept
Profiling and optimization
Design Patterns - Compiler Case Study - Hands-on Examples
Stack queue
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Joy of scala
RESTful API using scalaz (3)
Ad

Similar to T3chFest 2016 - The polyglot programmer (20)

PDF
Refactoring to Macros with Clojure
PPT
Os Vanrossum
PPT
sonam Kumari python.ppt
PDF
Low Level Exploits
PPT
Groovy Introduction - JAX Germany - 2008
PPTX
python programming internship presentation.pptx
PDF
Generic Functional Programming with Type Classes
PDF
Java VS Python
DOCX
C cheat sheet for varsity (extreme edition)
PDF
A Few of My Favorite (Python) Things
PDF
Data Analysis with R (combined slides)
PPTX
Things about Functional JavaScript
PDF
Scala @ TechMeetup Edinburgh
PDF
Class 12 computer sample paper with answers
PPT
IronPython and Dynamic Languages on .NET by Mahesh Prakriya
PDF
Introduction to clojure
PDF
Pydiomatic
PDF
Python idiomatico
PPT
python language programming presentation
PPTX
Behavior driven oop
Refactoring to Macros with Clojure
Os Vanrossum
sonam Kumari python.ppt
Low Level Exploits
Groovy Introduction - JAX Germany - 2008
python programming internship presentation.pptx
Generic Functional Programming with Type Classes
Java VS Python
C cheat sheet for varsity (extreme edition)
A Few of My Favorite (Python) Things
Data Analysis with R (combined slides)
Things about Functional JavaScript
Scala @ TechMeetup Edinburgh
Class 12 computer sample paper with answers
IronPython and Dynamic Languages on .NET by Mahesh Prakriya
Introduction to clojure
Pydiomatic
Python idiomatico
python language programming presentation
Behavior driven oop
Ad

Recently uploaded (20)

PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Welding lecture in detail for understanding
PPTX
additive manufacturing of ss316l using mig welding
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Construction Project Organization Group 2.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Structs to JSON How Go Powers REST APIs.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Embodied AI: Ushering in the Next Era of Intelligent Systems
Mechanical Engineering MATERIALS Selection
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Foundation to blockchain - A guide to Blockchain Tech
Arduino robotics embedded978-1-4302-3184-4.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Welding lecture in detail for understanding
additive manufacturing of ss316l using mig welding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Model Code of Practice - Construction Work - 21102022 .pdf
Construction Project Organization Group 2.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
bas. eng. economics group 4 presentation 1.pptx
Structs to JSON How Go Powers REST APIs.pdf

T3chFest 2016 - The polyglot programmer

  • 1. Except where otherwise noted, this work is licensed under: http: //creativecommons.org/licenses/by-nc-sa/3.0/ The polyglot programmer Leganés, feb 11, 12 David Muñoz @voiser
  • 3. let’s focus on some simple concepts style (structured / OO / prototype / functional / ...) typing (static / dynamic / strong / weak) execution model (high / low level, native, vm, thread safety, ...) difference between syntax, semantics, idioms, libraries and tools
  • 4. C (1972) - structured #include <stdio.h> void update(int i) { i = i + 1; } int main() { int i = 0; println(“i = %dn”, i); // i = 0 update(i); println(“i = %dn”, i); // i = ? char * str = (char*)i; // ? return 0; } hero
  • 5. C (1972) - structured #include <stdio.h> void update(int i) { i = i + 1; } int main() { int i = 0; println(“i = %dn”, i); // i = 0 update(i); println(“i = %dn”, i); // i = ? char * str = (char*)i; // ? return 0; } master static typing pass by value it’s a high level assembly! weak type system
  • 6. C (1972) - structured 0000000000400506 <update>: 400506: 55 push %rbp 400507: 48 89 e5 mov %rsp,%rbp 40050a: 89 7d fc mov %edi,-0x4(%rbp) 40050d: 83 45 fc 01 addl $0x1,-0x4(%rbp) 400511: 5d pop %rbp 400512: c3 retq 0000000000400513 <main>: 400513: 55 push %rbp 400514: 48 89 e5 mov %rsp,%rbp 400517: 48 83 ec 10 sub $0x10,%rsp 40051b: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp) 400522: 8b 45 fc mov -0x4(%rbp),%eax 400525: 89 c6 mov %eax,%esi 400527: bf e4 05 40 00 mov $0x4005e4,%edi 40052c: b8 00 00 00 00 mov $0x0,%eax 400531: e8 aa fe ff ff callq 4003e0 <printf@plt> 400536: 8b 45 fc mov -0x4(%rbp),%eax 400539: 89 c7 mov %eax,%edi 40053b: e8 c6 ff ff ff callq 400506 <update> 400540: 8b 45 fc mov -0x4(%rbp),%eax 400543: 89 c6 mov %eax,%esi 400545: bf e4 05 40 00 mov $0x4005e4,%edi 40054a: b8 00 00 00 00 mov $0x0,%eax 40054f: e8 8c fe ff ff callq 4003e0 <printf@plt> 400554: b8 00 00 00 00 mov $0x0,%eax 400559: c9 leaveq 40055a: c3 retq 40055b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) god of metal
  • 7. C++ (1983) - Object oriented #include <iostream> using namespace std; class Point { public: float _x, _y; Point(float x, float y) : _x(x), _y(y) {} }; ostream &operator<< (ostream &os, Point const &p) { return os << "Point(" << p._x << "," << p._y << ")"; } void update(Point p) { p._x += 1; p._y += 1; } int main() { Point a(1, 2); cout << a << endl; // Point(1, 2) update(a); cout << a << endl; // Point(?, ?) char * str = (char*)a; // ? char * str = (char*)&a; // ? return 0; }
  • 8. C++ (1983) - Object oriented #include <iostream> using namespace std; class Point { public: float _x, _y; Point(float x, float y) : _x(x), _y(y) {} }; ostream &operator<< (ostream &os, Point const &p) { return os << "Point(" << p._x << "," << p._y << ")"; } void update(Point p) { p._x += 1; p._y += 1; } int main() { Point a(1, 2); cout << a << endl; update(a); cout << a << endl; char * str = (char*)a; char * st2 = (char*)&a; return 0; } static typing pass by value (also by ref, not shown in this example) a little bit higher level than assembly weak type system namespaces (operator) overloading
  • 9. module Example where data Point a = Point a a deriving (Show) update (Point x y) = Point (x+1) (y+1) applyTwice f x = f (f x) main = let a = Point 1 1 b = applyTwice update a b = update b -- ? in print b Haskell (1990) - functional
  • 10. module Example where data Point a = Point a a deriving (Show) update (Point x y) = Point (x+1) (y+1) applyTwice f x = f (f x) main = let a = Point 1 1 b = applyTwice update a b = update b -- ? in print b Haskell (1990) - functional data type and constructor strong, static typing, type inference looks and feels different. Don’t take a look at the native code generated. immutability
  • 13. Python (1991) code example class AgentManager: def add(self, name, source, sender=None, locale="en"): """ Add an agent to the list. """ self.set_locale(locale) if not self.has_permissions(sender): print(MSG_NO_PERM) return self.feedback(MSG_NO_PERM, sender) alist = self.read_list() $ python3 ... >>> 1 + "2" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str' >>> a = 1 >>> a = "this is a string" https://github.com/voiser/zoe-startup-kit/blob/master/agents/zam/zam.py
  • 14. Python (1991) high level object oriented dynamic, strong typing vm, gc pretty*syntax, significant whitespace huge community (probably there’s a python library for that!) CPython / Jython / PyPy / IronPython
  • 15. what if I describe you a language without letting you see actual code?
  • 16. high level object oriented vm jit, gc static, explicit, strong* typing (messy) generic types simple*, verbose, bureaucratic enormous community
  • 17. Java - let me break your strong type system import java.util.List; public class T3chFest { private Object[] stuff; private List<Object> rooms; public T3chFest(String[] talks, List<String> rooms) { this.stuff = talks; stuff[0] = Thread.currentThread(); // ? this.rooms = rooms; // ? List rooms2 = rooms; // ? this.rooms = rooms2; // ? } }
  • 18. ~ java + ML functional + object oriented jvm static, strong rock-solid adamantium-like heavier-than-the-gods-of-metal typing nice* syntax with (partial) type inference A language build on its type system into JVM? learn. it. now. Scala (2003)
  • 19. scala> val a = Array("a", "b", "c") a: Array[String] = Array(a, b, c) scala> val b: Array[Object] = a <console>:11: error: type mismatch; found : Array[String] required: Array[Object] Note: String <: Object, but class Array is invariant in type T. Scala (2003) - fun with invariance
  • 20. scala> class Animal; scala> class Mammal extends Animal; scala> class Human extends Mammal; scala> class Group[ +A] scala> def sizeof(g: Group[ Animal]) = ... scala> sizeof(new Group[ Animal]()) scala> sizeof(new Group[ Mammal]()) scala> class Veterinary[ -A] scala> def treat(g: Group[Mammal], v: Veterinary[ Mammal]) = ... scala> treat(..., new Veterinary[ Mammal]()) scala> treat(..., new Veterinary[ Animal]()) scala> class Treatment[ T <: Mammal] scala> def cure[B](x: Group[ B]) : Treatment[ B] = … error: type arguments [B] do not conform to class Treatment's type parameter bounds [T <: Mammal] scala> def cure[ B <: Mammal](x: Group[B]) : Treatment[ B] = ... Scala - basic types
  • 21. typeclasses structural types (a.k.a. duck typing) refined types self-recursive types path-dependent types ... tip of the iceberg
  • 22. The recruiter’s infinite knowledge Why are you interested in Scala, when Java 8 already has lambdas?
  • 23. Except where otherwise noted, this work is licensed under: http: //creativecommons.org/licenses/by-nc-sa/3.0/ Leganés, feb 11, 12 Thanks!