Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
pyrtsa / q_maps.clj
Last active June 20, 2019 21:00
Getting Datomic query results as a sequence of maps.
(defn q-maps
[query & args]
{:pre [(map? query)]}
(let [ks (map #(-> % name (subs 1) keyword) (:find query))
rs (apply datomic.api/q query args)]
(map (partial zipmap ks) rs)))
(q-maps {:find '[?id ?val]
:where '[[?e :id ?id] [?e :val ?val]]}
db)
@pyrtsa
pyrtsa / ThrowingSubscripts.swift
Created November 12, 2015 17:19
"Throwing subscripts" in Swift 2
infix operator « { associativity left }
infix operator »« { associativity left }
postfix operator » {}
struct Index {
let value: Int
}
enum Error : ErrorType {
case OutOfBounds(index: Int, count: Int)
@pyrtsa
pyrtsa / .gitconfig
Created November 26, 2012 15:32
Best bits of my Git config
[user]
# name = Your Name ### Your name, obviously. Not mine.
# email = your.name@example.com
[core]
# editor = mate -w ### Use whatever you like.
excludesfile = ~/.gitignore ### Global ignore file for .DS_Store files etc
pager = less -+FRSX -FRX ### Make "git diff" nicer to browse
[color]
ui = auto ### Make things show up in color
[log]
@pyrtsa
pyrtsa / gist:1310524
Created October 24, 2011 22:11
take(n) and drop(n)
# Given,
def take(n, xs=None):
"""
Make a slice to the first n items, or last -n if n < 0.
In addition, if xs is given return xs[take(n)].
"""
if xs is not None: return xs[take(n)]
return slice(n, None) if n < 0 else slice(n)
@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) }
@pyrtsa
pyrtsa / gist:3499353
Created August 28, 2012 15:51
Too many parentheses, you say?

Too many parentheses, you say?

FWIW, here are a few common answers to clear your worries about parentheses in Clojure in particular:

  1. It's idiomatic to use indentation to display what's nested within a given set of parentheses. The preferred way to indent code is documented fairly well. [Edit: A few examples are found e.g. here: http://clojure-euler.wikispaces.com/Problem+001]

  2. If you're concerned about matching parens somewhere (i.e. you suspect things aren't correctly indented), use your text editor to blink the matching bracket and fix the indents if needed.

  3. Typical function definitions in Clojure are somewhere between 1 and 20 lines — there's isn't much to match. In case you have more, break things to new functions (or more likely, use the core library functions better to your advantage).

@pyrtsa
pyrtsa / throttle-SourceKitService
Created July 26, 2016 07:46
Script to keep SourceKitService from eating up all OS resources
#!/bin/bash
limit="${1-10000000}";
echo "Keeping SourceKitService below $limit KiB of virtual memory."
echo "Hit ^C to quit."
while true; do
sleep 1;
p=`pgrep ^SourceKitService$`
if [ -n "$p" ]; then
vsz=`ps -o vsz -p "$p" | tail -1`
@pyrtsa
pyrtsa / gist:6213784
Created August 12, 2013 18:46
When installing Haskell Platform fails on Mac OS X (with quick fix below)

When installing Haskell Platform fails on Mac OS X (with quick fix below)

I was fighting with Haskell last weekend. At first, I couldn't install some missing libraries with Cabal, and then, when trying to find out what's wrong, I ended up removing the whole Haskell installation — only to find out I could no longer install neither the Haskell Platform nor even just Cabal Install! The warnings I would see were more or less about the use of the single quote in source code:

Preprocessing library text-0.11.2.3...

Data/Text.hs:6:52:
     warning: missing terminating ' character [-Winvalid-pp-token]

-- Copyright : (c) 2009, 2010, 2011, 2012 Bryan O'Sullivan,

@pyrtsa
pyrtsa / StringSplit.swift
Created October 4, 2014 13:45
Splitting strings by a separator string in Swift
import Foundation
extension String {
public func split(separator: String) -> [String] {
if separator.isEmpty {
return map(self) { String($0) }
}
if var pre = self.rangeOfString(separator) {
var parts = [self.substringToIndex(pre.startIndex)]
while let rng = self.rangeOfString(separator, range: pre.endIndex..<endIndex) {
@pyrtsa
pyrtsa / gist:4504593
Created January 10, 2013 18:36
Boxing with Objective-C
// Public domain. (You're welcome.)
/// BOX(expr)
///
/// Macro. Box simple things like `CGPoint` or `CGRect` into an `NSValue`.
///
/// (Compare with the use of `@` in `@YES`, @123, @"abc" or `@(1 + 2)`.)
#define BOX(expr) ({ __typeof__(expr) _box_expr = (expr); \
[NSValue valueWithBytes:&_box_expr objCType:@encode(__typeof__(expr))]; })