- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleInstances, FlexibleContexts, MultiParamTypeClasses, OverlappingInstances #-}
class Filtering c a b where
(/-) :: c -> [a] -> [b]
class Containing a b | a -> b where
getter :: a -> [b]
instance (Containing a b, Containing b c) => Containing a c where
getter l = getter l >>= getter
instance Filtering (a -> Bool) a a where
p /- l = filter p l
instance (Filtering (a -> Bool) a a, Containing b a) => Filtering (a -> Bool) b b where
p /- l = filter (not . null . filter p . getter) l
instance (Filtering (c -> Bool) b b, Containing a b) => Filtering (c -> Bool) a b where
p /- l = p /- (l >>= getter)
data A = A { _ap :: Bool, _ab :: [B] } deriving (Show)
instance Containing A B where
getter = _ab
data B = B { _bp :: Bool, _bc :: [C] } deriving (Show)
instance Containing B C where
getter = _bc
data C = C { _cp :: Bool } deriving (Show)
test = [ A True [B True [C False]]
, A False [B True [C True, C False]]
]
main = do print ((_cp /- test) :: [B])
print ((_bp /- test) :: [A])
print ((_cp /- test) :: [C])
вы мечтали об операторах, жрущих любой тип a la PHP, в своём любимом статическом языке?
всего пара расширений GHC, и они уже в вашем проекте!
guest 14.11.2015 14:44 # −1
guest 20.11.2015 07:34 # −1
guest 20.11.2015 15:08 # −1
guest 24.11.2015 09:42 # −1
guest 22.01.2016 02:32 # 0
Олсо так лучше:
https://ideone.com/VZwx44