MTAT Agile Software Development

Similar documents
MTAT Agile Software Development

COSC441. Lecture 8 Introduction to Erlang

Elixir Documentation. Release. Elixir community

Macros, and protocols, and metaprogramming - Oh My!

CS205: Scalable Software Systems

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

Scheme as implemented by Racket

Logic - CM0845 Introduction to Haskell

Note that pcall can be implemented using futures. That is, instead of. we can use

General Computer Science (CH ) Fall 2016 SML Tutorial: Practice Problems

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Lecture 4: Higher Order Functions

CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

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

Elixir and Phoenix fast, concurrent and explicit Tobias pragtob.info

Higher Order Functions in Haskell

Functional Programming - 2. Higher Order Functions

Elixir and Phoenix fast, concurrent and explicit Tobias pragtob.info

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Functional programming in Erlang

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Functional Programming and Haskell

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

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

QuickCheck Mini for Elixir. Thomas Arts Quviq AB

Programming Languages

A general introduction to Functional Programming using Haskell

Homework 3 COSE212, Fall 2018

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Exercises on ML. Programming Languages. Chanseok Oh

Functional Programming in Haskell for A level teachers

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Induction and Recursion

x= suppose we want to calculate these large values 1) x= ) x= ) x=3 100 * ) x= ) 7) x=100!

Ruby: Introduction, Basics

INTRODUCTION TO HASKELL

CS 320: Concepts of Programming Languages

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Lecture: Functional Programming

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

Introduction to Erlang

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

Begin at the beginning

CSE 20. Lecture 4: Number System and Boolean Function. CSE 20: Lecture2

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

Ruby: Introduction, Basics

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Program Calculus Calculational Programming

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

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

Introduction to Erlang

Variable and Data Type I

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

CSE 307: Principles of Programming Languages

CSE 307: Principles of Programming Languages

Learn Functional Programming with Elixir

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Ex: If you use a program to record sales, you will want to remember data:

CSE 115. Introduction to Computer Science I

Introduction to Programming, Aug-Dec 2006

CS 440: Programming Languages and Translators, Spring 2019 Mon

Mini-ML. CS 502 Lecture 2 8/28/08

CSE 341 Section 5. Winter 2018

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

MTAT Agile Software Development in the Cloud. Lecture 3: Ruby (cont.)

CSCE 314 Programming Languages

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Variables. location where in memory is the information stored type what sort of information is stored in that memory

Shell CSCE 314 TAMU. Higher Order Functions

In addition to the correct answer, you MUST show all your work in order to receive full credit.

CS410/510 Advanced Programming Lecture 5: Collections in Smalltalk

The type checker will complain that the two branches have different types, one is string and the other is int

Elixir and Phoenix. fast, concurrent and explicit. Tobias pragtob.info

COMP Primitive and Class Types. Yi Hong May 14, 2015

CSci 4223 Principles of Programming Languages

CS 457/557: Functional Languages

Algorithms in Systems Engineering ISE 172. Lecture 1. Dr. Ted Ralphs

Example Scheme Function: equal

CSCC24 Functional Programming Scheme Part 2

Programming Paradigms

301AA - Advanced Programming

Higher-Order Functions

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

Foundations of Computation

List Functions, and Higher-Order Functions

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

CS558 Programming Languages

Expression Values Operators. (string-append a b c ) a, b, c string-append. (substring abcd 0 2) abcd, 0, 2 substring

Chapter 15. Functional Programming Languages

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CSI 32. Lecture 15. Chapter 11 Recursion Functional Recursion 11.4 Binary Search

Standard prelude. Appendix A. A.1 Classes

Lecture 21: Functional Programming in Python. List Comprehensions

Transcription:

MTAT.03.295 Agile Software Development Lecture 2: Elixir fundamentals Luciano García-Bañuelos

Elixir s numeric values Integer arbitrary-precision arithmetic Float 64 bits, double precision 1250 0xB21 0o732 0b10111 + - * div rem / is_number is_integer is_float 1.25 1.0e-5 1_500_000 Agile Software Development - Elixir fundamentals 1

Atoms & Booleans Atoms are constant strings that are used to refer to a unique value. Start with : or capital letter. (Well suited for pattern matching) Booleans values: true and false Boolean operators: is_atom is_boolean and or not Relaxed boolean operators: &&! Agile Software Development - Elixir fundamentals 2

Strings & Charlists Double-quoted sequence of characters are strings, e.g. "This is a string" Single-quoted sequence of characters are not strings, e.g. 'This is not a string' is_binary is_list > iex.bat iex(1)> IO.inspect 'string', charlists: :as_lists iex(2)> IO.inspect "string", binaries: :as_binaries Agile Software Development - Elixir fundamentals 3

More on strings String interpolation > iex.bat iex(1)> a = 'string'; b = [1,2,3] iex(2)> "Value of 'a': #{a}" iex(3)> "Value of 'b': #{b}" iex(4)> "Value of 'b': #{inspect(b)}" Multi-line strings mlstr = """ This is a multi-line string!!! """ Agile Software Development - Elixir fundamentals 4

Collection types Tuples {:ok, 123, false} Lists [1, 2, 3, 4] [1, 2]++[3] # Concatenation [1, 2, 3]--[2] # Difference hd [1, 2, 3] # List head tl [1, 2, 3] #... Tail [1 [2, 3]] # Cons operator Maps %{:name => "Alfonso Cuarón", :age => 55} Agile Software Development - Elixir fundamentals 5

Functions in Elixir Two types: anonymous and named sum = fn (a,b) -> a + b sum.(3, 5) sum = &(&1 + &2) sum.(3, 5) defmodule Example do def factorial(0), do: 1 def factorial(n) when n > 0, do: n * factorial(n 1) Example.factorial(6) Agile Software Development - Elixir fundamentals 6

Pattern matching In Elixir a = 1 does not mean we are assigning 1 to variable a The symbol "=" asserts that the left-hand side (LHS) matches the right-hand side (RHS) > iex.bat iex(1)> a = 1 1 iex(2)> 1 = a 1 iex(3)> a = 2 2 variables can be rebound unless variables are pinned > iex.bat iex(1)> a = 1 1 iex(2)> 1 = a 1 iex(3)> ^a = 2 ** (MatchError) no match... Agile Software Development - Elixir fundamentals 7

Pattern matching on collections Let us play a little bit > iex.bat iex(1)> [1, a, 3] = [1, 2, 3] [1, 2, 3] iex(2)> a 2 iex(3)> [1, a, a] = [1, 2, 2] [1, 2, 2] iex(3)> a 2 iex(4)> [1, a, a] = [1, 2, 3] ** (MatchError) no match... > iex.bat iex(1)> [a b] = [1, 2, 3] [1, 2, 3] iex(2)> a 1 iex(3)> b [2, 3] Agile Software Development - Elixir fundamentals 8

Fold operation 1 2 3 4 5 foldl (a.k.a. reduce) foldr 1 2 3 4 5 Agile Software Development - Elixir fundamentals 9

Adding the elements in a list with a foldl 0+1 1 1+2 3 3+3 6 6+4 10 10+5 15 0 + 1 + 2 + 3 + 4 + 5 defmodule Example do def foldl([], acc, _f), do: acc def foldl([h t], acc, f), do: foldl(t, f.(acc, h), f) > iex.bat S mix iex(1)> Example.foldl([1,2,3,4,5], 0, fn(a,n) -> a + n ) 15 Agile Software Development - Elixir fundamentals 10

Higher order functions Functions that take another function as input and/or produces another one as output Three widely used: Fold (left or right) Map Filter Agile Software Development - Elixir fundamentals 11

Higher order functions (map) MAP applies a given function to each element of the input list and produces a new list thereof defmodule Example do def map([], _f), do: [] def map([h t], f), do: [f.(h) map(t, f)] Agile Software Development - Elixir fundamentals 12

Higher order functions (filter) FILTER copies elements from an input to an output list, keeping only those for which a given function returns true defmodule Example do def filter([], _f), do: [] def filter([h t], f) do case f.(h) do true -> [h filter(t, f)] _ -> filter(t, f) Agile Software Development - Elixir fundamentals 13

In class exercise Write a function that computes the average of an input list of numbers You can use any of the three functions presented here (i.e. foldl, map, filter) Agile Software Development - Elixir fundamentals 14

Binary trees defmodule BinaryTree do def insert(nil, value), do: {value, nil, nil} def insert({value, left, right}, new_value) when new_value < value do {value, insert(left, new_value), right} def insert({value, left, right}, new_value) do {value, left, insert(right, new_value)}... Agile Software Development - Elixir fundamentals 15

In class exercise Write a function that implements an in-order traversal on a given binary tree Generalize the code above to a sort of fold function that takes as input an accumulator and an anonymous function and produces an ordered list (i.e. similar to the one produced by the in-order traversal) Agile Software Development - Elixir fundamentals 16