Skip to content

Instantly share code, notes, and snippets.

@zudov
zudov / ConstraintFunctor.hs
Last active December 29, 2015 22:42
ConstraintKinds, Functor, Set
-- | https://jeltsch.wordpress.com/2013/02/14/the-constraint-kind/
{-# LANGUAGE ConstraintKinds, TypeFamilies #-}
import Prelude hiding (Functor(..))
import Data.Set (Set)
import qualified Data.Set as Set
import GHC.Exts (Constraint)
class Functor f where
type Object f a :: Constraint
@pyrtsa
pyrtsa / .prompt-time
Last active February 12, 2023 05:12
Make Bash report the start times all commands, and total durations of long-running ones.
export _ps1_timer
function _ps1_timer_start {
# echo START
_ps1_timer=${_ps1_timer:-`gdate +%s%N 2> /dev/null || date +%s%N`};
}
function _ps1_timer_stop {
# echo STOP
if [ -z "$_ps1_timer" ]; then
@pyrtsa
pyrtsa / .ghci
Last active August 2, 2018 15:43
.ghci — Essential parts of my Haskell REPL config.
-- Show loaded modules in window title and use a green "λ>" as prompt.
-- Subsequent lines of multi-line commands shall begin with " |".
:set prompt "\SOH\ESC]0;GHCi: %s\BEL\ESC[32;1m\STXλ>\SOH\ESC[0m\STX "
:set prompt2 "\SOH\ESC[32;1m\STX |\SOH\ESC[0m\STX "
-- Paste and evaluate text from the OS X clipboard. (The pasted text also
-- prints in yellow unless pasting quietly using :paste-quiet.)
:set -package process
:def paste \_ -> do { paste <- System.Process.readProcess "pbpaste" [] ""; let cmd = if '\n' `elem` paste then ":{\ntype Ö = ()\n" ++ paste ++ "\n:}" else paste in putStrLn ("\SOH\ESC[33m\STX" ++ paste ++ "\SOH\ESC[0m\STX") >> return (":cmd return " ++ show cmd) }
:def paste-quiet \_ -> do { paste <- System.Process.readProcess "pbpaste" [] ""; let cmd = if '\n' `elem` paste then ":{\ntype Ö = ()\n" ++ paste ++ "\n:}" else paste in return (":cmd return " ++ show cmd) }
@alanzeino
alanzeino / lldb-debugging.md
Last active February 27, 2024 14:06
LLDB debugging with examples

LLDB Debugging Cheat Sheet

Commands

LLDB Commands

LLDB comes with a great set of commands for powerful debugging.

help

Your starting point for anything. Type help to get a list of all commands, plus any user installed ones. Type 'help for more information on a command. Type help to get help for a specific option in a command too.

{-# LANGUAGE Arrows #-}
import Control.Arrow
import Control.Category
import Prelude hiding (id, (.))
-- Only for testing
import Text.Printf
newtype SF a b = SF (Float -> a -> (SF a b, b))
@pyrtsa
pyrtsa / Random.example.swift
Last active March 23, 2017 11:51
Sampling random numbers in Swift
import Random
random(0 ..< 10) // Int
random(1 ... 1000_000) // Int
random(-1000_000 ... 1000_000) // Int
random(Int.min ... Int.max) // Int
randomMax(UInt64.max) // UInt64
random(0 ... UInt64.max) // UInt64
random(1 ... UInt(10)) // UInt
@natecook1000
natecook1000 / curryRecipe.swift
Created November 9, 2014 03:37
Automatically write a function currying function with many arguments
// Curry Recipe
func curryRecipe(n: Int) -> String {
let types = join(", ", map(1...n, { "T\($0)" }))
let returnType = join(" -> ", map(1...n, { "T\($0)" }))
let closures = join(" in ", map(1...n, { "{ t\($0)" }))
let braces = join(" ", Array(count: n, repeatedValue: "}"))
return "func curry<\(types), R>(f: (\(types)) -> R) -> \(returnType) -> R {\r" +
" return \(closures) in f(\(types.lowercaseString)) \(braces)\r}"
}
@pyrtsa
pyrtsa / Managing-optionals-in-Swift.swift
Last active March 7, 2017 18:39
Managing optionals in Swift
// Hello, fellow Swift programmer!
//
// Here's a demonstration of a few ways of cleaning up the unwrapping of
// optionals in Swift.
//
// Swift is known to have both OO and functional background. Thus, there is
// probably some middle ground programming style that is *more* functional than
// Objective-C, and probably less so than Haskell. This playground tries to
// compare their differences in the pretty common scenario of dealing with
// errors or missing input.
@pyrtsa
pyrtsa / ret.clj
Last active August 29, 2015 14:04
A short-circuiting let expression in Clojure
;; This is probably either a silly idea or a foolish implementation of an okay one.
(defn ret*
[bindings & body]
(if-let [[k v & more] (not-empty bindings)]
`(let [k# ~v]
(if (reduced? k#)
k#
(let [~k k#]
~(apply ret* more body))))
@pyrtsa
pyrtsa / example.cpp
Last active August 29, 2015 13:56
C++1y: Using user-defined literals for making `std::chrono::duration`s
#include <chrono>
constexpr auto operator "" _days (unsigned long long n) { return std::chrono::hours(24 * n); }
constexpr auto operator "" _h (unsigned long long n) { return std::chrono::hours(n); }
constexpr auto operator "" _min (unsigned long long n) { return std::chrono::minutes(n); }
constexpr auto operator "" _s (unsigned long long n) { return std::chrono::seconds(n); }
constexpr auto operator "" _ms (unsigned long long n) { return std::chrono::milliseconds(n); }
constexpr auto operator "" _µs (unsigned long long n) { return std::chrono::microseconds(n); }
constexpr auto operator "" _ns (unsigned long long n) { return std::chrono::nanoseconds(n); }