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

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

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

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

iex(1)> defmodule RepeatN do def repeat_n(function, count) do ...(1)> end repeat_n(function, count - 1) {:module, RepeatN,...}

Reverse Sort. array = (1..1_000).to_a. array.sort do item, other other <=> item end

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

Clojure. The Revenge of Data. by Vjeran Marcinko Kapsch CarrierCom

CPS506 - Comparative Programming Languages Elixir

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

Macros, and protocols, and metaprogramming - Oh My!

Look at all these toys!

MTAT Agile Software Development

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

functional ErlangVM Parallelism

reinsight Mobile Quick Start Guide

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

Versioned APIs with Phoenix. Elvio Vicosa

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

Developers Care About the License Using SPDX to Describe License Information. Jilayne Lovejoy 6 October 2015

Metaprogramming. Concepts of Programming Languages. Alexander Schramm. 2. November Institut für Softwaretechnik und Programmiersprachen

GRAPHIC WEB DESIGNER PROGRAM

The. Pragmatic Bookshelf. PragPub. The Second Iteration IN THIS ISSUE. * José Valim on Swift, Ruby, & Elixir

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

Python BASICS. Introduction to Python programming, basic concepts: formatting, naming conventions, variables, etc.

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Introduction 2 Lisp Part III

Elixir Documentation. Release. Elixir community

CS558 Programming Languages

Functional Programming. Pure Functional Programming

CMSC 330, Fall 2013, Practice Problems 3

Perl 6 Hands-On Tutorial

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

Lecture 1. basic Python programs, defining functions

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

Quiz 1 (March 14, 2016)

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

Functional Objects. Christopher Simpkins CS 3693, Fall Chris Simpkins (Georgia Tech) CS 3693 Scala / 1

Scala : an LLVM-targeted Scala compiler

Learn Functional Programming with Elixir

Functional programming

Accelerating Information Technology Innovation

AUTOMATION TESTING FRAMEWORK FOR LUMINOUS LMS

αrby : An Embedding of Alloy in Ruby Aleksandar Milicevic, Ido Efrati, and Daniel Jackson

CS558 Programming Languages

CMSC 330, Fall 2013, Practice Problem 3 Solutions

LFE - a lisp on the Erlang VM

Taking Off with /

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

Week 6: Introduction to Programming for GIS and Spatial Data Analysis GEO GEO A

WE ARE ALL BLESSED. Bruce Tate

Common LISP Tutorial 1 (Basic)

Using Type Annotations to Improve Your Code

Installing macos High Sierra

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

How to code control statements

Scheme as implemented by Racket

Modularly Programmable Syntax

Principles of Programming Languages 2017W, Functional Programming

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Robotics with Elixir

CMSC330 Fall 2017 Final Exam

Recap: Functions as first-class values

Accelerating Information Technology Innovation

SD314 Outils pour le Big Data

Taking the Derivative

MTAT Agile Software Development

Your First Ruby Script

Announcements For This Lecture

Structure and Interpretation of Computer Programs

Shared state model. April 3, / 29

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

About Double Dispatch

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Declarative concurrency. March 3, 2014

Lists, loops and decisions

CS558 Programming Languages. Winter 2013 Lecture 3

JavaScript: Objects, Methods, Prototypes

Splay Trees. Programming II - Elixir Version. Johan Montelius. Spring Term 2018

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

IFQL. Paul Dix Founder &

Test Listeners (Test Hooks)

G Programming Languages - Fall 2012

Red Hat Satellite 6.4

Immersion Day. Getting Started with Amazon S3. January Rev

SAS VISUAL ANALYTICS SAS VISUAL STATISTICS 7.2, 7.3 AND BEYOND

Object Model Comparisons

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Automated SQL Ownage Techniques. OWASP October 30 th, The OWASP Foundation

Lecture Notes on Induction and Recursion

CSE341 Spring 2017, Final Examination June 8, 2017

CS 61A Discussion 8: Scheme. March 23, 2017

{json:api} chris-guzman.com/jsonapi.pdf

ruby2c Automatic translation of ruby code to C. by Seattle.rb s Ryan Davis & Eric Hodel

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

HOW TO USE WORDPRESS TO BUILD A WEBSITE A STEP-BY-STEP GUIDE

Language Syntax version beta Proposed Syntax

Introduction to Python programming, II

Symbolic Computation and Common Lisp

Try the following example using the Try it option available at the top right corner of the below sample code box

Transcription:

vs

Ruby-like Syntax defmodule MyMap do def map([], _func), do: [] def map([head tail], func) do [func.(head) map(tail, func)] MyMap.map [1, 2, 3, 4], fn(i) -> i * i

Ruby to Elixir what's great and what you might miss Tobias Pfeiffer @PragTob pragtob.info

What's great

Pattern Matching defmodule Patterns do def greet(%{name: name, age: age}) do IO.puts "Hi there #{name}, what's up at #{age}?" def greet(%{name: name}) do IO.puts "Hi there #{name}" def greet(_) do IO.puts "Hi" Patterns.greet %{name: "Tobi", age: 26} Patterns.greet %{name: "Tobi"} Patterns.greet ["Mop"]

Pattern Matching defmodule Patterns do def greet(%{name: name, age: age}) do IO.puts "Hi there #{name}, what's up at #{age}?" def greet(%{name: name}) do IO.puts "Hi there #{name}" def greet(_) do IO.puts "Hi" Patterns.greet %{name: "Tobi", age: 26} Patterns.greet %{name: "Tobi"} Patterns.greet ["Mop"]

Method Overloading defmodule Patterns do def greet(%{name: name, age: age}) do IO.puts "Hi there #{name}, what's up at #{age}?" def greet(%{name: name}) do IO.puts "Hi there #{name}" def greet(_) do IO.puts "Hi" Patterns.greet %{name: "Tobi", age: 26} Patterns.greet %{name: "Tobi"} Patterns.greet ["Mop"]

Optional Type Annotations @spec all?(t) :: boolean @spec all?(t, (element -> as_boolean(term))) :: boolean def all?(enumerable, fun \\ fn(x) -> x ) def all?(enumerable, fun) when is_list(enumerable) do do_all?(enumerable, fun)

Functional Programming

Immutable Data variable = 10 do_something(variable) insert_in_db(variable)

Immutable Data person = Person.new(attributes) do_something(person) insert_in_db(person)

Immutable Data person = Person.new(attributes) person = do_something(person) insert_in_db(person)

Transformation of Data

Explicitness

Minimize state vs Hiding State

Same Input, Same Output

Testing++

Changesets def new_changeset(model, params \\ :empty) do model > cast(params, ~w(name username), []) > unique_constraint(:username) > validate_length(:username, min: 1, max: 20) def registration_changeset(model, params) do model > new_changeset(params) > cast(params, ~w(password), []) > validate_length(:password, min: 6, max: 100) > put_pass_hash()

iex(13)> user = Repo.get_by(User, name: "Homer") iex(14)> user.videos #Ecto.Association.NotLoaded<association :videos is not loaded> iex(15)> Repo.preload(user, :videos) Explicit preloading iex(16)> user.videos #Ecto.Association.NotLoaded<association :videos is not loaded> iex(17)> user = Repo.preload(user, :videos) iex(18)> user.videos [%Rumbl.Video{ meta : #Ecto.Schema.Metadata<:loaded>, category: #Ecto.Association.NotLoaded<association :category is not loaded>, category_id: nil, description: "such great many wow", id: 3, inserted_at: #Ecto.DateTime<2016-02-28T18:42:41Z>, title: "Hubidubiee", updated_at: #Ecto.DateTime<2016-02-28T18:42:41Z>, url: "www.lol.com", user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: 5}]

iex(13)> user = Repo.get_by(User, name: "Homer") iex(14)> user.videos #Ecto.Association.NotLoaded<association :videos is not loaded> iex(15)> Repo.preload(user, :videos) Explicit preloading iex(16)> user.videos #Ecto.Association.NotLoaded<association :videos is not loaded> iex(17)> user = Repo.preload(user, :videos) iex(18)> user.videos [%Rumbl.Video{ meta : #Ecto.Schema.Metadata<:loaded>, category: #Ecto.Association.NotLoaded<association :category is not loaded>, category_id: nil, description: "such great many wow", id: 3, inserted_at: #Ecto.DateTime<2016-02-28T18:42:41Z>, title: "Hubidubiee", updated_at: #Ecto.DateTime<2016-02-28T18:42:41Z>, url: "www.lol.com", user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: 5}]

iex(13)> user = Repo.get_by(User, name: "Homer") iex(14)> user.videos #Ecto.Association.NotLoaded<association :videos is not loaded> iex(15)> Repo.preload(user, :videos) Explicit preloading iex(16)> user.videos #Ecto.Association.NotLoaded<association :videos is not loaded> iex(17)> user = Repo.preload(user, :videos) iex(18)> user.videos [%Rumbl.Video{ meta : #Ecto.Schema.Metadata<:loaded>, category: #Ecto.Association.NotLoaded<association :category is not loaded>, category_id: nil, description: "such great many wow", id: 3, inserted_at: #Ecto.DateTime<2016-02-28T18:42:41Z>, title: "Hubidubiee", updated_at: #Ecto.DateTime<2016-02-28T18:42:41Z>, url: "www.lol.com", user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: 5}]

iex(13)> user = Repo.get_by(User, name: "Homer") iex(14)> user.videos #Ecto.Association.NotLoaded<association :videos is not loaded> iex(15)> Repo.preload(user, :videos) Explicit preloading iex(16)> user.videos #Ecto.Association.NotLoaded<association :videos is not loaded> iex(17)> user = Repo.preload(user, :videos) iex(18)> user.videos [%Rumbl.Video{ meta : #Ecto.Schema.Metadata<:loaded>, category: #Ecto.Association.NotLoaded<association :category is not loaded>, category_id: nil, description: "such great many wow", id: 3, inserted_at: #Ecto.DateTime<2016-02-28T18:42:41Z>, title: "Hubidubiee", updated_at: #Ecto.DateTime<2016-02-28T18:42:41Z>, url: "www.lol.com", user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: 5}]

Readability

The right tool

A new land

Meta Programming defmacro plug(plug, opts \\ []) do quote do @plugs {unquote(plug), unquote(opts), true}

What you might miss

Magic meta programming def method_missing(*args) args.map(&:to_s).join(" ") puts I can haz bareword strings!

Dirtiness

Baggage

Eco-System

Thanks & Enjoy Elixir Tobias Pfeiffer @PragTob pragtob.info

Photo Attribution CC BY-ND 2.0 https://www.flickr.com/photos/mmmswan/8918529543/ CC BY 2.0 https://flic.kr/p/ekgrrj https://flic.kr/p/8arswp CC BY-NC 2.0 https://flic.kr/p/emokpd https://www.flickr.com/photos/-jule/2728475835/ CC BY-NC-ND 2.0 https://flic.kr/p/bc4btc https://flic.kr/p/eyc7zt https://flic.kr/p/nnj2t4 https://flic.kr/p/bg2r2d CC BY-SA 2.0 https://commons.wikimedia.org/wiki/file:heckert_gnu_white.svg https://flic.kr/p/cejdc3