Skip to content

Instantly share code, notes, and snippets.

View davenportw15's full-sized avatar

William Davenport davenportw15

View GitHub Profile
@davenportw15
davenportw15 / SimulatedAnnealing.kt
Created April 20, 2018 18:12
Traveling Salesman through simulated annealing
package annealing
import java.awt.*
import java.awt.geom.Ellipse2D
import javax.swing.JFrame
import java.util.Random
import kotlin.math.pow
import kotlin.math.roundToInt
import java.io.File
val dictionary = "/usr/share/dict/words"
val minimumLength = 5
fun main(args: Array<String>) {
val words = File(dictionary).readLines()
val output = File("/Users/william/Desktop/words.txt").bufferedWriter()
words.forEach { word ->
@davenportw15
davenportw15 / App.kt
Created August 16, 2016 02:51
Demonstration of kotlin
fun main(args: Array<String>)
{
val users = listOf(
User(username = "will", isAdmin = true),
User(username = "james", isAdmin = true),
User(username = "aneesh", isAdmin = true),
User(username = "usain"),
User(username = "michael")
)
@davenportw15
davenportw15 / FunctionalPoints.fs
Created July 22, 2016 19:48
An F# FP example
open System
type Point = { x : double; y : double }
let distance (start : Point) (finish : Point) : double =
let x = finish.x - start.x
let y = finish.y - start.y
sqrt (x * x + y * y)
[<EntryPoint>]
@davenportw15
davenportw15 / DownloadAsync.cs
Created July 18, 2016 00:39
Async Download
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Net.Http;
namespace ConsoleApplication
{
class Program
{
@davenportw15
davenportw15 / Observable.swift
Created May 30, 2016 04:52
A simple example of reactive programming
// Automatically responds to changes in `value` property
class Observable<Value, Return> {
// Contents of observable
var value: Value
// Run whenever value changes
let onChange: Value -> Return
init(withInitial value: Value, onChange change: Value -> Return) {
@davenportw15
davenportw15 / Either.hs
Created January 9, 2016 20:25
An implementation of the Either monad in Haskell
module Either where
import Prelude hiding (Either, Left, Right)
data Either failure success = Left failure | Right success
deriving (Show, Read, Eq)
instance Functor (Either failure) where
fmap _ (Left x) = Left x
fmap f (Right x) = Right (f x)
//
// LazySquares.swift
// SwiftBook
//
// Created by William Davenport on 12/10/15.
// Copyright © 2015 William Davenport. All rights reserved.
//
import Foundation
@davenportw15
davenportw15 / HashMap.hs
Created September 7, 2015 05:51
A hashmap implementation in Haskell backed by a binary tree.
module HashMap where
import Prelude hiding (lookup)
-- | HashMap backed by a binary tree. Most operations are O(log(n))
data HashMap k v = EmptyMap | Map (k, v) (HashMap k v) (HashMap k v) deriving (Read, Show)
-- | Apply fmap over all values
instance (Ord k) => Functor (HashMap k) where
@davenportw15
davenportw15 / RailroadMonad.hs
Created August 20, 2015 00:57
An example of "railroad" error handling
module Result where
import Control.Monad
import Data.Monoid
data Result a b
= Failure a
| Success b
deriving (Show)