CPS506 - Comparative Programming Languages Elixir

Similar documents
The Actor Model. CSCI 5828: Foundations of Software Engineering Lecture 13 10/04/2016

Macros, and protocols, and metaprogramming - Oh My!

Starting the System & Basic Erlang Exercises

Organized by Jean-François Cloutier Held at Big Room Studios, Portland, ME. Holy Elixir, Batman! Meetup June 16, 2015

The Actor Model, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 18 10/23/2014

UNIT 3

Datatype declarations

Scala, Your Next Programming Language

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

A programming language requires two major definitions A simple one pass compiler

Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction

Look at all these toys!

Functional programming in LISP

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

CPS 506 Comparative Programming Languages. Programming Language Paradigm

COSC441. Lecture 8 Introduction to Erlang

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CS 415 Midterm Exam Fall 2003

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

The Curious Clojureist

Programming Clojure, Third Edition

LFE - a lisp on the Erlang VM

Scala : an LLVM-targeted Scala compiler

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Organization of Programming Languages CS3200/5200N. Lecture 11

Programming Language Concepts, cs2104 Lecture 04 ( )

G Programming Languages - Fall 2012

Programming Paradigms

Imperative languages

SMURF Language Reference Manual Serial MUsic Represented as Functions

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CSE3322 Programming Languages and Implementation

Erlang 101. Google Doc

Seminar in Programming Languages

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Introduction to Scheme

Introduction to Locks. Intrinsic Locks

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

sample exam Concurrent Programming tda383/dit390 Sample exam March 2016 Time:?? Place: Johanneberg

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

CS390 Principles of Concurrency and Parallelism. Lecture Notes for Lecture #5 2/2/2012. Author: Jared Hall

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Ruby-like Syntax. defmodule MyMap do def map([], _func), do: [] def map([head tail], func) do [func.(head) map(tail, func)] end end

Programming Language Concepts, cs2104 Lecture 01 ( )

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

INSTITUTE OF AERONAUTICAL ENGINEERING

Lecture 19: Functions, Types and Data Structures in Haskell

CS 314 Principles of Programming Languages

1. Introduction: Key Features of Erlang

Shell CSCE 314 TAMU. Haskell Functions

Semester Review CSC 301

CS 406/534 Compiler Construction Putting It All Together

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Principles of Programming Languages (E)

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Part I CHR tutorial. Cambridge University Press Constraint Handling Rules Thom Fruhwirth Excerpt More information

DISTRIBUTED SYSTEMS [COMP9243] Lecture 1.5: Erlang INTRODUCTION TO ERLANG BASICS: SEQUENTIAL PROGRAMMING 2. Slide 1

Taking the Derivative

Programming Paradigms Written Exam (6 CPs)

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

6. Discuss how producer-consumer problem and Dining philosophers problem are solved using concurrency in ADA. [16]

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

Functional Programming Languages (FPL)

BLM2031 Structured Programming. Zeyneb KURT

CS508-Modern Programming Solved MCQ(S) From Midterm Papers (1 TO 22 Lectures) BY Arslan

All you need is fun. Cons T Åhs Keeper of The Code

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

CSE 12 Abstract Syntax Trees

Erlang. Types, Abstract Form & Core. Salvador Tamarit Muñoz. Universitat Politècnica de València

(Refer Slide Time: 4:00)

Chapter 15. Functional Programming Languages

Chapter 6 Control Flow. June 9, 2015

Principles of Programming Languages

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

CS 457/557: Functional Languages

Example Scheme Function: equal

Introduction to lambda calculus Part 3

Concepts of Programming Languages

CSCI-GA Final Exam

CSCE 314 Programming Languages

Structure of Programming Languages Lecture 5

Control in Sequential Languages

Handout 2 August 25, 2008

let s implement a memory Asynchronous vs Synchronous the functional way a first try

Introduction to Functional Programming in Haskell 1 / 56

Scheme: Expressions & Procedures

Shared state model. April 3, / 29

Chapter 1. Fundamentals of Higher Order Programming

Chapter 15. Functional Programming Languages

SML A F unctional Functional Language Language Lecture 19

Formal Semantics. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1

Functional Programming Languages (FPL)

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Transcription:

CPS506 - Comparative Programming Languages Elixir Dr. Dave Mason Department of Computer Science Ryerson University c 2017 Dave Mason

History Joe Armstrong worked at Ericson Erlang originally for lab development 1986 1995 became production on a phone switch - 9-9 s 2006 became multi-processor

Overview Paradigms Functional Mostly immutable Rich concurrency support Syntax Erlang was Prolog-like; Elixir is more conventional Infix multi-precedent operators control structures are pattern matching Functions are defined in modules Only control structures are matching, recursion, list comprehensions fixed arity functions, but can share a name with different arity spawn, receive, send for communication Semantics tail recursion is recognized everything returns a value, control are parts of expressions parameters are call-by-value dynamicly typed functions can be spawned as processes - can receive messages Pragmatics

Elixir Example defmodule Sequential do def square(collection) do collection > Enum.map(fn x -> x * x end)) end end result =Sequential.square 1..1000

Elixir Example defmodule Sequential do def map(collection, func) do collection > Enum.map(fn x -> func.(x) end)) end end result =Sequential.map 1..1000, &(&1 * &1)

Elixir Example OO is about manipulating state Elixir is about transforming data defmodule Parallel do def pmap(collection, func) do collection > Enum.map(fn x -> Task.async(fn -> func.(x) end) > Enum.map(&Task.await/1) end end result = Parallel.pmap 1..1000, &(&1 * &1)

Elixir Example defmodule Parallel do def pmap(collection, func) do collection > Enum.map(&(Task.async(fn -> func.(&1) end))) > Enum.map(&Task.await/1) end end result = Parallel.pmap 1..1000, &(&1 * &1)

Functional Programming Object orientation is not the only way to design code. Functional programming need not be complex or mathematical. The foundations of programming are not assignments, if statements, and loops. Concurrency does not need locks, semaphores, monitors, and the like. Processes are not necessarily expensive resources. Metaprogramming is not just something tacked onto a language. Even if it is work, programming should be fun. from Elixir book

Binding and Immutability = is the binding operator not assignment assignment relates to GOTO

Expressions 1 + 2 1 < 4.0 :atom like #symbol variable = is pattern-match/binding - once [[3,4],5,6] [3,4 [5,6]] can have improper lists {2,3,4} {:valid,[h t],x} = {:valid,[1,2,3],:blat} generally use function patterns instead if e0 do e1 else e2 end cond do c1 -> e1 c2->e2 end case e0 do p1 -> e1 p2->e2 end

Definitions c "matching_function Matching_function.number(one) - can apply as functions to index negate = fn x -> -x end guards

Higher Order Functions & Lists Enum.reduce(numbers, 0, fn x, sum -> x + sum end) Enum.map(numbers, fn x -> x + 1 end) Enum.filter(numbers,small) Enum.all?([0, 1, 2],small) Enum.any?(numbers,small) Enum.take_while(numbers,3) Enum.take_while(numbers,small) Enum.drop_while(numbers,small). list comprehensions for x <-[1,2,3,4], x<3, y<-[5,6], y<x, do: {x,y} generators and filters import Enum deck = for rank <- 23456789TJQKA, suit <- CDHS, do: [suit,rank] deck > shuffle > take(13)

Loops and Recursion tail-calls properly recognized loops are simply tail-recursive function calls full power of function pattern-matching for loop control

Processes, Concurrency & Failure receive do pattern ->... pattern -> end pid = spawn(fn xxxx) pid = spawn(module,:fun,[args]) send pid,:message link(pid) leads to signal on exit pid = spawn_link(fn xxxx) register(:atom,pid) self receive... exit(status) after

Map %{key: value} map[:key] keys can be anything

Keyword lists [key: value] options parameter keys must be atoms

Stream lazy evaluation equivalent to Enum concat, cycle, take, drop

Workflow & mix mix - project builder + package manager mix new projectname mix test iex -S mix

Macros defmacro hygenic use keyword usually defines macros used in test definition

Servers module dynamically loaded into running server current/old versions existing old code continues fully qualified function calls access current code another load kills old, moves current to old, new to current -on_load(name/) allows checking if load should proceed there are higher-level task managers in OTP

Evaluation Simplicity Size of the grammar complexity of navigating modules/classes Orthogonality number of special syntax forms number of special datatypes Extensibility functional syntactically defining literals overloading