Skip to content

Instantly share code, notes, and snippets.

View stoeckley's full-sized avatar

andrew stoeckley

  • Balcony Studio
  • Netherlands
View GitHub Profile
if (foo != nil && bar > foo!) || foo == nil {
// something
}
if let foo = foo, bar > foo || self.foo == nil {
// something
}
@stoeckley
stoeckley / gist:a51669e6140490ab5590dce24762ddbf
Last active January 1, 2018 14:47
different ways of creating a default item in a dictionary if doesn't exist
var dict = [String: [String: Int]]()
if dict["foo"] == nil {
dict["foo"] = [:]
}
dict["foo"]?["bar"] = 5 // safe way to access dictionaries in general
dict["foo"]!["bar"] = 6 // we already tested for nil in the if statement, so force unwrap is safe, and maybe faster?
@stoeckley
stoeckley / gist:f6da4a656b911d247e827ef2a15badc4
Created December 31, 2017 23:58
how to initialize a let based on conditional branches
// i often do this just so i can make a variable a constant let and not a var;
// with a var I could just use a switch, but it's better to have a let when possible
// how to make this prettier?
let someConstant: Int64 =
someBoolExpression ? 1 :
someOtherBoolExpression ? 2 :
yetAnotherBoolExpression ? 3 : 0
@stoeckley
stoeckley / gist:5965f45cf6bc7853061e5d672ddd3577
Created January 1, 2018 14:04
best way to make this simple struct hashable?
// trying to keep this Hashable simple while minimizing collisions:
struct Simple: Hashable {
enum MyEnum: Int {
case a,b,c,d
}
let enumValue: MyEnum
let someInt: Int
@stoeckley
stoeckley / waves.pde
Created April 4, 2018 20:29 — forked from beesandbombs/waves.pde
waves
int[][] result;
float t, c;
float ease(float p) {
return 3*p*p - 2*p*p*p;
}
float ease(float p, float g) {
if (p < 0.5)
return 0.5 * pow(2*p, g);
@stoeckley
stoeckley / raytracer.nim
Created October 8, 2018 18:26 — forked from hcorion/raytracer.nim
A port of a raytracer, programmed in Nim, built by someone else, to sdl2
#Originally created by AdrianV and can be found here: https://gist.github.com/AdrianV/5774141
#Modified by me, hcorion to add sdl2 support and bring it up to speed with version 0.14.2 of Nim (nim-lang.org).
import math
import sequtils
import sdl2
import times
const
width = 1280
@stoeckley
stoeckley / raytracer.jl
Created November 29, 2018 23:58 — forked from IainNZ/raytracer.jl
Raytracer in Julia
const delta = sqrt(eps(Float64))
immutable Vec
x::Float64
y::Float64
z::Float64
end
+(a::Vec, b::Vec) = Vec(a.x+b.x, a.y+b.y, a.z+b.z)
-(a::Vec, b::Vec) = Vec(a.x-b.x, a.y-b.y, a.z-b.z)
*(a::Float64, b::Vec) = Vec(a*b.x, a*b.y, a*b.z)
@stoeckley
stoeckley / gfx-tinykaboom.go
Created August 13, 2019 12:14 — forked from peterhellberg/gfx-tinykaboom.go
tinykaboom with gfx
package main
import (
"image"
"image/color"
"math"
"github.com/peterhellberg/gfx"
)
@stoeckley
stoeckley / README.md
Created January 30, 2020 18:54 — forked from nikcub/README.md
Facebook PHP Source Code from August 2007