Skip to content

Instantly share code, notes, and snippets.

@killerstorm
Created February 6, 2015 14:48
Show Gist options
  • Save killerstorm/9c0633977cee1ecb0e0c to your computer and use it in GitHub Desktop.
Save killerstorm/9c0633977cee1ecb0e0c to your computer and use it in GitHub Desktop.
import qualified Data.Map as Map
type ColorKernel txPayload coinState = txPayload -> [coinState] -> [coinState]
data ColorTx txPayload = ColorTx { payload :: txPayload,
inputIndices :: [Int],
outputIndices :: [Int],
outputCount :: Int}
type TxId = String
type CoinId = (TxId, Int)
type CoinStateMap coinState = Map.Map CoinId coinState
data Tx txPayload = Tx { colorTx :: ColorTx txPayload, inputs :: [CoinId], txId :: TxId }
-- applyTx :: ColorKernel txPayload coinState -> Tx txPayload -> CoinStateMap coinState -> CoinStateMap coinState
maybeIndex :: [Maybe a] -> Int -> Maybe a
maybeIndex list idx = if idx < length list
then list !! idx
else Nothing
reshape :: [a] -> [Int] -> Int -> [Maybe a]
reshape list newIndices length = let imap = Map.fromList (zip newIndices list)
in map (\i -> Map.lookup i imap) [0..length-1]
applyColorKernel :: ColorKernel txPayload coinState -> ColorTx txPayload -> [Maybe coinState] -> [Maybe coinState]
applyColorKernel k tx in_values = let selectedInValues = map (maybeIndex in_values) (inputIndices tx)
inputsAreGood = and (map (maybe False (\x -> True)) selectedInValues)
outputIndicesOK = and (map ((>) (outputCount tx)) (outputIndices tx))
allCorrect = inputsAreGood && outputIndicesOK
kOutValues = k (payload tx) (map (\(Just x) -> x) selectedInValues)
in if allCorrect
then reshape kOutValues (outputIndices tx) (outputCount tx)
else replicate (outputCount tx) Nothing
trivialColorKernel :: String -> [Integer] -> [Integer]
trivialColorKernel op in_values = let out_values :: [Integer]
out_values = read op
in if (sum in_values) == (sum out_values)
then out_values
else []
main = print (applyColorKernel trivialColorKernel tx inputs)
where tx = ColorTx "[1]" [0] [0] 1
inputs = [Just 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment