Skip to content

Instantly share code, notes, and snippets.

@jakewitcher
jakewitcher / upload_download_zip_from_gcp_cloud_storage.py
Last active October 5, 2023 13:52
uploading and downloading zip files to gcp cloud storage using python
import io
import os
import pathlib
from dotenv import load_dotenv
from google.cloud import storage
from google.oauth2 import service_account
from zipfile import ZipFile, ZipInfo
@jakewitcher
jakewitcher / Validation.fsx
Last active March 24, 2020 01:34
Using apply for the Validation type. Validation is exactly like Result except that the Applicative instance for Validation accumulates the Failure path results.
type Validation<'a, 'e> =
| Success of 'a
| Failure of 'e
module Validation =
let map f ma =
match ma with
| Success a -> Success (f a)
| Failure e -> Failure e
@jakewitcher
jakewitcher / StateTExample.hs
Last active March 7, 2020 03:03
An example of how to use the StateT monad in a simple application for managing contact information.
module Main where
import Lib
import Control.Monad.Trans.State (StateT(..), runStateT, evalStateT)
import Control.Monad.IO.Class (liftIO)
import Data.List (find)
import System.Exit (exitSuccess)
type FirstName = String
type LastName = String
@jakewitcher
jakewitcher / ordering.fsx
Last active March 1, 2020 12:31
Introduction to Type Classes for .NET Developers, Ordering in F#
type CoinFlip = Heads | Tails
type Ord = LT | EQ | GT
let compare (a: CoinFlip) (b: CoinFlip) =
match a <= b with
| true when a = b -> EQ
| true -> LT
| _ -> GT
// there is a compare function already in F# Core, but it returns -1, 0, and 1 instead of LT, EQ, and GT
@jakewitcher
jakewitcher / ord-info.txt
Created March 1, 2020 03:03
Introduction to Type Classes for .NET Developers, Ord :info
*Main> :i Ord
class Eq a => Ord a where
compare :: a -> a -> Ordering
(<) :: a -> a -> Bool
(<=) :: a -> a -> Bool
(>) :: a -> a -> Bool
(>=) :: a -> a -> Bool
max :: a -> a -> a
min :: a -> a -> a
{-# MINIMAL compare | (<=) #-}
@jakewitcher
jakewitcher / coinflip.cs
Last active March 1, 2020 12:28
Introduction to Type Classes for .NET Developers, CoinFlip in C#
public class CoinFlip
{
public enum Coin { Heads, Tails };
public Coin Result { get; }
public CoinFlip(Coin result)
{
Result = result;
}
@jakewitcher
jakewitcher / deriving.hs
Created March 1, 2020 02:59
Introduction to Type Classes for .NET Developers, CoinFlip driving Eq, Ord
data CoinFlip = Heads | Tails deriving (Eq, Ord)
@jakewitcher
jakewitcher / coinflip-ord.hs
Created March 1, 2020 02:57
Introduction to Type Classes for .NET Developers, CoinFlip Ord
data CoinFlip = Heads | Tails
instance Ord CoinFlip where
compare Heads Tails = LT
compare Tails Heads = GT
compare _ _ = EQ
@jakewitcher
jakewitcher / coinflip-eq.hs
Created March 1, 2020 02:56
Introduction to Type Classes for .NET Developers, CoinFlip Eq
data CoinFlip = Heads | Tails
instance Eq CoinFlip where
(==) Heads Heads = True
(==) Tails Tails = True
(==) _ _ = False
@jakewitcher
jakewitcher / number.cs
Last active March 1, 2020 03:10
Introduction to Type Classes for .NET Developers, Number interface
public interface Number<T>
{
T Add(T a, T b);
T Subtract(T a, T b);
}
public class Integer : Number<int>
{
public int Add(int a, int b) => a + b;