Skip to content

Instantly share code, notes, and snippets.

@StephenWakely
StephenWakely / main.rs
Last active November 3, 2022 23:19
Monads in Rust
trait Monad<A> {
type Res<B>;
fn pure(item: A) -> Self;
fn bind<B, F: Fn(&A) -> Self::Res<B>>(&self, f: F) -> Self::Res<B>;
}
impl<A> Monad<A> for Option<A> {
type Res<B> = Option<B>;
@StephenWakely
StephenWakely / main.rs
Created November 3, 2022 22:26
Functors in Rust
trait Functor<A> {
type Res<B>;
fn fmap<B, F: Fn(&A) -> B>(&self, f: F) -> Self::Res<B>;
}
impl<A> Functor<A> for Option<A> {
type Res<B> = Option<B>;
fn fmap<B, F: Fn(&A) -> B>(&self, f: F) -> Self::Res<B> {
match self {
;;; vrl-mode.el --- Major mode for Vector Remap Language code -*- lexical-binding: t; -*-
;; Version: 0.0.1
;; Keywords: VRL major mode
;; Author: Stephen Wakely
;; Package-Requires: ((emacs "26.1"))
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
use std::collections::HashMap;
use std::env;
// Alias some types
type Count = HashMap<String, usize>;
type ParseError = String;
/// Take a list of tuples of strings and counts and
/// convert them into a hash table.
fn make_count(tpls: Vec<(String, usize)>) -> Count {
-- | Type level Peano numbers in PureScript.
module Main where
import Prelude hiding (zero,one,add)
import Effect (Effect)
import Effect.Console (log)
foreign import kind Peano
module MyPrelude
( module Prelude
, (|>), (<|)
) where
import Prelude
infixr 0 apply as |>
infixl 1 applyFlipped as <|
@StephenWakely
StephenWakely / polyservant.hs
Last active December 1, 2019 18:04
Servant with Polysemy
{-# OPTIONS_GHC -fplugin=Polysemy.Plugin #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
-- |
-- | A variable
-- x.y[3].z[2][9]
variableParser :: Parser Var
variableParser = do
pos <- getSourcePos
var <- parseSimpleVar pos <?> "Simple var"
go var
where
-- | Code for calculating the levenstein distance between two strings
module Data.Levenstein (distance) where
import Prelude
import Data.Array as Array
import Data.Array ((!!))
import Data.Function.Memoize (memoize2)
import Data.Maybe as Maybe
@StephenWakely
StephenWakely / main.hs
Created January 31, 2019 11:27
Generate purescript file from fontawesome
{-# LANGUAGE OverloadedStrings #-}
module Lib
( someFunc
) where
import System.IO
import Data.Char
import Data.Text (Text)
import qualified Data.Text as T