Skip to content

Instantly share code, notes, and snippets.

@kspeakman
kspeakman / Api.elm
Last active August 4, 2018 13:50
JsonStuff - Elm native module to auto-encode/decode JSON
port module Api exposing (..)
import JsonStuff exposing (ToJson, FromJson, getEncoder, getDecoder)
import Http
helpPostJson : String -> ToJson a msg -> FromJson b msg -> a -> Http.Request b
helpPostJson url toJson fromJson o =
Http.request
{ method = "POST"
, headers = []
@kspeakman
kspeakman / Example.elm
Last active April 13, 2017 14:00
JWT expiration notification
-- Try to setup auto logout. But JWT parsing can fail in several ways.
--
-- Failure strategy:
-- 1. Console log failure.
-- 2. The fallback (not shown below) is to instruct the user to
-- manually log out and back in when we receive 401 Unauthorized.
--
-- TODO send failure to logging infrastructure.
notifyOnExpired : String -> Cmd Msg
@kspeakman
kspeakman / FileUtils.elm
Last active November 14, 2017 21:13
Elm file uploads as simple as I could make them
module FileUtils exposing (..)
import Native.FileUtils
import Html exposing (..)
import Html.Events exposing (..)
import Http exposing (Body)
import Json.Decode as Json
type alias File =
@kspeakman
kspeakman / Combinators.fs
Last active July 16, 2021 11:22
Idiomatic F# routing with ASP.NET Core
module Combinators
// These are basic combinators that work with ASP.NET HttpContext.
// Feel free to add your own.
//
// most of this adapted from Giraffe v0.1.0-alpha025
// https://github.com/dustinmoris/Giraffe/blob/v0.1.0-alpha025/src/Giraffe/HttpHandlers.fs
// Some combinators adapted from Suave
// https://github.com/SuaveIO/suave
// Both projects are Apache 2.0 Licensed
@kspeakman
kspeakman / ListFolds.elm
Created February 14, 2018 21:51
Fixing Elm's List folds
module ListFolds exposing (..)
{-| Alternative implementations of List.foldl and List.foldr
-- this will add the `fold` and `foldBack` functions to the List module
import ListFolds as List
List.fold (++) "" [ "Hello", " ", "World", "!" ] == "Hello World!"
-}
@kspeakman
kspeakman / GuidBits.cs
Last active April 24, 2018 16:18
Utf8Json Short GUID parser
// differences from Utf8Json GuidBits
// * parses Base64 encoded Guids
// * writes Guids without '-' character
[StructLayout(LayoutKind.Explicit, Pack = 1)]
public struct GuidBits
{
[FieldOffset(0)]
public readonly Guid Value;
@kspeakman
kspeakman / Async.fs
Created June 28, 2018 23:42
Helpers
namespace Utils
module Async =
let retn x =
async { return x }
let lift f =
f >> retn
@kspeakman
kspeakman / Ssm.fs
Last active December 13, 2018 23:43
Update-Perform style fetching parameters from AWS SSM
module Ssm
module GetParametersWorkflow =
open Amazon.SimpleSystemsManagement.Model
open System.Net
type Failure =
| ParameterRequestFailed of exn
| ParameterRequestError of HttpStatusCode * Amazon.Runtime.ResponseMetadata
@kspeakman
kspeakman / boot
Created October 2, 2020 13:00
meta-rpi-bt-dac 0.5.1 logs
Fri Mar 9 12:34:56 2018: Fri Mar 9 12:34:56 UTC 2018
Fri Mar 9 12:34:56 2018: urandom start: failed.
Fri Mar 9 12:34:56 2018: ALSA: Restoring mixer settings...
Fri Mar 9 12:34:57 2018: INIT: Entering runlevel: 5
Fri Mar 9 12:34:57 2018: Starting system message bus: dbus.
Fri Mar 9 12:34:57 2018: Starting bluetooth: bluetoothd.
Fri Mar 9 12:34:57 2018: Starting syslogd/klogd: done
Fri Mar 9 12:34:57 2018: bcm43xx_init
Fri Mar 9 12:34:57 2018: Flash firmware /lib/firmware/brcm/BCM43430A1.hcd
Fri Mar 9 12:35:02 2018: Set Controller UART speed to 921600 bit/s
@kspeakman
kspeakman / Main.fs
Last active March 31, 2021 23:11
ASP NET from F#
// F# type alias
type Builder = IApplicationBuilder -> IApplicationBuilder
let createWebServer settings =
let createBuilder () = (WebHostBuilder(), [])
let setConfig basePath (host: IWebHostBuilder, configs: Builder list) =
( host
.ConfigureAppConfiguration(fun (_: WebHostBuilderContext) (builder: IConfigurationBuilder) ->