Skip to content

Instantly share code, notes, and snippets.

View jonbodner's full-sized avatar

Jon Bodner jonbodner

  • @jonbodner@noc.social
View GitHub Profile
@jonbodner
jonbodner / star_solve.go
Created July 14, 2014 04:10
Solver for the 6-point Magic Star Problem
package main
import (
"fmt"
)
type ValSet struct {
one, two, three, four *int
}
@jonbodner
jonbodner / fizzbuzz_no_cond.go
Last active August 29, 2015 14:12
FizzBuzz for Go with no conditionals.
package main
import "fmt"
type fber func(int)string
func fizzer(i int) string {
return "fizz"
}
@jonbodner
jonbodner / future.go
Last active August 24, 2017 17:00
future-blog-post-1
package future
// Interface represents a future. No concrete implementation is
// exposed; all access to a future is via this interface.
type Interface interface {
// Get returns the values calculated by the future. It will pause until
// the value is calculated.
//
// If Get is invoked multiple times, the same value will be returned each time.
// Subsequent calls to Get will return instantaneously.
@jonbodner
jonbodner / process1.go
Created August 24, 2017 16:04
future-blog-post-2
type Process func() (interface{}, error)
@jonbodner
jonbodner / new1.go
Last active August 24, 2017 17:00
future-blog-post-3
func New(inFunc Process) Interface {
//…
}
@jonbodner
jonbodner / client.go
Last active August 24, 2017 17:01
future-blog-post-4
package main
import (
"time"
"fmt"
"future"
)
func doSomethingThatTakesAWhile(i int) (int, error) {
// what this does isn’t that important for the example
@jonbodner
jonbodner / futureImpl1.go
Last active August 24, 2017 17:01
future-blog-post-5
type futureImpl struct {
done chan struct{}
val interface{}
err error
}
func (f *futureImpl) Get() (interface{}, error) {
<-f.done
return f.val, f.err
}
@jonbodner
jonbodner / new2.go
Last active August 24, 2017 17:01
future-blog-post-6
// New creates a new Future that wraps the provided function.
func New(inFunc Processor) Interface {
f := &futureImpl {
done: make(chan struct{}),
}
go func() {
f.val, f.err = inFunc()
close(f.done)
}()
@jonbodner
jonbodner / future2.go
Last active August 24, 2017 17:01
future-blog-post-7
// Interface represents a future. No concrete implementation is
// exposed; all access to a future is via this interface.
type Interface interface {
// Get returns the values calculated by the future. It will pause until
// the value is calculated.
//
// If Get is invoked multiple times, the same value will be returned each time.
// Subsequent calls to Get will return instantaneously.
Get() (interface{}, error)
@jonbodner
jonbodner / getUntil1.go
Last active August 24, 2017 17:02
future-blog-post-8
func (f *futureImpl) GetUntil(d time.Duration) (interface{}, bool, error) {
select {
case <-f.done:
val, err := f.Get()
return val, false, err
case <-time.After(d):
return nil, true, nil
}
// This should never be executed