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 / homeAsBack.java
Created April 23, 2014 19:04
Treat Android action-bar Home button as "Back"
@Override
public boolean onOptionsItemSelected(final MenuItem item)
{
final int itemId = item.getItemId();
switch (itemId)
{
case android.R.id.home:
// Action bar home button: go back
finish();
@kristopherjohnson
kristopherjohnson / EnumUtil.fs
Last active August 29, 2015 14:00
F# utility functions for Enum types
open System
module EnumUtil =
/// Return all values for an enumeration type
let EnumValues (enumType : Type) : seq<int> =
let values = Enum.GetValues enumType
let getValue (i : int) : int = downcast values.GetValue i
let lb = values.GetLowerBound 0
let ub = values.GetUpperBound 0
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap Template</title>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<!DOCTYPE html>
<html lang="en" ng-app>
<head>
<meta charset="utf-8">
<title>AngularJS Template</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-route.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-sanitize.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-animate.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-cookies.min.js"></script>
@kristopherjohnson
kristopherjohnson / ng-formatjson.html
Last active August 29, 2015 14:01
Web page for reformatting JSON, implemented using AngularJS
<!DOCTYPE html>
<html lang="en" ng-app="formatterApp">
<head>
<meta charset="utf-8">
<title>JSON Reformatter</title>
<!--
Copyright (c) 2014 Kristopher Johnson
Permission is hereby granted, free of charge, to any person obtaining
@kristopherjohnson
kristopherjohnson / angular_karma.conf.js
Last active August 29, 2015 14:02
Set up yeoman and grunt to generate an AngularJS project and run it on OS X using Homebrew
// NOTE: Due to bugs in the Angular/Karma generators, the Karma runner may not work correctly when following the
// steps given in yeoman_angular.sh. If that's the case, compare the contents of your test/karma.conf.js with
// this version, and edit yours as needed.
// Karma configuration
// http://karma-runner.github.io/0.12/config/configuration-file.html
// Generated on 2014-06-05 using
// generator-karma 0.8.1
module.exports = function(config) {
@kristopherjohnson
kristopherjohnson / nullcoalescing.swift
Last active August 29, 2015 14:02
Null-coalescing operator for Swift
// "A !|| B" returns unwrapped value of A if it is not nil, or B if it is
operator infix !|| { associativity left }
func !|| <T>(lhs: T?, rhs: T) -> T {
if let unwrappedValue = lhs {
return unwrappedValue;
} else {
return rhs;
}
}
@kristopherjohnson
kristopherjohnson / fsharp.swift
Last active August 29, 2015 14:02
F#-isms translated into Swift
// Wrap closure that will be evaluated only the first time force()/value is called.
// Subsequent calls will return the already-calculated value.
class Lazy<T> {
init(_ closure: () -> T) {
self.force = { [unowned self] in
let v = closure()
self.force = { return v }
return v
}
}
@kristopherjohnson
kristopherjohnson / curry.swift
Last active August 29, 2015 14:02
curry() in Swift
// Credit: https://twitter.com/eridius/status/474990322506670080
func curry<A, B, C>(f: (A, B) -> C) -> A -> B -> C {
return { a in { b in f(a, b) } }
}
func curry<A, B, C, D>(f: (A, B, C) -> D) -> A -> B -> C -> D {
return { a in { b in { c in f(a, b, c) } } }
}
@kristopherjohnson
kristopherjohnson / NumericText.swift
Last active August 29, 2015 14:02
Reusable functions/protocols/classes for text-to-string/string-to-text conversions in Swift
/*
Copyright (c) 2014 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: