Skip to content

Instantly share code, notes, and snippets.

@tjweir
Last active September 18, 2018 18:25
Show Gist options
  • Save tjweir/c6086928706f50484794939af9a4d984 to your computer and use it in GitHub Desktop.
Save tjweir/c6086928706f50484794939af9a4d984 to your computer and use it in GitHub Desktop.
Lists of As and B, to Lists of Tuples, to merging on Keys and calcing.
import Data.Map.Strict as M
import Prelude as P
data A = A
{ aa :: Int
, ab :: Int
} deriving (Show)
data B = B
{ ba :: Int
, bb :: Int
} deriving (Show)
findMatch :: Int -> [(Int, Int)] -> [(Int, Int)]
findMatch key = P.filter (\x -> fst x == key)
mergeThem :: [(Int, Int)] -> [(Int, Int)] -> [(Int, Int)]
mergeThem aaa bbb = P.map (\x ->
case findMatch (fst x) bbb of
[] -> (fst x, 0)
[t] -> (fst x, snd x - snd t)
) aaa
main = do
let as = [A 1 2, A 3 4]
let bs = [B 1 1, B 3 1]
let las = P.map ( \x -> ( aa x, ab x ) ) as
let bas = P.map ( \x -> ( ba x, bb x ) ) bs
print las
print bas
let res = mergeThem las bas
print res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment