Last active
August 30, 2016 17:01
-
-
Save sleexyz/7c14f345cd3ca11599f20ab8195ffc85 to your computer and use it in GitHub Desktop.
hylogen 1.4
This file contains hidden or 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
import Hylogen.Expr | |
import Hylogen.WithHylide | |
findWithDefault :: ToGLSLType a => | |
(Expr a -> Booly) -> Expr a -> [Expr a] -> Expr a | |
findWithDefault predicate = foldr (\x acc -> sel (predicate x) x acc) | |
findWithDefault' :: (ToGLSLType a, ToGLSLType b) => | |
(Expr a -> Booly) -> Expr b -> [(Expr a, Expr b)] -> Expr b | |
findWithDefault' predicate x = foldr (\(a, b) acc -> sel (predicate a) b acc) x | |
foo :: Vec1 | |
foo = findWithDefault (`lt`10) 0 [1, 2, 11, 12, 14] | |
-- ( (true && (1.0 < 10.0)) ? 1.0 : ( (true && (2.0 < 10.0)) ? 2.0 : ( (true && (11.0 < 10.0)) ? 11.0 : ( (true && (12.0 < 10.0)) ? 12.0 : ( (true && (14.0 < 10.0)) ? 14.0 : 0.0))))) | |
bar :: Vec1 | |
bar = findWithDefault' (`gt` time) 0 [(0, 0), (100, 1)] | |
-- ( (true && (0.0 > time)) ? 0.0 : ( (true && (100.0 > time)) ? 1.0 : 0.0)) |
Here are the functions with explicit recursion:
Keep in mind sel
is just the ternary operator:
-- haskell
sel b x y
-- C, javascript
b ? x : y
If x matches by the predicate, sel branches to x. Else, it recurses down the list.
findWithDefault predicate def (x:xs) = sel (predicate x) x (findWithDefault predicate def xs)
findWithDefault predicate def [] = def
Given (a, b) from the list, if a matches by the predicate, sel branches to b. Else, it recurses down the list.
findWithDefault' predicate def ((a, b):xs) = sel (predicate a) b (findWithDefault predicate def xs)
findWithDefault' predicate def [] = def
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
without types: