Skip to content

Instantly share code, notes, and snippets.

@rumblesan
Last active January 7, 2016 12:03
Show Gist options
  • Save rumblesan/042f8570115878c6ca30 to your computer and use it in GitHub Desktop.
Save rumblesan/042f8570115878c6ca30 to your computer and use it in GitHub Desktop.
Code examples for Functional Talk
data Car = Car {
parcels :: Int,
driverName :: String,
isInsured :: Boolean
}
data Bicycle = Bicycle {
parcels :: Int,
riderName :: String,
isFixie :: Boolean
}
type Courier = (String, Int)
couriers :: [Courier]
couriers = [("Juan", 8), ("Higgis", 4)]
sumDeliveries :: Int -> Courier -> Int
sumDeliveries dels courier = dels + (snd courier)
deliveries = foldl sumDeliveries 0 couriers
deliveries =
let sumDeliveries = (\d c -> d + (snd c))
in foldl sumDeliveries 0 couriers
deliveries = foldl (\d c -> d + (snd c)) 0 couriers
deliveries = foldl 0 (+) (map snd couriers)
names = fmap (\c -> fst c) couriers
names = fmap fst couriers
foldl :: (b -> a -> b) -> b -> [a] -> b
reachedTarget :: (Vehicle v) => Int -> [v] -> [String]
reachedTarget target vehicles =
let filtered = filter (\v -> (deliveries v) >= target) vehicles
in map (\v -> employee v) filtered
class Functor f where
fmap :: (a -> b) -> f a -> f b
instance Functor [] where
fmap = map
instance Functor Maybe where
fmap _ Nothing = Nothing
fmap f (Just a) = Just (f a)
instance Monad Future where
(>>=) :: Future a -> (a -> Future b) -> Future b
return :: a -> Future a
lookupCourier :: String -> Future Courier
getDeliveries :: Courier -> Future [Delivery]
deliveries :: Future [Delivery]
deliveries = (lookupCourier "Juan") >>= getDeliveries
deliveries = return "Juan" >>= lookupCourier >>= getDeliveries
deliveries = do
courier <- lookupCourier "Juan"
deliveries <- getDeliveries courier
return deliveries
foo :: (a -> b) -> [a] -> [b]
bar :: (b -> a -> b) -> b -> [a] -> b
type Courier = (String, Int)
justCourier :: Maybe Courier
justCourier = Just ("Juan", 8)
noCourier :: Maybe Courier
noCourier = Nothing
justName = fmap fst justCourier
noName = fmap fst noCourier
domath1 :: Int -> Maybe Int
domath2 :: Int -> Maybe Int
output1 = (Just 3) >>= doMath1 >>= doMath2
output2 = Nothing >>= doMath1 >>= doMath2
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
return :: a -> m a
instance Monad Maybe where
(Just x) >>= k = k x
Nothing >>= _ = Nothing
return = Just
a :: Int
a = 3 + 4
b :: String
b = “Hello, World!”
c :: Int -> Int -> Int
foo a b = a + b
d :: [Int]
d = [1, 2, 4]
struct Bike {
let parcels: Int
let rider: String
let fixie: Bool
}
public protocol Vehicle {
func deliveries() -> Int
func employee() -> String
func douchbag() -> Bool
}
extension Bike : Vehicle {
public func deliveries() -> Int {
return self.parcels
}
public func employee() -> Int {
return self.rider
}
public func douchbag() -> Int {
return self.fixie
}
}
func reachedTarget(vehicle: Vehicle, target: Int) -> Bool {
return vehicle.deliveries >= target
}
let b = Bike(parcels: 4, rider: "Juan", fixie: false)
reachedTarget(b, target: 3)
class Vehicle v where
deliveries :: v -> Int
employee :: v -> String
douchbag :: v -> Boolean
instance Vehicle Bicycle where
deliveries b = parcels b
employee b = riderName b
douchebag b = isFixie b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment