Last active
January 7, 2016 12:03
-
-
Save rumblesan/042f8570115878c6ca30 to your computer and use it in GitHub Desktop.
Code examples for Functional Talk
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 Car = Car { | |
parcels :: Int, | |
driverName :: String, | |
isInsured :: Boolean | |
} | |
data Bicycle = Bicycle { | |
parcels :: Int, | |
riderName :: String, | |
isFixie :: Boolean | |
} |
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
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 |
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
foldl :: (b -> a -> b) -> b -> [a] -> b |
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
reachedTarget :: (Vehicle v) => Int -> [v] -> [String] | |
reachedTarget target vehicles = | |
let filtered = filter (\v -> (deliveries v) >= target) vehicles | |
in map (\v -> employee v) filtered |
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
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) |
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
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 | |
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
foo :: (a -> b) -> [a] -> [b] | |
bar :: (b -> a -> b) -> b -> [a] -> b |
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
type Courier = (String, Int) | |
justCourier :: Maybe Courier | |
justCourier = Just ("Juan", 8) | |
noCourier :: Maybe Courier | |
noCourier = Nothing | |
justName = fmap fst justCourier | |
noName = fmap fst noCourier |
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
domath1 :: Int -> Maybe Int | |
domath2 :: Int -> Maybe Int | |
output1 = (Just 3) >>= doMath1 >>= doMath2 | |
output2 = Nothing >>= doMath1 >>= doMath2 |
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
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 |
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
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] |
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
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) |
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
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