Skip to content

Instantly share code, notes, and snippets.

View kristopherjohnson's full-sized avatar
💭
Huh?

Kristopher Johnson kristopherjohnson

💭
Huh?
View GitHub Profile
@kristopherjohnson
kristopherjohnson / zipCombine.swift
Last active July 11, 2018 02:18
zipWith-like function for Swift
/// Given two sequences and a function, return a sequence of results of applying
/// the function to sucessive elements of the sequences.
///
/// Like Haskell's zipWith.
func zipCombine<S1: SequenceType, S2: SequenceType, T>(
sequence1: S1,
_ sequence2: S2,
combine: (S1.Generator.Element, S2.Generator.Element) -> T
) -> AnySequence<T> {
return AnySequence { () -> AnyGenerator<T> in
@kristopherjohnson
kristopherjohnson / asLazySequence.swift
Created November 23, 2015 16:06
For a Swift SequenceType, return a lazy sequence of elements that conform to a specified type
import Foundation
extension SequenceType {
/// Return a lazy sequence of elements of this sequence that are of type T
func asLazySequence<T>() -> LazyMapSequence<LazyFilterSequence<Self>, T> {
return self.lazy.filter{ $0 is T }.map{ $0 as! T }
}
/// Return a lazy sequence of the elements that are NSDictionary instances
func asLazyNSDictionarySequence()
@kristopherjohnson
kristopherjohnson / generate_if_necessary.sh
Last active November 20, 2015 11:18
Xcode build-phase script that generates source files if they are not present or if input file is newer
src="$SRCROOT/myproject/input.json"
dst_hpp="$SRCROOT/myproject/output.hpp"
dst_cpp="$SRCROOT/myproject/output.cpp"
if [ "$src" -nt "$dst_hpp" ] || [ "$src" -nt "$dst_cpp" ]; then
echo "Generating $dst_hpp and $dst_cpp..."
"$BUILT_PRODUCTS_DIR/codegenerator" "$src" "$dst_hpp" "$dst_cpp"
else
echo "$dst_hpp and $dst_cpp are up to date."
fi
@kristopherjohnson
kristopherjohnson / updateeverything.sh
Created November 16, 2015 10:25
Bash script to update Homebrew, npm packages, Ruby gems, and OS X
#!/bin/bash
set -e
echo 'Updating homebrew...'
brew update && brew upgrade && brew cleanup
echo 'Updating npm...'
npm update -g
@kristopherjohnson
kristopherjohnson / printtime.cpp
Last active November 13, 2015 15:56
Print the current time using C++11 facilities
auto now = std::chrono::system_clock::now();
auto now_c = std::chrono::system_clock::to_time_t(now);
std::cout << std::put_time(std::localtime(&now_c), "%F %T") << '\n';
@kristopherjohnson
kristopherjohnson / log.swift
Created November 7, 2015 17:22
Simple logging functions for Swift
import Foundation
func logDebug(message: String,
function: String = __FUNCTION__, file: String = __FILE__, line: Int = __LINE__)
{
#if DEBUG
let filename = (file as NSString).lastPathComponent
let msg = "DEBUG: \(message) [\(function) \(filename):\(line)]"
NSLog("%@", msg);
#endif
@kristopherjohnson
kristopherjohnson / KeepVPNOpen.applescript
Last active December 8, 2015 19:15
AppleScript app to keep VPN connection open
on idle
tell application "System Events"
set vpn_name to "'My VPN'"
set rc to do shell script "scutil --nc status " & vpn_name
if rc does not start with "Connected" then
do shell script "scutil --nc start " & vpn_name
end if
end tell
return 120
end idle
@kristopherjohnson
kristopherjohnson / KJSimpleBrainfuck.cpp
Last active October 17, 2015 13:18
Simplified Brainfuck interpreter in C++11
/*
Copyright (c) 2015 Kristopher Johnson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
@kristopherjohnson
kristopherjohnson / KJBrainfuck.cpp
Last active December 7, 2021 03:49
Brainfuck interpreter in C++11
/*
Copyright (c) 2015 Kristopher Johnson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
@kristopherjohnson
kristopherjohnson / Makefile
Last active January 2, 2024 04:55
Simple example of using the readline library from C++
CXXFLAGS=-I/usr/local/include --std=c++11
LDFLAGS=-L/usr/local/lib -lreadline
rltest: rltest.cpp
clean:
- /bin/rm rltest