Skip to content

Instantly share code, notes, and snippets.

@DarinM223
DarinM223 / Main.hs
Created January 22, 2021 14:40
Playing around with recursion schemes
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
module Main where
import Data.Foldable (fold)
import Data.Functor.Foldable (cata, embed, hylo)
@DarinM223
DarinM223 / Main.hs
Last active January 6, 2021 00:26
ExceptT with resource handling, async, and exceptions
module Main where
import Control.Concurrent.Async (async, wait)
import Control.Exception (Exception, catch, throwIO)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Except (ExceptT (ExceptT), runExceptT)
import Control.Monad.Trans.Resource (ResourceT, allocate, release, runResourceT)
newtype MyException = MyException String deriving Show
@DarinM223
DarinM223 / concurrency.rkt
Created December 13, 2019 13:28
Concurrency+Parallelism examples (test 1 and 4 are good, 2 and 3 are bad)
#lang racket/base
(require racket/async-channel racket/future ffi/unsafe/os-thread)
(define (mandelbrot iterations x y n)
(let ([ci (- (/ (* 2.0 y) n) 1.0)]
[cr (- (/ (* 2.0 x) n) 1.5)])
(let loop ([i 0] [zr 0.0] [zi 0.0])
(if (> i iterations)
i
@DarinM223
DarinM223 / vec_get_set.rkt
Last active November 26, 2019 00:23
More convenient getting/setting of multidimensional vectors in Racket
(require syntax/parse/define
(for-syntax racket/base racket/syntax))
(define-syntax-parser v!
#:datum-literals (:=)
[(_ vec:id [e:expr ...]) #'(~> vec (vector-ref e) ...)]
[(_ vec:id [e:expr ...] := exp:expr)
(define es (syntax-e #'(e ...)))
#`(~> vec
#,@(for/list ([i (in-range 0 (length es))]
@DarinM223
DarinM223 / table.lisp
Last active December 28, 2019 10:18
Compile time table in Racket and Common Lisp
(defmacro make-table (sym &rest kvs)
(let* ((hash-sym (gensym))
(setters (iter (for (k v) in kvs)
(collect `(setf (gethash ,k ,hash-sym) ,v)))))
`(defmacro ,sym ()
(let ((,hash-sym (make-hash-table)))
,@setters
,hash-sym))))
(defmacro get-table (sym k)
@DarinM223
DarinM223 / operator_parser.rkt
Created November 23, 2019 11:39
Operator precedence parsing macro in Racket
(define-syntax (: stx)
(syntax-parse stx
; #:literals needs to have bindings in order to work
; #:datum-literals only needs symbols so it works with '^'.
#:datum-literals (+ - * / ^)
[(_ l ... + r ...) #'(+ (: l ...) (: r ...))]
[(_ l ... - r ...) #'(- (: l ...) (: r ...))]
[(_ l ... * r ...) #'(* (: l ...) (: r ...))]
[(_ l ... / r ...) #'(/ (: l ...) (: r ...))]
[(_ l ... ^ r ...) #'(expt (: l ...) (: r ...))]
@DarinM223
DarinM223 / script.hs
Created November 21, 2019 03:34
Example of scripting using Haskell and Cabal
{- cabal:
build-depends: base >= 4.11 && < 5,shelly,text
ghc-options: -Wall -O0
default-language: Haskell2010
-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Shelly
@DarinM223
DarinM223 / oops.hs
Last active November 11, 2019 12:55
Error handling with variants
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Main where
import Control.Monad.Except
import Control.Monad.Identity
import Data.Bifunctor
@DarinM223
DarinM223 / tooling.md
Last active January 26, 2021 03:33
Haskell/Purescript/Lisp tooling

Haskell

Package manager

The main package manager to use in Haskell is Cabal. Make sure that the Cabal version that you are using is at least 3.0. Then build, repl, install, etc will use the upgraded v2-style versions that work with minimal problems.

Documentation

@DarinM223
DarinM223 / implicit_params.md
Last active March 15, 2022 18:04
ImplicitParams Notes

ImplicitParams notes:

Give explicit type signatures to all functions that use implicits

All top level functions should already have explicit type signatures, and there are GHC warnings that force this rule.

However, all local functions that use implicits should