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 / then1.go
Created August 24, 2017 16:32
future-blog-post-11
func (f *futureImpl) Then(next Step) Interface {
nextFuture := New(func() (interface{}, error) {
result, err := f.Get()
if err != nil {
return result, err
}
return next(result)
})
return nextFuture
}
@jonbodner
jonbodner / future3.go
Created August 24, 2017 16:29
future-blog-post-10
// 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 / client2.go
Created August 24, 2017 16:25
future-blog-post-9
func main() {
a := 10
f := future.New(func() (interface{}, error) {
return doSomethingThatTakesAWhile(a)
})
// this will wait for a second to complete
// and will return nil for both val and err
// and timeout will be true
val, timeout, err := f.GetUntil(1 * time.Second)
@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
@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 / 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 / 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 / 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 / new1.go
Last active August 24, 2017 17:00
future-blog-post-3
func New(inFunc Process) Interface {
//…
}
@jonbodner
jonbodner / process1.go
Created August 24, 2017 16:04
future-blog-post-2
type Process func() (interface{}, error)