Skip to content

Instantly share code, notes, and snippets.

package concurrency
import (
"fmt"
"sync"
)
type Promise struct {
value interface{}
error error
package main
import (
"archive/zip"
"bytes"
"io/ioutil"
"log"
)
package main
import (
"archive/zip"
"bytes"
"io/ioutil"
func (db *DB) Transact(txFunc func(db *DB) error) (err error) {
tx := db.Begin()
if tx.Error == gorm.ErrCantStartTransaction {
// transaction already in progress
return txFunc(db)
}
defer func() {
if r := recover(); r != nil {
tx.Rollback()
panic(r)
suspend fun uvClose(handle: CPointer<uv_handle_t>): Unit = suspendCoroutine { cont ->
handle.pointed.data = StableRef.create(cont).asCPointer()
uv_close(handle, staticCFunction { handle: CPointer<uv_handle_t>? ->
handle ?: return@staticCFunction
val data = handle.pointed.data ?: return@staticCFunction
val contRef = data.asStableRef<Continuation<Unit>>()
contRef.get().resume(Unit)
contRef.dispose()
})
}
suspend fun uvClose(handle: CPointer<uv_handle_t>): Unit = suspendCoroutine { cont ->
handle.pointed.data = StableRef.create(cont).asCPointer()
uv_close(handle, staticCFunction { handle: CPointer<uv_handle_t>? ->
handle ?: return@staticCFunction
val data = handle.pointed.data ?: return@staticCFunction
val contRef = data.asStableRef<Continuation<Unit>>()
contRef.get().resume(Unit)
contRef.dispose()
})
}
@lenstr
lenstr / core.clj
Created November 12, 2012 03:50
game-of-life
(ns game-of-life.core
(:require [clojure.string :as str]
[clojure.core.reducers :as r]))
(comment
" Rules
The universe of the Game of Life is an infinite two-dimensional orthogonal
grid of square cells, each of which is in one of two possible states,
alive or dead. Every cell interacts with its eight neighbours,
@lenstr
lenstr / comp.hs
Created April 29, 2012 09:51
comp
-- LeNsTR
module Main where
comp :: (a -> a -> Bool) -> [a] -> Bool
comp f = comp' where
comp' (x:[]) = True
comp' (x:y:[]) = x `f` y
comp' (x:y:(h:t)) | x `f` y = comp' (y:h:t)
| otherwise = False
@lenstr
lenstr / elevator.clj
Created April 28, 2012 00:01
Elevators
(defn elevator [commands]
(letfn [(ff-open [[cmd & rest]]
#(case cmd
:close (ff-closed rest)
:done true
false))
(ff-closed [[cmd & rest]]
#(case cmd
:open (ff-open rest)
:up (sf-closed rest)
(defn map-ext [f x & xs]
(let [colls (cons x xs)
res (apply map f colls)
next (filter not-empty (map #(drop (count res) %) colls))]
(if (empty? next) res
(lazy-seq (concat res (apply map-ext f next))))))