Last active
May 6, 2021 12:58
-
-
Save nevrome/9a53ef13677e0900e38dfb52dd26d32a to your computer and use it in GitHub Desktop.
An experiment with the lambda.r R package (Haskell and R code)
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
data FruitSet = | |
LAS | |
| SAS | |
| OFS | |
deriving (Eq, Show) | |
fSMergeList :: [FruitSet] -> Bool -> FruitSet | |
fSMergeList (x:xs) intersect = foldr (\a b -> fSMerge a b intersect) x xs | |
fSMerge :: FruitSet -> FruitSet -> Bool -> FruitSet | |
fSMerge LAS LAS _ = LAS | |
fSMerge SAS SAS _ = SAS | |
fSMerge OFS _ _ = OFS | |
fSMerge _ OFS _ = OFS | |
fSMerge LAS SAS True = SAS | |
fSMerge SAS LAS True = SAS | |
fSMerge LAS SAS False = LAS | |
fSMerge SAS LAS False = LAS |
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
library(lambda.r) | |
FruitSet("LAS") %as% "LAS" | |
FruitSet("SAS") %as% "SAS" | |
FruitSet("OFS") %as% "OFS" | |
FruitSetList(...) %::% FruitSet... : FruitSetList | |
FruitSetList(...) %as% asFruitSetList(list(...)) | |
asFruitSetList(xs) %::% list : FruitSetList | |
asFruitSetList(xs) %as% { | |
class(xs) <- c("FruitSetList") | |
xs | |
} | |
fsMerge(a, b, intersect) %::% FruitSet : FruitSet : logical : FruitSet | |
fsMerge("LAS", "LAS", intersect) %as% FruitSet("LAS") | |
fsMerge("SAS", "SAS", intersect) %as% FruitSet("SAS") | |
fsMerge("OFS", b, intersect) %as% FruitSet("OFS") | |
fsMerge(a, "OFS", intersect) %as% FruitSet("OFS") | |
fsMerge("LAS", "SAS", TRUE ) %as% FruitSet("SAS") | |
fsMerge("SAS", "LAS", TRUE ) %as% FruitSet("SAS") | |
fsMerge("LAS", "SAS", FALSE ) %as% FruitSet("LAS") | |
fsMerge("SAS", "LAS", FALSE ) %as% FruitSet("LAS") | |
fsMergeList(xs, intersect) %::% FruitSetList : logical : FruitSet | |
fsMergeList(xs, intersect) %as% | |
Reduce( | |
function(a, b) { fsMerge(a, b, intersect) }, | |
xs[tail(seq_along(xs), n = -1)], | |
init = xs[[1]] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment