Last active
August 12, 2016 23:47
-
-
Save vilterp/ae297e586f3e7afe5b00be615d01e243 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Joins exposing (..) | |
import Dict exposing (..) | |
join : List a -> List b -> (a -> b -> Bool) -> List (a, List b) | |
join left right joinTest = | |
left | |
|> List.map (\leftItem -> | |
right | |
|> List.filter (joinTest leftItem) | |
|> (\rightItems -> (leftItem, rightItems)) | |
) | |
--splitApplyCombine : (item -> key) -> (List item -> combined) -> List item -> List combined | |
--splitApplyCombine getKey combine items = | |
-- items | |
-- |> List.map (\item -> (getKey item, item)) | |
-- |> List.foldl (\(key, item) soFar -> multimapPut key item) Dict.empty | |
multimapPut : comparable -> v -> Dict comparable (List v) -> Dict comparable (List v) | |
multimapPut key value dict = | |
Dict.update | |
key | |
(\maybeEntry -> | |
case maybeEntry of | |
Just entry -> | |
Just (value :: entry) | |
Nothing -> | |
Just [value] | |
) | |
dict | |
-- testing | |
blogPosts = | |
[ { id = 1, title = "title 1", body = "body 1" } | |
, { id = 2, title = "title 2", body = "body 2" } | |
] | |
comments = | |
[ { id = 1, postId = 1, timeStamp = 1, body = "yo" } | |
, { id = 2, postId = 1, timeStamp = 2, body = "yo yourself" } | |
, { id = 3, postId = 2, timeStamp = 1, body = "whazzup" } | |
] | |
joined = | |
join | |
blogPosts | |
comments | |
(\post comment -> comment.postId == post.id) | |
{- | |
[ ( { id = 1, title = "title 1", body = "body 1" } | |
, [ { id = 1, postId = 1, timeStamp = 1, body = "yo" } | |
, { id = 2, postId = 1, timeStamp = 2, body = "yo yourself" } | |
] | |
), | |
( { id = 2, title = "title 2", body = "body 2" } | |
, [ { id = 3, postId = 2, timeStamp = 1, body = "whazzup" } ] | |
) | |
] | |
: List | |
( { body : String, id : number, title : String } | |
, List | |
{ body : String, id : number, postId : number, timeStamp : number' | |
} | |
) | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment