Skip to content

Instantly share code, notes, and snippets.

@danyx23
danyx23 / ResultErrorListApplicative.fs
Created February 14, 2018 20:48
F# Result applicative accumulating errors in a list
type Test =
{ A : string
B : int
C : float
}
let buildTest a b c =
{ A = a
B = b
C = c
@danyx23
danyx23 / SwaggerMinimal.elm
Created November 18, 2016 18:32
Recursive Elm Json decoder crashes in Elm 0.18
module SwaggerMinimal exposing (..)
import Json.Decode
exposing
( int
, string
, float
, Decoder
, map
, oneOf
@danyx23
danyx23 / SwaggerApiUpdates.sh
Created October 12, 2016 21:06
Simple recipe for how to regenerate swagger based apis without overwriting implementation
java -jar swagger-codegen-cli.jar generate -l python-flask -i SWAGGER-YAML-FILE -o api
git add api
git commit ...
# this is just a convienience for the first time you change your api
git tag -a initial-api-scaffold -m "Initial swagger scaffold. Base point for future api changes"
# work on implementation, then:
git add, commit
// This script will insert images for all the inchis it finds
// in a google document
//How to use this scipt:
// Go to a google document where you want this script to work
// Choose from the Menu: Tools -> Script editor
// Delete everything in the editor that opens
// Paste in this entire script
// Save it and close the window
//
-- This is a naive brainstorm draft for a binary parser for an imaginary binary image format.
-- The image format is assumed to be:
-- Little endian
-- 4 bytes: magic identifier 0x23 0x23 0x23 0x23
-- 2 bytes: width of image in pixels
-- 2 bytes: height of image in pixels
-- (width * height * 3) bytes: R G B values stored for each pixel (unsigned int)
import Binary.DataView.UInt8 exposing (uint8ViewLittleEndian, getBytes, getUInt16)
-- this is early brainstorm stage. Writing to arraybuffers is a mutation and we don't have those in Elm atm
-- aside from via Tasks/Cmds. Can we model mutations as something you build up and then "perform" all at once?
-- I'm not sure we could keep all of Elms guarantees (debugging, replay of actions, ...), but at least this API
-- would try to make the mutations happen in a single place? Or maybe performMutations will have to be a Cmd
-- even though it is all native JS code to satisfy the self imposed Elm constraints?
-- new library functions:
createArrayBuffer : Int -> ArrayBufferMutation
toFloat32Array : ArrayBufferMutation -> Float32ArrayMutation -- creates a typed float32 array
float32map : (Float -> Int -> Float) -> Float32ArrayMutation -> Float32ArrayMutation
-- new library function for using a FileReader contains this function:
readAsText : FileRef -> Task Error String
-- new helper to render HTML input with type set to "file" and a built-in decoder for a getting a List of FileRefs out.
-- I think this will be less prone to abuse than exposing the decoder and letting the user create the input herself
fileInput : (List FileRef -> msg) -> List (Attribute msg) -> List (Html msg) -> Html msg
type Model
= Empty
Developer Certificate of Origin
Version 1.1
Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129
Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.
{-# LANGUAGE OverloadedStrings, GeneralizedNewtypeDeriving #-}
module Text.Fractions where
import Control.Applicative
import Data.Ratio ((%))
import Text.Trifecta
import Test.Hspec
badFraction = "1/0"
alsoBad = "10"
shouldWork = "1/2"
data MyMaybe a =
MyNothing
| MyJust a
deriving (Show)
instance Functor MyMaybe where
fmap _ MyNothing = MyNothing
fmap f (MyJust a) = MyJust (f a)
instance Applicative MyMaybe