Skip to content

Instantly share code, notes, and snippets.

@jki127
Last active December 5, 2019 21:43
Show Gist options
  • Save jki127/4ae2303ceabd29762ae78783442bc750 to your computer and use it in GitHub Desktop.
Save jki127/4ae2303ceabd29762ae78783442bc750 to your computer and use it in GitHub Desktop.

Student Presentations I - Programming Languages - Dec 3rd, 2019

Table of Contents

Elm

Elm Architecture

Model -> View -> Message

Demo: Increment / Decrement Counter

import Browser
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)

-- MODEL(State)
type alias Model = {value: Int}

initModel : Model
initModel = { value = 0 }

-- UPDATE
type Msg = Increment | Decrement

update msg model = 
	case expression of 
		Increment ->
		| {model | value = model.value + 1}
		Decrement ->
		| {model | value = model.value - 1}

-- VIEW
view model =
| div [] [
| button [onClick Decrement] [text "-"]
| div [] [text (String.fromInt model.value)]
| button [onClick Increment] [text "+"]
| ]

How it works

Browser sends a message to the Elm runtime

Elm runtime calls Update which sends back a Model

Why Elm

  • Speed over other javascript frameworks like Angular and React

J language

Array-based language

  • operators are called verbs
  • a line is called a phrase
  • variables are called nouns

Math

4*3+2

This returns 20 because addition is done before multiplication. You can use parenthesis for normal order of operations.

Vectors

Index operator

i is the index operator that lets you initialize a vector.

name =. i.5

This creates a vector of 0 1 2 3 4

Functions

Every function has a monadic and diatic definition.

Documentation

The creators of J made something a website called NuVoc which lists out all of the different operators. It can be helpful since in J, operators like +. and +: do very different things.

Insert

The insert operator / is an adverb that applies a verb to every item in a list.

Julia Programming

Benefits of Julia

  • high-performance
  • expressive
  • meta-programming

Julia Strings

  • $ is a string interpolation operator
$(1+2)

Types

  • dynamic with optional types

just-in-time compiled

A function is compiled when it is called

Strings

test = "Hello World"

Arrays

A = [1,2,3,4,5]

Arrays indexes start at 1

You can get the last element of an array using end

A[end]

Arrays can have elements of different types.

[1.5, 1, "foo"]

Type stability

Example: adding a floating-point number and an integer makes performance slow

function sumofsins2(n::Integer)
	r = 0.0
	for i in 1:n
		r += sin(3.4)
	end
	return r
end

Using two floating-point numbers improves performance

function sumofsins2(n::Integer)
	r = 0.0
	for i in 1:n
		r += sin(3.4)
	end
	return r
end

Metaprogramming and Macros

macro welcome(name)
	return :(println("hello ", $name, " like CS-3314"))
end

Call the macro like so:

@welcome("foo")

Anonymous Functions

squareall(A) = map(x->x^2, A)

Scratch

https://scratch.mit.edu

  • Create for teaching kids how to program
  • Block-based visual programming language
  • Created at MIT, led by Mitchel Resnick
  • 40 million projects and 40 million monthly users
  • You can share code on the website and anyone can remix it

Makes kids less frustrated because there are no syntax errors. You just can connect some blocks together.

Block

A block is what we would call a function

Input

You can use the ask block to get user input

Printing

You can use the say block to print

Events

Scratch can detect lots of events like loudness which detects how loud the sound from your computer mic is

MyPy - Static typing in Python

We can define our own types like so

str_sequence = List[str]

We can constrain parameter types like so

def flip_seq_static(collection: str_sequence)
def print_people(collection: PersonCollection) -> None

Let's create a int stack class

class IntStack:
		def __init__(self) -> None:
			self.data: List[int] = []
		def push(self, elem:int) -> None: 
			self.data.append(elem)
		def pop(self) -> int:
			if len(self.data): return self.data.pop()
		def top(self) -> int:
			if len(self.data): return self.data[-1]

What if we wanted a stack that worked with many types?

Templates

T = TypeVar('T')
class GenStack(Generic[T]):
	def __init__(self) -> None:
			self.data: List[T] = []
		def push(self, elem:T) -> None: 
			self.data.append(elem)
		def pop(self) -> T:
			if len(self.data): return self.data.pop()
		def top(self) -> T:
			if len(self.data): return self.data[-1]

#school/f19/proglang-f19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment