Skip to content

Instantly share code, notes, and snippets.

View TreeTraversal.kt
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 Aug 8, 2020
parallel map in Kotlin
View pmap.kt
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 Oct 18, 2019
Haskell literal Html
View Lib.hs
{-# 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 Sep 19, 2019
Lazy lists for Unison
View lazylist.u
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)
View docker_commands.md

enter a container's shell

docker ps -> ID

docker exec -it 42 bash

copy a file to container

View 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
View ListComprehensions.hs
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]]
View Factorial.hs
{-# 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
View factorial.c
#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)
View factorial.h
/* A table of factorials. table[i] is the factorial of i. The
* max field is calculated so that its factorial would not be an
* integer overflow.
*/
typedef struct {
unsigned max;
unsigned *table;
} factorial_table;