Skip to content

Instantly share code, notes, and snippets.

@clojj
clojj / progressor.clj
Last active September 3, 2023 09:25
in response to http://oobaloo.co.uk/multiplexing-work-reliably-and-efficiently-with-state-machines-and-core-dot-async .... so (start) will give you a channel to put initial states on.. or you can comment-in line 70
(ns async-state-progressor.progressor
(:require [clojure.core.async :refer [go-loop >! <! thread chan timeout onto-chan]]))
(defn log-state [msg current-state]
(locking *out* (println msg (:state current-state))))
(defn download-report [{:keys [client date] :as state}]
;; snip
(assoc state :file "dummy-file"))
data class Property(val name: String, val objectRef: String? = null)
data class Model(val name: String, val properties: List<Property>)
val m1 = Model(
"m1", listOf(
Property("p11"),
Property("p12", "m2"),
)
@clojj
clojj / pmap.kt
Created August 8, 2020 18:08
parallel map in Kotlin
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.awaitAll
suspend fun <A, B> Iterable<A>.pmap(f: suspend (A) -> B): List<B> = coroutineScope {
map { async { f(it) } }.awaitAll()
}
@clojj
clojj / Lib.hs
Last active October 18, 2019 20:29
Haskell literal Html
{-# LANGUAGE OverloadedStrings #-}
module Lib
( Html, Markup(..), html, eval
) where
eval :: Html -> String
eval (Html head body) = "<html>" ++ evalHead head ++ evalBody body ++ "</html>"
evalHead :: Head -> String
@clojj
clojj / lazylist.u
Last active September 19, 2019 13:39
Lazy lists for Unison
use .builtin
use .builtin.Nat
use .builtin.Int
use .builtin.List
use .builtin.Optional
-- TODO use Fix type
type Fix f = Fix (f (Fix f))
type LazyList a = LazyNil | LazyCons a (() -> LazyList a)

enter a container's shell

docker ps -> ID

docker exec -it 42 bash

copy a file to container

@clojj
clojj / Tictactoe.hs
Created January 1, 2019 11:20
Tictactoe.hs
data Coord = One | Two | Three deriving (Enum, Eq)
data Symb = O | X | Nada deriving (Show, Eq)
type TicTacToe = Coord -> Coord -> Symb
showBoard :: TicTacToe -> String
showBoard b = unlines $ map showLine [One .. Three]
where
showLine c = unwords $ map (showHouse $ b c) [One .. Three]
showHouse b' c = show $ b' c
permutations :: (Eq a) => [a] -> [[a]]
permutations [] = [[]]
permutations l = [a:x | a <- l, x <- (permutate $ filter (\x -> x /= a) l)]
allUnique :: Eq a => [a] -> Bool
allUnique [] = True
allUnique ls = and [r | x <- ls, r <- [length (filter (\e -> e /= x) ls) == (length ls) - 1]]
{-# LANGUAGE CPP, ForeignFunctionInterface, EmptyDataDecls #-}
#include <factorial.h>
#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)
module Factorial (factorial) where
import Control.Monad
import Foreign.Ptr
import Foreign.ForeignPtr
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <factorial.h>
/* Calculates max and allocate table. Returns !0 if
* memory could not be allocated.
*/
int factorial_table_init(factorial_table *t)