Skip to content

Instantly share code, notes, and snippets.

@DarinM223
DarinM223 / Associated Types.md
Last active September 21, 2023 13:24
Associated types

Introduction to associated types

One of the most useful things in typed programming languages is generics. Generics allows you to write code that works across multiple types while still being checkable by the compiler. Even better is that with many languages like Rust and C#, generics have a distinct performance advantage over runtime casting. However although generics are extremely useful, many programming languages that have them don't allow for convenient ways of expressing them, especially for traits/interfaces. Like in Java if you want a generic interface you are forced to use the same Name<Type1, Type2, Type3, ...> syntax that you would use for a class. However, that often leads to ugly overly-verbose code.

Here's an example: Lets say that you have two traits/interfaces Foo and Bar that depend on three subtypes and you wanted a function that takes in any implementation of both Foo and Bar and returns the first type of Foo and the third type of Bar. The traditional Rust version that is similar to o

@DarinM223
DarinM223 / blackjack.swift
Last active August 28, 2017 11:28
Simple Swift program to learn Swift 3
// Simple Blackjack game in Swift 3
import Foundation
// Extension to allow for shuffling of arrays
extension MutableCollection where Index == Int {
mutating func shuffle() {
if count < 2 {
return
}
main.hs:71:5: error:
• Couldn't match type ‘t0 ((->) Int)’ with ‘IO’
Expected type: IO (State GameState ())
Actual type: t0 ((->) Int) (State GameState ())
• In a stmt of a 'do' block: lift dealCards
In the expression:
do { lift dealCards;
players <- lift getPlayers;
dealer <- lift getDealer;
lift $ handlePlayers players }
error: cannot borrow immutable local variable `deck` as mutable
--> src/main.rs:45:17
|
44 | let deck = Deck::new();
| ---- use `mut deck` here to make mutable
45 | let card1 = deck.take().unwrap();
| ^^^^ cannot borrow mutably
error: cannot borrow immutable local variable `deck` as mutable
--> src/main.rs:46:17
@DarinM223
DarinM223 / atoi.rs
Created November 27, 2016 09:51
Interview prep
#[derive(PartialEq)]
pub enum State {
BeforePrefixOp,
DigitStart,
}
fn convert_to_int(s: &Vec<u8>) -> i32 {
if s.is_empty() {
return 0;
}
@DarinM223
DarinM223 / main.rs
Created January 16, 2017 05:12
Glium drawing a textured rectangle
#[macro_use]
extern crate glium;
extern crate cgmath;
extern crate image;
use cgmath::{Vector2, Matrix4};
use glium::glutin;
use glium::{DisplayBuild, Surface};
use std::io::Cursor;
@DarinM223
DarinM223 / async_timer.java
Last active February 6, 2017 09:38
Prints "The timer was called!" and the response body to a www.google.com request every second in Java using rxjava
import org.asynchttpclient.*;
import org.asynchttpclient.extras.rxjava.*;
import rx.Observable;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
AsyncHttpClient client = new DefaultAsyncHttpClient();
Observable<Long> timer = Observable.timer(1, 1, TimeUnit.SECONDS);
@DarinM223
DarinM223 / main.rs
Last active February 23, 2017 13:32
How to get server service to send http requests in Hyper?
extern crate tokio_core;
extern crate hyper;
extern crate futures;
use futures::Future;
use futures::future::ok;
use hyper::{Body, Client, Get, StatusCode};
use hyper::client::HttpConnector;
use hyper::header::ContentLength;
@DarinM223
DarinM223 / debugging_http.md
Last active March 27, 2017 04:22
Debugging HTTP requests

One day I decided to look back at an example on how to make an async Rust server and found that all requests made by my server were returning 400 bad request errors! I knew that curl was successfully getting the url so in order to fix this problem I would have to see exactly what request is being sent by both curl and my server.

One way to debug HTTP requests is to use netcat (nc) to start a server that echoes all incoming requests.

First I wanted to test the request sent from curl, so I ran nc -l 5000 to start the debugging server on one terminal tab and curl localhost:5000 in another. The response was shown below:

GET / HTTP/1.1
@DarinM223
DarinM223 / Parser.java
Created April 7, 2017 07:54
Simple parser from a context free grammar
package parser;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import static java.util.Objects.requireNonNull;
/**
* A translator that converts simple arithmetic expressions into postfix form.