Skip to content

Instantly share code, notes, and snippets.

View yuanw's full-sized avatar

Yuan Wang yuanw

View GitHub Profile
@yuanw
yuanw / Lib.hs
Last active April 14, 2017 13:56
Beginning Haskell Chapter 6
import Data.List
import qualified Data.Map as M
-- Data.List.minimumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a --
-- M.adjust :: Ord k => (a -> a) -> k -> Map k a -> Map k a --
clusterAssignmentPhase :: (Vector v, Vectorizable e v) => [v] -> [e] -> M.Map v [e]
clusterAssignmentPhase centroids points =
let initialMap = M.fromList $ zip centroids (repeat [])
in foldr (\p m -> let chosenCentroid = minimumBy (\x y -> compare (distance x $ toVector p)
(distance y $ toVector p))
@yuanw
yuanw / learn-html-conduit.cabal
Created March 5, 2017 23:32
HTML parsing example
name: learn-html-conduit
version: 0.1.0.0
-- synopsis:
-- description:
homepage: https://github.com/githubuser/learn-html-conduit#readme
license: BSD3
license-file: LICENSE
author: Author name here
maintainer: [email protected]
copyright: 2017 Author name here
{-# LANGUAGE OverloadedStrings #-}
import CodeWorld
botCircle c = colored c (translated 0 (-1.5) (solidCircle 1))
middleCircle c = colored c (translated 0 1.5 (solidCircle 1))
topCircle c = colored c (translated 0 4.5 (solidCircle 1))
frame = rectangle 2.5 12.0
trafficLight 0 = botCircle green & middleCircle black & topCircle black & frame
@yuanw
yuanw / S3.hs
Created January 24, 2016 22:40
S3 Haskell
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
-- |
-- Module : Example.S3
-- Copyright : (c) 2013-2015 Brendan Hay
-- License : Mozilla Public License, v. 2.0.
-- Maintainer : Brendan Hay <[email protected]>
-- Stability : provisional
-- Portability : non-portable (GHC extensions)
@yuanw
yuanw / MonadTransformation.hs
Last active August 29, 2015 14:11
Haskell Monad Tranformation
import Data.Maybe
import Control.Monad
import Control.Monad (guard)
import Control.Monad.Trans (lift)
import Control.Monad.Trans.Maybe
import Data.Char
getValidPassphrase :: MaybeT IO String
getValidPassphrase = do s <- lift getLine
@yuanw
yuanw / option
Created June 11, 2014 05:10
Option
var optionalString: String? = "Hello"
optionalString = nil
var optionalName: String? = nil
var gretting = "Hello!"
if let name = optionalName {
gretting = "Hello, \(name)"
} else {
gretting = "Hello anonymous"
}
@yuanw
yuanw / string format
Created June 11, 2014 05:04
Simple String format
let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces of fruit ."
let name = "Tommy"
let helloworld = "Nice to see you \(name) at \(3.0 + 4.5) years later."
@yuanw
yuanw / Operator Overload
Created June 11, 2014 04:53
Operator Overload
let label = "The width is "
let width = 94
let widthLabel = label + width (Error: Could not findgi)
@yuanw
yuanw / Optional typing
Created June 11, 2014 04:52
Optional typing in Swift
let firstFloat: Float = 4
let secondFloat = 4.0