Skip to content

Instantly share code, notes, and snippets.

View greister's full-sized avatar
🎯
Focusing

greister

🎯
Focusing
View GitHub Profile

Advanced Functional Programming with Scala - Notes

Copyright © 2017 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x
@greister
greister / gist:a1fe07b71d961e8937d208e20d98fa93
Created February 9, 2017 14:13 — forked from osipov/gist:c2a34884a647c29765ed
Install Scala and SBT using apt-get on Ubuntu 14.04 or any Debian derivative using apt-get
sudo apt-get remove scala-library scala
sudo wget www.scala-lang.org/files/archive/scala-2.10.4.deb
sudo dpkg -i scala-2.10.4.deb
sudo apt-get update
sudo apt-get install scala
wget http://scalasbt.artifactoryonline.com/scalasbt/sbt-native-packages/org/scala-sbt/sbt/0.12.4/sbt.deb
sudo dpkg -i sbt.deb
sudo apt-get update
sudo apt-get install sbt
@greister
greister / PomToSbt.scala
Created February 6, 2017 07:48 — forked from mslinn/PomToSbt.scala
Convert pom.xml to build.sbt
import scala.xml._
// To convert a Maven pom.xml to build.sbt:
// 1) Place this code into a file called PomToSbt.scala next to pom.xml
// 2) Type scala PomtoSbt.scala > build.sbt
// The dependencies from pom.xml will be extracted and place into a complete build.sbt file
// Because most pom.xml files only refernence non-Scala dependencies, I did not use %%
val lines = (XML.load("pom.xml") \\ "dependencies") \ "dependency" map { dependency =>
val groupId = (dependency \ "groupId").text
val artifactId = (dependency \ "artifactId").text
@greister
greister / ffap-nodejs-module.el
Created December 2, 2016 12:45 — forked from davazp/ffap-nodejs-module.el
Find node module at point
;;; Try to find the string at point as a NodeJS module
(require 'ffap)
(defun ffap-nodejs-module (name)
(unless (or (string-prefix-p "/" name)
(string-prefix-p "./" name)
(string-prefix-p "../" name))
(let ((base (locate-dominating-file
default-directory
@greister
greister / article.md
Created August 29, 2016 13:27 — forked from punmechanic/article.md
static-vs-dynamic-dispatch.md

Static vs Dynamic dispatch

AKA: Why is consuming an Iterator from glutin so damn hard?


In Rust, like many other languages, there are two different ways to call a function: static and dynamic dispatch. They're used in different scenarios and each have their own pros and cons, which are summarised thusly:

  1. Static calls tend to be faster because they do not require the use of a lookup table, which incurs overhead as the compiler has to 'find' the function it is calling at runtime.
  2. Dynamic calls tend to be more conservative of space as they can be re-used. This isn't so much an issue with functions like strcpy, but with functions that have multiple types of arguments (generics, for example), the code for the function must be duplicated.
@greister
greister / You cannot unwrap an Arc that you don't own
Last active May 11, 2016 00:22 — forked from anonymous/playground.rs
You cannot unwrap an Arc that you don't own
use std::sync::{Arc, RwLock};
use std::collections::HashMap;
use std::hash::Hash;
use std::cmp::Eq;
trait SessionStore<K, V> {
fn remove(&self, key: &K) -> bool;
}
type Store<K, V> = RwLock<HashMap<K, RwLock<V>>>;
@greister
greister / trait aliases
Last active May 8, 2016 03:49 — forked from anonymous/playground.rs
Shared via Rust Playground
use std::fmt::{Debug};
use std::ops::Add;
use std::num::{Zero, One};
trait Numeric : 'static + Debug + Clone + Zero + One { }
}
impl<T> Numeric for T where T : 'static + Debug + Clone + Zero + One { }
// incoming packet handler and reconnection thread
thread::spawn(move || {
// get underlying tcpstream clone from connection object
let mut _stream_pkt_hndlr_thrd = {
let ref connection = *_client_pkt_hndlr_thrd.connection.lock().unwrap();
let stream = match connection.stream {
Some(ref s) => s,
None => panic!("No stream found in the connectino"),
};
stream.try_clone().unwrap()
@greister
greister / List.len by using "Deref".rs
Last active April 30, 2016 09:04 — forked from anonymous/playground.rs
Shared via Rust Playground
// Define a structure named `List` containing a `Vec`.
use std::fmt;
use std::ops::Deref;
struct List(Vec<i32>);
impl Deref for List {
type Target = Vec<i32>;
@greister
greister / aatch change impl panic funtion
Last active April 26, 2016 14:53 — forked from anonymous/playground.rs
Shared via Rust Playground
// http://rosettacode.org/wiki/Define_a_primitive_data_type
/// Implements a custom type named CustomInt.
/// One problem that I'm not sure how to solve is bounds checking on variable assignments.
/// This type only implements a subset of all traits within std::ops.
use std::ops;
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Copy, Clone)]
pub struct CustomInt {
value: u8,
}