Skip to content

Instantly share code, notes, and snippets.

@jki127
Last active December 18, 2019 03:23
Show Gist options
  • Save jki127/cb827c061f42e3432c46acdfa82df7d8 to your computer and use it in GitHub Desktop.
Save jki127/cb827c061f42e3432c46acdfa82df7d8 to your computer and use it in GitHub Desktop.

Student Presentations III - Programming Languages - Dec 10th, 2019

Table of Contents

Haskell Type Families

Kind in Haskell

  • Type system of types
  • *:types have values
  • *->*: type constructor/function on types/types take a single parameter

Functional Dependencies

Type Families

  • Constructor representing sets of types
  • Types are used to define overloaded data

Type Synonym Families

type family f :: a

Data families vs Type families

Data families create new types, every instance of a data family declares a new constructor

Asynchronous Programming, Dart

  • Asynchronous programming - Performing multiple operations in parallel
void myFunc() async {
	print("Do something")
}

Dart will automatically assign myFunc to its own thread

Disadvantages of asynchronous programming

  • What if you need the result of an asynchronous operation?
  • What if you encounter an error?
  • How do you define the return of an operation?

Ugly Solutions

Callbacks

Passing functions (success and failure) to "callback" after asynchronous operation

Blocking

String contents = await f.readAsString();
  • await blocks the main thread until the worker thread is finished
  • more readable than callbacks

A "better" solution

Promises (called "Futures" in Dart)

  • A Promise is an abstraction of an asynchronous operation
  • Future.wait(futures).then(...)
  • Dart uses "Isolates" to achieve concurrency and improve Future performance
  • Unlike threads, Isolates do not share memory (to prevent deadlocking and read first errors) but pass messages to each other along a shared pipe
  • Each thread inherits its parent's memory

Error Handling

Future.wait(futures)
	.then(...)
	.onError(...)

Demo: Counting the number of lines in a file

We can use async operations to show the user a "loading" screen while the number of lines are being counted.

Optionals in Swift

Overview

  • Released in 2010 in Apple
  • Multi-paradigm, general purpose
  • Inspried by Python, Ruby, Obj-C, Haskell, Rust
  • Statically Typed

Optionals

  • Feature in swift to improve type-safety
  • Provides a safe solution to handle missing values
  • Similar to Maybes in Haskell
var name: String?
name = "John"
print(name) // Error: needs to be unwrapped first

if let unwrappedName = name {
	let count = unwrappedName
	print(count)
} else {
	print("The value is nil")
}

Guards

var age: Int?
age = 15

func getAge(_ age: Int?) {
	guard let unwrappedAge = age else {
		print("ERROR")
		return
	}
	print("Age is \(unwrappedAge)")
}

Force Unwrapping

var age: Int?
age = 15

print(age!)

Optional Chaining

let cars = ["BMW", "Audi", "Tesla"]
let cars2 = []
let lower = cars2.first?.lowercased()

print(lower)

first? checks if the optional has a value.

Typecasting

class Human {}

class Student : Human {}
class Professor : Human {}

let humans = [Student(), Professor()]

for person in humans {
	if let student = person as? Student {
		// call saySomething whenever the person is a student
		student.saySomething()
	}
}

D language

History

  • Up-and-coming language
  • Design goals
    • performance and safety of compiled languages
    • expressiveness of modern dynamic languages

Pros/Cons

Pros vs C++

  • Easy to read
  • Provides garbage collection
  • Array implementation better

Cons vs C++

  • Slow garbage collection
  • Less community support

Language

// Declaration
int[] arr = [1,2,3];
int[] arr2 = [4,5,6];

// Concatenation
auto arr3 = arr ~ arr2

Higher-Order Functions

  • Map, Filter, Fold like Haskell.
  • Chain will attempt to concatenate the two parameters passed to it

Demo: Filter to get words less than 4 chars

result = words.filter!(x => x.length < 4);

Classes

  • similar to C++, Java syntax
  • No multiple inheritance
  • this
  • super
  • override
  • abstract
  • public, protected, private

Memory Safety

D provides memory safety through the use of tags

  • @system - default normal functions
  • @safe - disallow function calls that may lead to memory corruption
  • @trusted - functions guaranteed not to exhibit undefined behavior

Universal Functional Call Syntax

  • syntactic sugar that allows regular functions to be called like member functions
cook("fries", 3) // => Cooking fries for 3 people
"fries".cook(3) // Same thing!

GLSL

Vertex Shader

  • Determines the position of the shader

Fragment Shader

  • Determines the color to fill shapes

GLSL

  • OpenGL Shading Language
  • C-style language
  • Built in math libraries (vector, matrix,...)
  • Control branches, function definitions
  • Does not support recursion

Data Type

  • Scalars
  • Vectors

Qualifiers

  • attribute:
    • shared between application and vertex shader
    • vertex-specific data
  • uniform:
    • shared between application and shader
    • object properties (eg. light properties, projection matrix)
  • varying:
    • shared between vertex shader and gragment shader
    • information from vertex shader (eg. effects of lighting)

Sample Vertex Shader

attribute vec3 vPosition;
attribute vec3 vColor;
varying vec4 color;

uniform mat4 model_view;
uniform mat4 projection;

...

#school/f19/proglang-f19

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